28

I am creating an application for data retrieval from the web page. The page is password protected and when the user logs in the cookie is created.

In order to retrieve the data the application first has to log in: make web request with username and password and store the cookie. Then when the cookie is stored it has to be added into the headers of all requests.

Here is the method which makes requests and retrieves the content:

public void getAsyncDailyPDPContextActivationDeactivation()
{
    HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(dailyPDPContextActivationDeactivation);

    IAsyncResult asyncResult = httpWebRequest.BeginGetResponse(null, null);

    asyncResult.AsyncWaitHandle.WaitOne();

    using (HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.EndGetResponse(asyncResult))
    using (StreamReader responseStreamReader = new StreamReader(httpWebResponse.GetResponseStream()))
    {
        string responseText = responseStreamReader.ReadToEnd();
    }
}

Does anyone know how to modify this method in order to add a cookie into the header?

I would be also thankful if anyone suggested a way to store cookie from the response (when the application makes a request http:xxx.xxx.xxx/login?username=xxx&password=xxx the cookie is created and has to be stored for future requests).

adrianbanks
  • 81,306
  • 22
  • 176
  • 206
Niko Gamulin
  • 66,025
  • 95
  • 221
  • 286

2 Answers2

41
CookieContainer cookieContainer = new CookieContainer();
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(...);
httpWebRequest.CookieContainer = cookieContainer;

Then you reuse this CookieContainer in subsequent requests:

HttpWebRequest httpWebRequest2 = (HttpWebRequest)WebRequest.Create(...);
httpWebRequest2.CookieContainer = cookieContainer;
k_b
  • 2,470
  • 18
  • 9
  • 2
    I don't get it actually. Why would you put a freshly created cookiecontainer(so it's empty) into the request you are about to send? Shouldn't it a be a lot more logical that it you have to fill that cookieContainer later on with the response ? IS there a reason it's this way ? edit: A lot of answers are given this way, but i just can't figure out why it's done this way. – deltu100 Nov 20 '12 at 12:37
  • My application has undesired behavior using that. Using httpWebRequest2.Headers.Set("Cookie",... works fine. – Eduardo Dec 27 '12 at 21:46
  • If I've logged onto the webpage previously, there is a cookie stored on my HD so I can skip logging in programatically. Does assigning the cookie container to the web request automatically search for and populate the contain with found cookies, or do you have to do that manually? How does one do that, if the latter? – Steve H. Apr 01 '13 at 17:28
  • @SteveH. It's not going to resolve from your HD. You could have multiple sessions running simultaneously across different browsers each with their own set of the same cookies but different values. The browser usually handles that. – mirezus Apr 16 '13 at 20:43
  • 6
    @deltu100, the cookieContainer is filled after you call `HttpWebRequest.GetResponse()` if you maintain a reference to the same object in memory and assign it to a subsequent `HttpWebRequest` it will pass the previously retrieved cookies with the request. – Aaron Hoffman Jul 02 '14 at 19:14
  • I found that `CookieContainer` does not automatically update after `HttpWebRequest.GetResponse()` and instead I had to manually call `cookieContainer.Add(response.Cookies)`. – Eliott May 03 '23 at 21:07
0

Use the CookieContainer or you could use a CookieAwareWebClient

Community
  • 1
  • 1
Don
  • 9,511
  • 4
  • 26
  • 25