0

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();
Cœur
  • 37,241
  • 25
  • 195
  • 267
Ron
  • 277
  • 1
  • 2
  • 13
  • I don't quite understand your question, how do you want the cookies to be returned to you? If there are any cookies sent by the server, there will be a Set-Cookie header in the response. – argaz Jun 17 '13 at 18:36
  • Well I talked to the web master of the server that I am getting the document from and all they said was: "Update their application to return the Cookies sent to them just like a browser does." So, I am just confused about how to that using c# httpwebrequest – Ron Jun 17 '13 at 19:02

1 Answers1

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

Community
  • 1
  • 1
argaz
  • 1,458
  • 10
  • 15