1
using System;
2
using System.Runtime.InteropServices;
3
using System.Text;
4
using System.Net;
5![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
namespace NExplus.NSiter
7
{
8
/// <summary>
9
/// 获取Cookie的方法类。
10
/// </summary>
11
public class CookieManger
12
{
13
/// <summary>
14
/// 通过COM来获取Cookie数据。
15
/// </summary>
16
/// <param name="url">当前网址。</param>
17
/// <param name="cookieName">CookieName.</param>
18
/// <param name="cookieData">用于保存Cookie Data的<see cref="StringBuilder"/>实例。</param>
19
/// <param name="size">Cookie大小。</param>
20
/// <returns>如果成功则返回<c>true</c>,否则返回<c>false</c>。</returns>
21
[DllImport("wininet.dll", SetLastError = true)]
22
public static extern bool InternetGetCookie(
23
string url, string cookieName,
24
StringBuilder cookieData, ref int size);
25
/// <summary>
26
/// 获取当前<see cref="Uri"/>的<see cref="CookieContainer"/>实例。
27
/// </summary>
28
/// <param name="uri">当前<see cref="Uri"/>地址。</param>
29
/// <returns>当前<see cref="Uri"/>的<see cref="CookieContainer"/>实例。</returns>
30
public static CookieContainer GetUriCookieContainer(Uri uri) {
31
CookieContainer cookies = null;
32![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
// 定义Cookie数据的大小。
34
int datasize = 256;
35
StringBuilder cookieData = new StringBuilder(datasize);
36![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
if (!InternetGetCookie(uri.ToString(), null, cookieData,
38
ref datasize)) {
39
if (datasize < 0)
40
return null;
41![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
42
// 确信有足够大的空间来容纳Cookie数据。
43
cookieData = new StringBuilder(datasize);
44
if (!InternetGetCookie(uri.ToString(), null, cookieData,
45
ref datasize))
46
return null;
47
}
48![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
50
if (cookieData.Length > 0) {
51
cookies = new CookieContainer();
52
cookies.SetCookies(uri, cookieData.ToString().Replace(';', ','));
53
}
54
return cookies;
55
}
56![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
57
}
58
}
是不是相当简单阿,希望对大家有一点用处。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
42
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
43
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
48
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
50
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
51
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
52
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
54
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
55
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
56
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
57
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
58
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)