-
Notifications
You must be signed in to change notification settings - Fork 3
/
Cookies.cs
69 lines (55 loc) · 1.74 KB
/
Cookies.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System;
using System.Runtime.InteropServices;
using System.Text;
// Reading cookies from Internet Explorer
// https://www.cyotek.com/blog/reading-cookies-from-internet-explorer
// Copyright © 2019 Cyotek Ltd. All Rights Reserved.
// This source code is licensed under the Creative Commons Attribution 4.0 International License.
// To view a copy of this license, visit http://creativecommons.org/licenses/by/4.0/.
// Found this example useful?
// https://www.paypal.me/cyotek
namespace Cyotek.Demonstrations.InternetGetCookieEx
{
internal static class Cookies
{
#region Static Methods
public static string GetCookies(Uri uri)
{
return GetCookies(uri.OriginalString);
}
public static string GetCookies(string uri)
{
return GetCookies(uri, CookieRetrievalFlags.None);
}
public static string GetCookies(Uri uri, CookieRetrievalFlags flags)
{
return GetCookies(uri.OriginalString, flags);
}
public static string GetCookies(string uri, CookieRetrievalFlags flags)
{
StringBuilder buffer;
string result;
int bufferLength;
bufferLength = 1024;
buffer = new StringBuilder(bufferLength);
if (NativeMethods.InternetGetCookieEx(uri, null, buffer, ref bufferLength, (int)flags, IntPtr.Zero))
{
result = buffer.ToString();
}
else
{
result = null;
if (Marshal.GetLastWin32Error() == NativeMethods.ERROR_INSUFFICIENT_BUFFER)
{
buffer.Length = bufferLength;
if (NativeMethods.InternetGetCookieEx(uri, null, buffer, ref bufferLength, (int)flags, IntPtr.Zero))
{
result = buffer.ToString();
}
}
}
return result;
}
#endregion
}
}