ihorko
ihorko

Reputation: 6945

How to pass cookies on ASP.NET request

I need to pass all my current page cookies to other server with request:

string url = "http://www.someserver.com/page1.aspx";
        // Create a request for the URL. 
        WebRequest request = WebRequest.Create(url);
        // If required by the server, set the credentials.
        request.Credentials = CredentialCache.DefaultCredentials;
        // Get the response.
        WebResponse response = request.GetResponse();
        // Display the status.
        //Console.WriteLine(((HttpWebResponse)response).StatusDescription);
        // Get the stream containing content returned by the server.
        Stream dataStream = response.GetResponseStream();
        // Open the stream using a StreamReader for easy access.
        StreamReader reader = new StreamReader(dataStream);
        // Read the content.
        string responseFromServer = reader.ReadToEnd();
        // Display the content.
        Console.WriteLine(responseFromServer);
        // Clean up the streams and the response.
        reader.Close();
        response.Close();

What should I add to this code to read current cookies and pass them to http://www.someserver.com/page1.aspx

Thanks

Upvotes: 0

Views: 1256

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

((HttpWebRequest)request).Headers[HttpRequestHeader.Cookie] = 
    Request.Headers[HttpRequestHeader.Cookie.ToString()];

where obviously the Request variable used here is an ASP.NET HttpRequest instance.

Upvotes: 1

Related Questions