2

I let my program (c#) log in to website, i get correct buffered cookie information. Yet, i get a 401, session timed out, when i want to retrieve the correct data behind my login.

So i figured, the website must not be able to retrieve that cookie info. Can't figure out how to store it so that the website can retrieve it.

        WebRequest req = WebRequest.Create(Url);
        req.ContentType = "application/x-www-form-urlencoded";
        req.Method = "POST";
        byte[] bytes = Encoding.ASCII.GetBytes(Gegevens);
        req.ContentLength = bytes.Length;
        using (Stream os = req.GetRequestStream())
        {
        os.Write(bytes, 0, bytes.Length);
        }
        WebResponse resp = req.GetResponse();
        cookieHeader = resp.Headers["Set-cookie"];

cookieHeader, contains the correct info. Thanks in advance.

  • are you asking how to add a cookie or why you're not getting it? Sorry for the mixup – Eric Stallcup Nov 27 '12 at 14:39
  • If i need to information from cookieHeader, stored in a .txt locally for it to work, since i got it now there, and it doesn't seem working, or is there more too it? – Carl Van Elsen Nov 27 '12 at 14:44
  • Are you getting the 401 error from the code you posted, or is it coming from another `WebRequest` somewhere else in your code? – Ichabod Clay Nov 27 '12 at 14:45
  • from another webrequest, i let my program log in to the website, i get a succes message, but when i try and get the next page, it gives me 401 – Carl Van Elsen Nov 27 '12 at 14:48
  • Are you trying to login to the website using web request? This question will help you? http://stackoverflow.com/questions/450380/login-to-the-page-with-httpwebrequest ... In the same answer you will also see how to retrieve the cookie.. – Amitd Nov 27 '12 at 14:48

2 Answers2

3

You need to assign a CookieContainer to your web request and use this very same CookieContainer for the following requests, too.

See MSDN for reference.

You could (if you want to persist the cookies upon closing your application) get the list of Cookies from the CookieContainer and serialize these. Upon opening the application you could deserialize and rebuild the CookieContainer.

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
1

From the comment you provided, I'm going to hazard a guess and say you aren't properly adding your login cookies to your next WebRequest. Cookie handling with a WebRequest object is a bit difficult, so I recommend using HttpWebRequest and HttpWebResponse which has built-in cookie parsing. You only have to change a couple lines of code here and there:

Building a request (using the same example in your question)

CookieContainer cookies = new CookieContainer();

// When using HttpWebRequest or HttpWebResponse, you need to cast for it to work properly
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.CookieContainer = cookies;
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";

byte[] bytes = Encoding.ASCII.GetBytes(Gegevens);
req.ContentLength = bytes.Length;
using (Stream os = req.GetRequestStream())
{
    os.Write(bytes, 0, bytes.Length);
}

// Cast here as well
using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
{
    // Code related to the web response goes in here
}

Right now, your cookie information is saved in the CookieContainer object. It can be reused later in your code to validate your login. You don't need to write this cookie information to the disk if you don't need to.

Building a request with cookie information (Pretty much the same as above, but you're not adding POST data and you're using a GET request)

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.CookieContainer = cookies; // This is where you add your login cookies to the current request
req.Method = "GET";

using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
{
    // Code related to the web response goes here
}

Hopefully this will get you on the right track :)

Ichabod Clay
  • 1,981
  • 13
  • 17