1

I am trying to submit a form (via WebClient - C# WindowsForms) but I do not get it yet.

My HTTP page sequence is: Username_Form > Password_Form > Download PDF File

<!-- Username_Form.html -->
<form ... action="/AgenciaWeb/autenticar/autenticar.do">
    <input type="text" name="username" />
    <input type="submit" value="Continue" />
</form>

<!-- Password_Form.html -->
<form ... action="/AgenciaWeb/autenticar/validarSenha.do">
    <input type="password" name="password" />
    <input type="submit" value="Login" />
</form>

<!-- Password_Form.html -->
<div>
    <a href="PDF_download.pdf">Download</a>
</div>

So I should fill all forms in the first page to be redirected to second page to also fill all forms and then to get the final page and start to download my PDF File.

The problem is that I am not getting the submit in the first page. I am doing:

private void button1_Click(object sender, EventArgs e)
        {
            var url = "http://test_something.com:8080/AgenciaWeb/autenticar/autenticar.do";

            using (var client = new WebClient())
            {
                var values = new NameValueCollection();
                values["username"] = "user01";

                var response = client.UploadValues(url, "POST", values);

                var responseString = Encoding.Default.GetString(response);

                Console.WriteLine(responseString);
            }

        }

However my 'responseString' is the exactly same page with all forms filled (input type="text" name="username" value="user01") and the submit does not happen, like I can not see or interact with the second page (Password_Form.html).

Is there something wrong that I am missing?

1 Answers1

0

You might want to check the response and see if it's returning a 302 status. If it is then you can move on to the next page, having set the value successfully on the server side. What you are doing here requires you to follow the redirect manually.

groksrc
  • 2,910
  • 1
  • 29
  • 29
  • Thank you @GrokSrc, but I can't get status when I'm using 'WebClient'. Although I've been trying: `var urlSenha = "http://test_something.com:8080/AgenciaWeb/autenticar/validarSenha.do"; var valuesSenha = new NameValueCollection(); valuesSenha["senha"] = senha; var responseSenha = client.UploadValues(urlSenha, "POST", valuesSenha);` inside of my `using (var client = new WebClient())`, but it didn't work as well... – antoniolpfilho Mar 15 '17 at 19:59
  • You might do better with a HttpWebRequest / HttpWebResponse pair: https://msdn.microsoft.com/en-us/library/a3xa6ys0(v=vs.110).aspx You can get the status with the HttpWebResponse.StatusCode. – groksrc Mar 15 '17 at 20:42
  • I've changed to HttpWebResquest/HttpWebResponse and I've got the status '200' ('OK'). I start thinking the problem is about the responsible for redirect/submit to next page, because it's via 'JavaScript' so I think it might be a problem... I don't know if it will work in this way... – antoniolpfilho Mar 16 '17 at 02:23
  • Well if it works in a browser you can definitely reproduce the calls with the WebClient. Have you tried looking at the raw http calls made in a browser's developer tools? – groksrc Mar 16 '17 at 02:45
  • No, sorry, I don't know what it is... _Ps: When I will read the variable `responseString` I can see the main page with all forms filled, but I won't redirect to next page..._ – antoniolpfilho Mar 16 '17 at 18:03