locked
how to use cookies in httpwebrequest?

    Question

  • CookieContainer cookieContainer = new CookieContainer();
                httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
                httpWebRequest.Accept = "*/*";
                httpWebRequest.KeepAlive = true;
                httpWebRequest.CookieContainer = cookieContainer;
                httpWebRequest.UserAgent =
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
                httpWebRequest.Method = "GET";
                httpWebRequest.Headers.Add(HttpRequestHeader.Cookie,"test=123");
                webResponse = (HttpWebResponse)httpWebRequest.GetResponse();

      after the execution of the red line I checked httpWebRequest.Headers ,"cookie: test=123" was in the collection.
      but as soon as the next line(green one) executed, the item "cookie: test=123" was no longer there.
    why is this happening? Did I use "
    httpWebRequest.Headers.Add(HttpRequestHeader.Cookie,"test=123")" right?



    another question:
    How should I use the cookies obtained from the login of a website to do further requests?
    I found many different usages on the internet. I wonder what's the best way or the standard way to use it?

    Thanks!
    Will Shun

    Thursday, May 29, 2008 6:41 PM

Answers

  • Don't use both CookieContainer and the Headers.Add, use one or the other preferably the CookieContainer as that will allow you to easily grab the cookies which comes back from the requested Url.  After the GetResponse call the cookieContainer will contain the cookies sent back from the requested Url.  You can then use that CookieContainer object to send on subsequent requests.
    • Marked as answer by Will Shun Saturday, May 31, 2008 5:03 AM
    Thursday, May 29, 2008 7:23 PM

All replies

  • I just found out it's the blue background line makes the item "cookie: test=123" disappear in the header collection.

    but these questions still exists:
       I trapped the http request package with some external tools and I couldn't find the "cookie: test=123" field when I am using CookieContainer. why?

    How should I use the cookies obtained from the login of a website to do further requests?
    I found many different usages on the internet. I wonder what's the best way or the standard way to use it?



     Will Shun wrote:
    CookieContainer cookieContainer = new CookieContainer();
                httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
                httpWebRequest.Accept = "*/*";
                httpWebRequest.KeepAlive = true;
                httpWebRequest.CookieContainer = cookieContainer;
                httpWebRequest.UserAgent =
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
                httpWebRequest.Method = "GET";
                httpWebRequest.Headers.Add(HttpRequestHeader.Cookie,"test=123");
                webResponse = (HttpWebResponse)httpWebRequest.GetResponse();

      after the execution of the red line I checked httpWebRequest.Headers ,"cookie: test=123" was in the collection.
      but as soon as the next line(green one) executed, the item "cookie: test=123" was no longer there.
    why is this happening? Did I use "
    httpWebRequest.Headers.Add(HttpRequestHeader.Cookie,"test=123")" right?



    another question:
    How should I use the cookies obtained from the login of a website to do further requests?
    I found many different usages on the internet. I wonder what's the best way or the standard way to use it?

    Thanks!
    Will Shun

    Thursday, May 29, 2008 7:02 PM
  • Don't use both CookieContainer and the Headers.Add, use one or the other preferably the CookieContainer as that will allow you to easily grab the cookies which comes back from the requested Url.  After the GetResponse call the cookieContainer will contain the cookies sent back from the requested Url.  You can then use that CookieContainer object to send on subsequent requests.
    • Marked as answer by Will Shun Saturday, May 31, 2008 5:03 AM
    Thursday, May 29, 2008 7:23 PM
  • inetscan said:

    Don't use both CookieContainer and the Headers.Add, use one or the other preferably the CookieContainer as that will allow you to easily grab the cookies which comes back from the requested Url.  After the GetResponse call the cookieContainer will contain the cookies sent back from the requested Url.  You can then use that CookieContainer object to send on subsequent requests.



    Thanks for your reply.
    But the problem is that when I make another request with the returned CookieContainer, the remote website doesn't response in the way it suppose to be. Then I trapped the request to find no "Cookie" field was in the request package...
    Is there any extra thing that I need to do before send out the request?

    When I am using cookie in the subsequent requests, Should I send all the items in the CookieContainer or just the needed ones?
    What if I add some redundant items? I am little confused about it and I just want to make it clear.
    Friday, May 30, 2008 9:33 AM
  • The easiest way to diagnose the problem is to use a traffic monitor like Fiddler to intercept the data when browsing through your choice of browser.  Then it's a matter of matching the requests that are sent from the browser in code.  Guaranteed right now the requests from code arn't the same which is causing problems.
    Friday, May 30, 2008 7:34 PM