首页 新闻 会员 周边 捐助

httpwebrequest获取cookie不全的问题(有图有代码,目前无真相...)

0
悬赏园豆:50 [已解决问题] 解决于 2011-03-14 16:49

网址:http://tuiguang.ganji.com/house_report/auth.php

 我用httpwatch跟踪有7个cookie,而用httpwebrequest只能得到2个,不知道为啥,哪位帮个忙??? 

 我只能得到 jsession 和 nsc_wt_uh_qpsu_80 ,连wt_fpc都得不到,很奇怪... 

代码:

View Code
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 }
问题补充: 补充一下,我清了一下cookie,发现默认不是7个,是三个,但我的request还是只能得到两个,这个没有得到 __utmc Sent 32156897 / ganji.com (Session) 貌似跟域名有关系,但是我不知道怎么得到这个(ganji.com)域名下的cookie.
卡蒙的主页 卡蒙 | 初学一级 | 园豆:135
提问于:2011-03-14 14:53
< >
分享
最佳答案
0

经初步分析.

你所说的不存在的Cookies冒似是又js文件

http://sta.ganjistatic1.com/public/js/util/ganji/ganji.__1299729635__.js

创建的.~~所以WebRequest在读取的时候并没有执行相关js脚本而所引发的此问题.~

收获园豆:50
dotNetDR_ | 老鸟四级 |园豆:2078 | 2011-03-14 16:36
哥,很强大,分都给你.我用别的方法试试..
卡蒙 | 园豆:135 (初学一级) | 2011-03-14 16:48
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册