1using System;
2using System.Runtime.InteropServices;
3using System.Text;
4using System.Net;
5
6namespace 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
33 // 定义Cookie数据的大小。
34 int datasize = 256;
35 StringBuilder cookieData = new StringBuilder(datasize);
36
37 if (!InternetGetCookie(uri.ToString(), null, cookieData,
38 ref datasize)) {
39 if (datasize < 0)
40 return null;
41
42 // 确信有足够大的空间来容纳Cookie数据。
43 cookieData = new StringBuilder(datasize);
44 if (!InternetGetCookie(uri.ToString(), null, cookieData,
45 ref datasize))
46 return null;
47 }
48
49
50 if (cookieData.Length > 0) {
51 cookies = new CookieContainer();
52 cookies.SetCookies(uri, cookieData.ToString().Replace(';', ','));
53 }
54 return cookies;
55 }
56
57 }
58}
是不是相当简单阿,希望对大家有一点用处。
2using System.Runtime.InteropServices;
3using System.Text;
4using System.Net;
5
6namespace 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
33 // 定义Cookie数据的大小。
34 int datasize = 256;
35 StringBuilder cookieData = new StringBuilder(datasize);
36
37 if (!InternetGetCookie(uri.ToString(), null, cookieData,
38 ref datasize)) {
39 if (datasize < 0)
40 return null;
41
42 // 确信有足够大的空间来容纳Cookie数据。
43 cookieData = new StringBuilder(datasize);
44 if (!InternetGetCookie(uri.ToString(), null, cookieData,
45 ref datasize))
46 return null;
47 }
48
49
50 if (cookieData.Length > 0) {
51 cookies = new CookieContainer();
52 cookies.SetCookies(uri, cookieData.ToString().Replace(';', ','));
53 }
54 return cookies;
55 }
56
57 }
58}