HttpWebRequest的使用cookie重定向 [英] HttpWebRequest redirect with cookies
问题描述
我知道很多问题都已经问HttpWebRequest的。但是,我找不到我有这个问题的任何答案。
I know a lot of questions have been asked about HttpWebRequest. However, I can't find any answer for the problem I'm having.
我要模仿浏览器重定向URL的方式。我的代码工作正常,但不是为下面的网址:
I need to mimic the way browsers redirect URLs. My code works fine but not for the URL below:
我的代码:
sURL = "http://ad2.adfarm1.adition.com/redi*lid=689397953768/sid=404178/kid=253598/bid=847344/c=33349/keyword=/sr=0/clickurl=&ClickTarget=_blank&";
CookieContainer oCookies = new CookieContainer();
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(sURL);
myHttpWebRequest.CookieContainer = oCookies;
myHttpWebRequest.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)";
myHttpWebRequest.Accept = "text/html, application/xhtml+xml, */*";
myHttpWebRequest.Headers["Accept-Language"] = "en-GB";
myHttpWebRequest.Headers["Accept-Encoding"] = "gzip, deflate";
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
sNewURL = myHttpWebResponse.GetResponseHeader("Location");
提琴手,请求:
Fiddler by IE, Request:
GET http://ad2.adfarm1.adition.com/redi*lid=689397953768/sid=404178/kid=253598/bid=847344/c=33349/keyword=/sr=0/clickurl=&ClickTarget=_blank& HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Accept-Language: en-GB
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
Host: ad2.adfarm1.adition.com
Cookie: fc3=101c3; co=1; UserID1=19177152736; lv_248940=w=470167|t=1321974830; lv_249431=w=125082|t=1321974853; lc_253598=w=404178|t=1321977996
通过代码小提琴手,请求:
Fiddler by code, Request:
GET http://ad2.adfarm1.adition.com/redi*lid=689397953768/sid=404178/kid=253598/bid=847344/c=33349/keyword=/sr=0/clickurl=&ClickTarget=_blank& HTTP/1.1
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
Accept: text/html, application/xhtml+xml, */*
Accept-Language: en-GB
Accept-Encoding: gzip, deflate
Host: ad2.adfarm1.adition.com
Connection: Keep-Alive
菲德勒:
HTTP/1.1 302 Found
Server: ADITIONSERVER v1.0
Date: Tue, 22 Nov 2011 17:37:39 +0100
Connection: close
Content-Type: text/plain
Location: &ClickTarget=_blank&
P3P: policyref="http://imagesrv.adition.com/w3c/p3p.xml",CP="NOI DSP COR NID ADMo OUR NOR COM"
Set-Cookie: co=1; expires=Wed, 01-Jan-2025 00:00:00 GMT; path=/; domain=.adfarm1.adition.com
Set-Cookie: UserID1=19177152736; expires=Sun, 20-May-2012 18:37:39 GMT; path=/; domain=.adfarm1.adition.com
Set-Cookie: lc_253598=w=404178|t=1321979859; expires=Tue, 22-Nov-2011 18:07:39 GMT; path=/; domain=.adfarm1.adition.com
通过代码小提琴手,响应:
Fiddler by code, Response:
HTTP/1.1 302 Found
Server: ADITIONSERVER v1.0
Date: Tue, 22 Nov 2011 18:07:39 +0100
Connection: close
Content-Type: text/plain
Location: http://ad2.adfarm1.adition.com:80/redi?co=1&bid=847344&c=33349&keyword=&kid=253598&lid=689397953768&sid=404178&sr=0&clickurl=%26ClickTarget%3D%5Fblank%26
P3P: policyref="http://imagesrv.adition.com/w3c/p3p.xml",CP="NOI DSP COR NID ADMo OUR NOR COM"
Set-Cookie: co=1; expires=Wed, 01-Jan-2025 00:00:00 GMT; path=/; domain=.adfarm1.adition.com
正如你所看到的,位置值从我的代码获得从IE浏览器的不同。我可以从小提琴手看到的唯一区别是饼干都没有被发送请求。但是,如果是那些来自哪里饼干和我怎样才能将它们设置?
As you can see, the location value I got from the code is different from IE. The only difference I can see from Fiddler is the cookies are not been send by the request. However, where are those cookies coming from and how can I set them?
非常感谢,
推荐答案
它使用IE当发送的cookie必须随一个Set-Cookie头回答前一个请求期间被保存在您的会话或上观看高清。
The cookies it is sending when using IE must have been saved in your session or on your hd during a previous request which responded with a Set-Cookie header.
如果你想手动设置cookies来与您最初的要求,你会做这样的事情发。
If you want to manually set cookies to send with your initial request you would do something like this.
CookieContainer oCookies = new CookieContainer();
CookieCollection cookieGroup = new CookieCollection();
Cookie userId = new Cookie("UserID1","19177152736", "/", ".adfarm1.adition.com");
cookieGroup.Add(userId);
oCookies.Add(cookieGroup);
然后将其添加到您的HttpWebRequest这样。
then add it to your HTTPWebRequest like this.
myHttpWebRequest.CookieContainer = oCookies;
如果您想在任何后续请求饼干只要确保设置httpWebRequests的CookieContainer来oCookies
If you want to include the cookies in any subsequent requests just make sure to set that httpWebRequests CookieContainer to oCookies.
这篇关于HttpWebRequest的使用cookie重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!