gpt4 book ai didi

c# - HttpWebRequest 使用 cookie 重定向

转载 作者:太空宇宙 更新时间:2023-11-03 14:09:40 26 4
gpt4 key购买 nike

我知道有很多关于 HttpWebRequest 的问题被问到。但是,我找不到我遇到的问题的任何答案。

我需要模仿浏览器重定向 URL 的方式。我的代码工作正常但不适用于以下 URL:

我的代码:

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");

IE 的 Fiddler,请求:

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 的代码,请求:

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

IE 的 Fiddler,响应:

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 的代码,响应:

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 不同。我从 Fiddler 看到的唯一区别是请求没有发送 cookie。但是,这些 cookie 是从哪里来的,我该如何设置它们?

非常感谢,

最佳答案

它在使用 IE 时发送的 cookie 必须在先前使用 Set-Cookie header 响应的请求期间保存在您的 session 或硬盘上。

如果您想手动设置 cookie 以与您的初始请求一起发送,您可以这样做。

CookieContainer oCookies = new CookieContainer();
CookieCollection cookieGroup = new CookieCollection();
Cookie userId = new Cookie("UserID1","19177152736", "/", ".adfarm1.adition.com");
cookieGroup.Add(userId);
oCookies.Add(cookieGroup);

然后像这样将它添加到您的 HTTPWebRequest 中。

myHttpWebRequest.CookieContainer = oCookies;

如果您想在任何后续请求中包含 cookie,只需确保将 httpWebRequests CookieContainer 设置为 oCookies。

关于c# - HttpWebRequest 使用 cookie 重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8245034/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com