Search code examples
c#cookieshttpwebrequesthttp-status-code-404

HttpWebRequest/Response return cookies sent


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

Solution

  • 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