在WebBrowser下对网页进行操作其实是一件挺轻松的事情,他可以很方便实现自定义的网站访问习惯。而WebBrowser毕竟是对MS原生控件的封装,当我们使用C#下的WebBrowser尤其是这样,虽然他可以更方便大家去掉用,但是他的方便却是牺牲了灵活性为代价的。
有一天我想使用WebBrowser获取一个网站的COOKIE,在调用Document.Cookie时,发现无法完整获取其COOKIE,百思不得其解,这么简单的功能,WebBrowser也会秀逗。经过抓包分析,发现原来该网站对写入到用户端关键的Cookie,加入httponly
HttpOnly,其实是一个提高WEB网站应用程序安全性的一个功能,但是大家往往都把他给忽略掉了。比如在ASP.NET 2.0的web.config的配制文件里就提供这个子的设置
转载请注明出处:http://www.mobans.cn/net/1524.html
1 <httpCookies httpOnlyCookies="true" />
2 // ***********.NET的代码如:
3 HttpCookie myCookie = new HttpCookie("myCookie");
4 myCookie.HttpOnly = true;
5 Response.AppendCookie(myCookie);
2 // ***********.NET的代码如:
3 HttpCookie myCookie = new HttpCookie("myCookie");
4 myCookie.HttpOnly = true;
5 Response.AppendCookie(myCookie);
就是这个小小的设置,令WebBrowser拿他却是没辙,查了大量资料,要想实现这个功能,一定需要实现对最原始的网络流的读写,这种方法对很多程序员来说实现难度太大了。下面就教大家实现一个很简单的实现方法
1 public string Cookie()
2 {
3 if (this.Url == null)
4 return null;
5 string dir = this.Url.Host;
6 FileStream fr = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.Cookies) + "\\index.dat", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
7 byte[] __dat = new byte[(int)fr.Length];
8 fr.Read(__dat, 0, __dat.Length);
9 fr.Close();
10 fr.Dispose();
11 string __datstream = Encoding.Default.GetString(__dat);
12 int p1 = 0;
13 p1 = __datstream.IndexOf("@" + dir, p1);
14 if (p1 == -1)
15 p1 = __datstream.IndexOf("@" + dir.Substring(dir.IndexOf('.') + 1));
16 if (p1 == -1)
17 return this.Document.Cookie;
18 int p2 = __datstream.IndexOf(".txt", p1 + 1);
19 p1 = __datstream.LastIndexOf('@', p2);
20 string dm = __datstream.Substring(p1 + 1, p2 - p1 + 3).TrimStart('?');
21 p1 = __datstream.LastIndexOf(":", p1);
22 p2 = __datstream.IndexOf('@', ++p1);
23 __datstream = string.Format("{0}@{1}", __datstream.Substring(p1, p2 - p1), dm);
24
25 Dictionary<string, string> __cookiedicts = new Dictionary<string, string>();
26 string __n;
27 StringBuilder __cookies = new StringBuilder();
28 __datstream = File.ReadAllText(Environment.GetFolderPath(Environment.SpecialFolder.Cookies) + "\\" + __datstream, Encoding.Default);
29 p1 = -2;
30 do
31 {
32 p1 += 2;
33 p2 = __datstream.IndexOf('\n', p1);
34 if (p2 == -1)
35 break;
36 __n = __datstream.Substring(p1, p2 - p1);
37 p1 = p2 + 1;
38 p2 = __datstream.IndexOf('\n', p1);
39 if (!__cookiedicts.ContainsKey(__n))
40 __cookiedicts.Add(__n, __datstream.Substring(p1, p2 - p1));
41 }
42 while ((p1 = __datstream.IndexOf("*\n", p1)) > -1);
43 if (this.Document.Cookie != null && this.Document.Cookie.Length > 0)
44 {
45 foreach (string s in this.Document.Cookie.Split(';'))
46 {
47 p1 = s.IndexOf('=');
48 if (p1 == -1)
49 continue;
50 __datstream = s.Substring(0, p1).TrimStart();
51 if (__cookiedicts.ContainsKey(__datstream))
52 __cookiedicts[__datstream] = s.Substring(p1 + 1);
53 else
54 __cookiedicts.Add(__datstream, s.Substring(p1 + 1));
55 }
56 }
57 foreach (string s in __cookiedicts.Keys)
58 {
59 if (__cookies.Length > 0)
60 __cookies.Append(';');
61 __cookies.Append(s);
62 __cookies.Append('=');
63 __cookies.Append(__cookiedicts[s]);
64 }
65 return __cookies.ToString();
66 }
2 {
3 if (this.Url == null)
4 return null;
5 string dir = this.Url.Host;
6 FileStream fr = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.Cookies) + "\\index.dat", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
7 byte[] __dat = new byte[(int)fr.Length];
8 fr.Read(__dat, 0, __dat.Length);
9 fr.Close();
10 fr.Dispose();
11 string __datstream = Encoding.Default.GetString(__dat);
12 int p1 = 0;
13 p1 = __datstream.IndexOf("@" + dir, p1);
14 if (p1 == -1)
15 p1 = __datstream.IndexOf("@" + dir.Substring(dir.IndexOf('.') + 1));
16 if (p1 == -1)
17 return this.Document.Cookie;
18 int p2 = __datstream.IndexOf(".txt", p1 + 1);
19 p1 = __datstream.LastIndexOf('@', p2);
20 string dm = __datstream.Substring(p1 + 1, p2 - p1 + 3).TrimStart('?');
21 p1 = __datstream.LastIndexOf(":", p1);
22 p2 = __datstream.IndexOf('@', ++p1);
23 __datstream = string.Format("{0}@{1}", __datstream.Substring(p1, p2 - p1), dm);
24
25 Dictionary<string, string> __cookiedicts = new Dictionary<string, string>();
26 string __n;
27 StringBuilder __cookies = new StringBuilder();
28 __datstream = File.ReadAllText(Environment.GetFolderPath(Environment.SpecialFolder.Cookies) + "\\" + __datstream, Encoding.Default);
29 p1 = -2;
30 do
31 {
32 p1 += 2;
33 p2 = __datstream.IndexOf('\n', p1);
34 if (p2 == -1)
35 break;
36 __n = __datstream.Substring(p1, p2 - p1);
37 p1 = p2 + 1;
38 p2 = __datstream.IndexOf('\n', p1);
39 if (!__cookiedicts.ContainsKey(__n))
40 __cookiedicts.Add(__n, __datstream.Substring(p1, p2 - p1));
41 }
42 while ((p1 = __datstream.IndexOf("*\n", p1)) > -1);
43 if (this.Document.Cookie != null && this.Document.Cookie.Length > 0)
44 {
45 foreach (string s in this.Document.Cookie.Split(';'))
46 {
47 p1 = s.IndexOf('=');
48 if (p1 == -1)
49 continue;
50 __datstream = s.Substring(0, p1).TrimStart();
51 if (__cookiedicts.ContainsKey(__datstream))
52 __cookiedicts[__datstream] = s.Substring(p1 + 1);
53 else
54 __cookiedicts.Add(__datstream, s.Substring(p1 + 1));
55 }
56 }
57 foreach (string s in __cookiedicts.Keys)
58 {
59 if (__cookies.Length > 0)
60 __cookies.Append(';');
61 __cookies.Append(s);
62 __cookies.Append('=');
63 __cookies.Append(__cookiedicts[s]);
64 }
65 return __cookies.ToString();
66 }