Reuse Connection with HttpWebRequest in C#

12,289

Solution 1

The reason you don't stay logged in is because you don't give the HttpWebRequest a CookieContainer to keep the session id in.

See the following to StackOverflow Q&A's for your possible solution:

C# keep session id over httpwebrequest

Multiple WebRequest in same session

I hope this helped.

Solution 2

Any idea how the authentication is managed on the other end? IE. If it sets a cookie, then you need to make sure you account for that, see this page, and specifically this note:

Note

For security reasons, cookies are disabled by default. If you want to use cookies, use the CookieContainer property to enable cookies.

Share:
12,289
Momo
Author by

Momo

I work as a software development manager, working mostly with C#, Azure and SQL Server.

Updated on July 29, 2022

Comments

  • Momo
    Momo over 1 year

    I need to make a POST request using .Net.

    I can authenticate by GET, and so I’m trying to make a POST request on the same connection to keep my authentication.

    The problem is I get a 401 Not Authenticated exception, which implies the connection hasn’t been reused.

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("my-server");
    request.Credentials = new NetworkCredential("user", "password");
    
    request.GetResponse().Close();  // Works fine
    
    // Now the request I want to make...
    
    request = (HttpWebRequest)WebRequest.Create("my-server");
    request.Credentials = new NetworkCredential("user", "password");
    
    request.Method = "post";
    
    string postData = "param1=1&param2=2";
    byte[] data = new ASCIIEncoding().GetBytes(postData);
    request.ContentLength = data.Length;
    request.ContentType = "application/x-www-form-urlencoded";
    
    using (Stream stream = request.GetRequestStream())
    {
        stream.Write(data, 0, data.Length);
        stream.Close();
        request.GetResponse().Close();      // This line gets a 401 Not Authorized error.
    }
    

    EDIT: There has been some suggestions that I need to transfer cookies. The following doesn’t work either:

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("my-server");
    request.Credentials = new NetworkCredential("user", "password");
    
    var response = (HttpWebResponse)request.GetResponse();
    
    var cookieContainer = new CookieContainer();
    foreach (Cookie cookie in response.Cookies)
    {
        cookieContainer.Add(cookie);
    }
    
    response.Close();
    
    // Now the request I want to make...
    
    request = (HttpWebRequest)WebRequest.Create("my-server");
    request.Credentials = new NetworkCredential("user", "password");
    request.CookieContainer = cookieContainer;
    
    request.Method = "post";
    
    string postData = "param1=1&param2=2";
    byte[] data = new ASCIIEncoding().GetBytes(postData);
    request.ContentLength = data.Length;
    request.ContentType = "application/x-www-form-urlencoded";
    
    using (Stream stream = request.GetRequestStream())
    {
        stream.Write(data, 0, data.Length);
        request.GetResponse().Close();      // This line gets a 401 Not Authorized error.
    }