通过COM来获取CookieContainer,简单又好用[转]
最近在做一个通过HttpWebRequest来实现登陆和自动发信息的小软件,虽然还没有实现,但是今天从一个老外的网站上看到了一个获取CookieContainer很好的方法,拿出来共享一下,这是我看到过代码最少而且很不错的了
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}
或者另一种方法
//加上这个就可以直接存Cookies啦
HttpWebRequest myHttpWebRequest = WebRequest.Create(url) as HttpWebRequest;
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
myHttpWebRequest.Method = "GET";
myHttpWebRequest.Timeout = 60 * 1000;
if (myCookieContainer == null)
{
myCookieContainer = new CookieContainer();
}
myHttpWebRequest.CookieContainer = myCookieContainer;
/// <summary>
/// 设置获取页面Session/Cookies数据
/// </summary>
private CookieContainer MyCookieContainer
{
set { myCookieContainer = value; }
get { return myCookieContainer; }
}
//其实也未尝一定要用Com
HttpWebRequest myHttpWebRequest = WebRequest.Create(url) as HttpWebRequest;
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
myHttpWebRequest.Method = "GET";
myHttpWebRequest.Timeout = 60 * 1000;
if (myCookieContainer == null)
{
myCookieContainer = new CookieContainer();
}
myHttpWebRequest.CookieContainer = myCookieContainer;
/// <summary>
/// 设置获取页面Session/Cookies数据
/// </summary>
private CookieContainer MyCookieContainer
{
set { myCookieContainer = value; }
get { return myCookieContainer; }
}
//其实也未尝一定要用Com
满招损,谦受益,学,永无止境