网址:http://tuiguang.ganji.com/house_report/auth.php
我用httpwatch跟踪有7个cookie,而用httpwebrequest只能得到2个,不知道为啥,哪位帮个忙???
我只能得到 jsession 和 nsc_wt_uh_qpsu_80 ,连wt_fpc都得不到,很奇怪...
代码:
1 HttpWebRequest request;
2 HttpWebResponse response = null;
3
4 request = WebRequest.Create("http://tuiguang.ganji.com/house_report/auth.php") as HttpWebRequest;
5 request.CookieContainer = cookie;
6 request.Method = "GET";
7 request.Timeout = 30000;
8 request.AllowAutoRedirect = true;
9 request.ContentType = "application/x-www-form-urlencoded";
10 request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; Alexa Toolbar)";
11 request.Accept = "*/*";
12 request.Referer = url;
13 //request.Headers.Add("Accept-Language", "zh-CN");
14 //request.Headers.Add("Accept-Encoding", "gzip, deflate");
15 request.KeepAlive = true;
16 try
17 {
18 //获取服务器返回的资源
19 using (response = (HttpWebResponse)request.GetResponse())
20 {
21 response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);
22 using (StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
23 {
24
25 // request.Headers.Remove("Cookie");
26 string cookieHeader = request.CookieContainer.GetCookieHeader(request.Address);
27 if (cookieHeader.Length > 0)
28 {
29 request.Headers["Cookie"] = cookieHeader;
30 }
31
32
33
34 if (response.Cookies.Count > 0)
35 {
36 cookie.Add(response.Cookies);
37 }
38 }
39 }
40 }
41 catch
42 {
43
44 }
经初步分析.
你所说的不存在的Cookies冒似是又js文件
http://sta.ganjistatic1.com/public/js/util/ganji/ganji.__1299729635__.js
创建的.~~所以WebRequest在读取的时候并没有执行相关js脚本而所引发的此问题.~