HttpWebRequest/Response return cookies sent

杀马特。学长 韩版系。学妹 提交于 2019-12-12 04:24:34

问题


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.

How do I update my application to return the Cookies sent to me just like a browser does?

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

回答1:


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.

There are also some methods for using cookies with WebClient: Using CookieContainer with WebClient class



来源:https://stackoverflow.com/questions/17154029/httpwebrequest-response-return-cookies-sent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!