0

I'm trying to login to website and download some pages as it see logged user in C#. I have class, where are functions to send POST to login page to login. I have verified, that I actually log in, but data's are not keeped, so when I download HTML of the page I need, I get page saying to log in. I found this, but I don't know how to implement it. I have class with two funtions: Login() used to login to website, based on this example:

using(WebClient client = new WebClient())
{
    System.Collections.Specialized.NameValueCollection reqparm = new System.Collections.Specialized.NameValueCollection();
    reqparm.Add("param1", "<any> kinds & of = ? strings");
    reqparm.Add("param2", "escaping is already handled");
    byte[] responsebytes = client.UploadValues("http://localhost", "POST", reqparm);
    string responsebody = Encoding.UTF8.GetString(responsebytes);
}

Then, second function DownloadHtml(string url), contains this:

 using (WebClient client = new WebClient()) {
       client.Encoding = Encoding.UTF8;
       html = client.DownloadString(url);
       return html;
 }

How to save cookis in Login() and use them in DownloadHtml() to see the page as logged user? Or shouldn't I use WebClient? If no, what should I use? Thanks.

Community
  • 1
  • 1

1 Answers1

0

After you receive cookies from your first request to authorization you should saved them (in some variable perhaps) and then manually add them to any subsequent request that you will be make.

Adding cookies to request can be made like this:

using (WebClient client = new WebClient())
{
    client.Headers.Add("Cookie", "AUTH_COOKIE_NAME=" + AUTH_COOKIE_VALUE);
    client.Encoding = Encoding.UTF8;
    html = client.DownloadString(url);
    return html;
}
m1burn
  • 78
  • 7