0

I'm trying to download a file for which I must be logged on a site. I'm using this solution but this doesn't work. Instead of file I get a downloading page as result of request. On downloading page there two different forms, one for premium membership ( which I need) and one for free downloading with captcha. Maybe this is problem, existence of two submit buttons.

Login method:

            CookieContainer container;

            var request = (HttpWebRequest)WebRequest.Create(loginPageAddress);

            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            var buffer = Encoding.ASCII.GetBytes(loginData.ToString());
            request.ContentLength = buffer.Length;
            var requestStream = request.GetRequestStream();
            requestStream.Write(buffer, 0, buffer.Length);
            requestStream.Close();

            container = request.CookieContainer = new CookieContainer();

            var response = request.GetResponse();
            //response.Close();
            CookieContainer = container;
            return response.GetResponseStream();  //this returns a login page

Usage:

                var loginAddress = url;
                var loginData = new NameValueCollection
                    {
                      { "frm-downloadDialog-loginForm-username", "username" },
                      { "frm-downloadDialog-loginForm-password", "password" }
                    };


                var client = new CookieAwareWebClient();
                client.Login(loginAddress, loginData);

Login page:

<input type="text" name="username" class="text" id="frm-downloadDialog-loginForm-username" required="" data-nette-rules="[{&quot;op&quot;:&quot;:filled&quot;,&quot;msg&quot;:&quot;Username required.&quot;}]" value="">
<input type="password" name="password" class="text" id="frm-downloadDialog-loginForm-password" required="" data-nette-rules="[{&quot;op&quot;:&quot;:filled&quot;,&quot;msg&quot;:&quot;Password required.&quot;}]">
<input type="submit" name="login" class="button" id="frm-downloadDialog-loginForm-login" value="Submit">
Community
  • 1
  • 1
skomi
  • 125
  • 2
  • 13
  • 1
    I would recommend using Fiddler and comparing the request you are creating and sending, with a request that is made when you go to the website and click the submit button. Be sure to also compare the headers of the request. – Jochen van Wylick Feb 03 '15 at 15:59
  • Ok, I find out that request have username, password and some token ( some ID ) which I can't find out what is stands for... Submit button have token value which generate on request. Is a way to find out this token value on request? – skomi Feb 03 '15 at 17:13
  • 1
    No, but I was working on something just like this - and here's what I did in code: First, get a webclient to open up the login page - a simple GET. That will set cookies and stuff you don't know about. After that send the POST request ( with the same cookie container ). I fiddled the request body from an actual post and replaced the stuff that needed to be replaced. Then set the body in RequestStream of that POST request. – Jochen van Wylick Feb 03 '15 at 17:15
  • Ok, I menage to logon via WebBrowser contol and I'm able to download file via WebBrowser. But is there any way to get direct link of file via WebBrowser control and cancel downloading? – skomi Feb 03 '15 at 19:43
  • 1
    Are you saying the download starts automatically after logging in? In that case I would look into HttpWebRequest.AllowAutoRedirect = false. Then you can scrape the download URLs from the page ( HTML Agility pack ) or in case of a redirect - read out the headers of the response. – Jochen van Wylick Feb 03 '15 at 22:02
  • Yes, after logging in I get a link to download, but this isn't direct link. After navigating `WebBrowser` to this link, downloading starts automatically. Is there any way to get direct link and stop downloading via `WebBrowser`? I tried to get link in `WebBrowser Events` `Navigate/Navigating`, but last URL that I get is `about:blank`. – skomi Feb 03 '15 at 23:31

0 Answers0