积极答复者
就是C#中通过HttpWebRequest的方式无法获取完整的Cookie,

问题
答案
-
Cookie默认是空的,必须实例化之后才可以获取。
烦请参考:https://msdn.microsoft.com/zh-cn/library/system.net.httpwebrequest.cookiecontainer(v=vs.110).aspx
ASP.NET Forum
StackOverFlow
FreeRice Donate
Issues to report
Free Tech Books Search and Download- 已建议为答案 DotNet Wang 2016年9月4日 3:38
- 已标记为答案 Xavier Xie-MSFTModerator 2016年10月11日 2:12
-
您好 TheYangtzeTheYellowRiver,
>>"但是我想要的是cookie2=13e525ab4391a4227bf5285ba0daea26;这样一串东东。就是获取不到啊!?"
这一串东西有可能是之前的某个时间点向taobao发送请求的时候被设置的。你可以清空下浏览器的Cookie再试试访问taobao看看这个cookie还有没有,如果有的话,那是在那个请求的响应中被设置的。如果我们想完全的模拟浏览器的行为,我们需要把浏览器发送请求时候所有的请求参数都设置在HttpWebRequest上。
Best Regards,
Li WangWe are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.- 已建议为答案 DotNet Wang 2016年10月5日 2:00
- 已标记为答案 Xavier Xie-MSFTModerator 2016年10月11日 2:12
全部回复
-
Cookie默认是空的,必须实例化之后才可以获取。
烦请参考:https://msdn.microsoft.com/zh-cn/library/system.net.httpwebrequest.cookiecontainer(v=vs.110).aspx
ASP.NET Forum
StackOverFlow
FreeRice Donate
Issues to report
Free Tech Books Search and Download- 已建议为答案 DotNet Wang 2016年9月4日 3:38
- 已标记为答案 Xavier Xie-MSFTModerator 2016年10月11日 2:12
-
但是我设置了示例,也还是没有Cookie信息啊:
我的例子如下,求解:
CookieContainer beginCookie = new CookieContainer();
string url = "https://www.taobao.com/";
//string url = "https://detail.tmall.com/item.htm?id=24969500758";
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
myHttpWebRequest.Timeout = 20 * 1000; //连接超时
myHttpWebRequest.Accept = "text/html, application/xhtml+xml, */*";
myHttpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko";
myHttpWebRequest.KeepAlive = true;
myHttpWebRequest.CookieContainer = beginCookie; //暂存到新实例
HttpWebResponse beginResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
string temp123 = beginResponse.Headers["Set-Cookie"];
CookieCollection ccArray = beginResponse.Cookies;temp123的内容为
thw=cn; Path=/; Domain=.taobao.com; Expires=Tue, 05-Sep-17 06:54:57 GMT;
ccArray 中压根就没有,只有一个taobao.com的内容,其他什么都没有。
但是我想要的是cookie2=13e525ab4391a4227bf5285ba0daea26;这样一串东东。就是获取不到啊!?
不知道这是为什么?!求大神解惑!
-
大侠,@ThankfulHeart,求教!
-
您好 TheYangtzeTheYellowRiver,
>>"但是我想要的是cookie2=13e525ab4391a4227bf5285ba0daea26;这样一串东东。就是获取不到啊!?"
这一串东西有可能是之前的某个时间点向taobao发送请求的时候被设置的。你可以清空下浏览器的Cookie再试试访问taobao看看这个cookie还有没有,如果有的话,那是在那个请求的响应中被设置的。如果我们想完全的模拟浏览器的行为,我们需要把浏览器发送请求时候所有的请求参数都设置在HttpWebRequest上。
Best Regards,
Li WangWe are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.- 已建议为答案 DotNet Wang 2016年10月5日 2:00
- 已标记为答案 Xavier Xie-MSFTModerator 2016年10月11日 2:12