发送的HttpWebRequest/Response返回cookie [英] HttpWebRequest/Response return cookies sent
问题描述
我收到404 Not Found错误,因为我的应用程序不像浏览器那样处理Cookie.由于我要访问的服务器是Akamai的主动-主动"服务器,因此使用了每个服务器返回的Cookie来保持粘性,因此Web浏览器知道会返回Cookie.
I am getting a 404 Not Found error because my application does not handle cookies like a browser does. Since the server I am accessing is "Active-Active" with Akamai using Cookies returned by each server to maintain stickiness, a web browser knows to return the cookies.
我如何更新我的应用程序以返回发送给我的Cookies,就像浏览器一样?
How do I update my application to return the Cookies sent to me just like a browser does?
我以前使用的是WebClient,但此后切换到HttpWebRequest/Response以下载响应流.这是我到目前为止所拥有的,可用于访问和下载文件.
I was previously using a WebClient but have since switched to HttpWebRequest/Response to download the response stream. This is what I have so far, which works for accessing and downloading the file.
HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(this.url);
webReq.KeepAlive = true;
webReq.Method = "GET";
webReq.ContentType = "text/html";
webReq.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
webReq.Headers.Add("Accept-Language", "en-US,en;q=0.5");
HttpWebResponse webResp = (HttpWebResponse)webReq.GetResponse();
System.IO.Stream rxStream = webResp.GetResponseStream();
Encoding enc = System.Text.Encoding.GetEncoding(1252);
System.IO.StreamReader respStream = new System.IO.StreamReader(rxStream, enc);
string myString = respStream.ReadToEnd();
推荐答案
您可以在HttpWebRequest
和HttpWebResponse
上使用CookieContainer
将从服务器接收的cookie传递给下一个请求,例如浏览器做.
此处.
You can use CookieContainer
on HttpWebRequest
and HttpWebResponse
to pass the cookies that you are receiving from the server to your next requests, like a browser does.
Some documentation and examples are here.
还有一些在WebClient
中使用cookie的方法:在WebClient类中使用CookieContainer
There are also some methods for using cookies with WebClient
: Using CookieContainer with WebClient class
这篇关于发送的HttpWebRequest/Response返回cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!