今天帮朋友做了个工具,用webbrowser做的,用户使用用户名密码登陆网站后,需要在后台下载和分析一些页面。

分析页面使用的是htmlparser .net版

里面唯一需要解决的问题是,登陆后的cookie在webbrowser上,使用HttpWebRequest无法保存其状态。

因为中间有几个cookie值是httponly的!

上网查询到使用InternetGetCookieEx函数可以在ie8中读出httponly的值

然后,解决方法如下:

这段代码可以获取登陆后的cookie

 [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool InternetGetCookieEx(string pchURL, string pchCookieName, StringBuilder pchCookieData, ref int pcchCookieData, int dwFlags, object lpReserved); private static string GetCookieString(string url)
{
// Determine the size of the cookie
int datasize = ;
StringBuilder cookieData = new StringBuilder(datasize); if (!InternetGetCookieEx(url, null, cookieData, ref datasize, 0x00002000, null))
{
if (datasize < )
return null; // Allocate stringbuilder large enough to hold the cookie
cookieData = new StringBuilder(datasize);
if (!InternetGetCookieEx(url, null, cookieData, ref datasize, 0x00002000, null))
return null;
}
return cookieData.ToString();
}

使用HttpWebRequest获取页面

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.Headers.Clear();
request.Headers[HttpRequestHeader.Cookie] = GetCookieString(url);
request.ContentType = "application/x-www-form-urlencoded";
request.KeepAlive = true;
request.AllowAutoRedirect = true;
HttpWebResponse httpResponse = (HttpWebResponse)request.GetResponse();
using (System.IO.Stream dataStream = httpResponse.GetResponseStream())
{
using (System.IO.StreamReader sr = new System.IO.StreamReader(dataStream, Encoding.GetEncoding("gb2312")))
{
html = sr.ReadToEnd();
sr.Close();
}
}
httpResponse.Close();

一些相关链接:

http://msdn.microsoft.com/en-us/library/aa384714.aspx 官方描述

http://s.yanghao.org/program/viewdetail.php?i=5382 这里面的讨论很有帮助

http://forums.asp.net/p/1598274/4071908.aspx  InternetGetCookieEx 的申明方法和INTERNET_COOKIE_HTTPONLY的值

http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/64b8cdc6-ca00-4c31-b450-a781419d982c 另一种申明,未采纳

http://topic.csdn.net/u/20090813/10/c28dc154-3ab5-4401-86b1-ddef7f005fda.html 一个有些作用的讨论

转这篇 提示使用INTERNET_COOKIE_HTTPONLY

http://blog.csdn.net/windless0530/archive/2010/05/12/5583618.aspx

InternetSetCookieEx和InternetGetCookieEx使用tip(COOKIE文件格式) 收藏 这两个函数都要注意,szCookieName这个参数不太好使,最好设为NULL,然后在写cookie时手动写在内容里:

"my_cookie_name=my_cookie_value; path=/; expires=Wed, 30-Mar-11 09:00:00 GMT; domain=.csdn.net"

读的时候整个读出来然后自己解析。

另外,InternetGetCookieEx的dwFlag参数,对于IE8,最好加上INTERNET_COOKIE_HTTPONLY,这样才能读出具有httponly属性的cookie字段值。如果SDK比较老,没有这个宏,可以直接从DLL里GetProcAddress把这两个函数扒出来,然后定义这个值为0x00002000使用。对于IE6和IE7,想获取HTTPONLY的COOKIE,就只好手动读取COOKIE文件然后解析了。

COOKIE文件格式:

_ntes_nnid                                  // 字段名

456f74e9863f8f4b1a1e37774b0c464d,0            // 字段值

163.com/                                     // 字段所属域

3584                                            // 标志位

3205176064                                // 过期时间(低位)

37425091                                    // 过期时间(高位)

2444768976                                // 创建时间(低位)

30082544                                    // 创建时间(高位)

05-07 11:26
查看更多