1

I need to know how to submit an HTML form and retrieve the data. I used the code from How do you programmatically fill in a form and 'POST' a web page?

C#

    WebRequest req = 
    WebRequest.Create("http://localhost:8080/Test/views/user/index.jsp");
    string postData = "email=test&password=test";

    byte[] send = System.Text.Encoding.Default.GetBytes(postData);
    req.Method = "POST";
    req.ContentType = "application/x-www-form-urlencoded";
    req.ContentLength = send.Length;

    Stream sout = req.GetRequestStream ();
    sout.Write (send, 0, send.Length);
    sout.Flush ();
    sout.Close ();

    WebResponse res = req.GetResponse ();
    StreamReader sr = new StreamReader (res.GetResponseStream ());
    Debug.Log (sr.ReadToEnd ());

JSP index.jsp

<form action="/Test/views/user/success.jsp" action="POST">
<input type="text" id="email" name="email"> <br> <input
    type="password" id="password" name="password"> <br> <input
    type="submit" id="submit" name="submit" value="Submit"></form>

success.jsp

<'%

    if(test.equals("test")){
      out.print("true");
    }

%'>

I wish to get that "true" string but the code above only returns the HTML code. How do I retrieve that "true" value using C#? C# does not reside in server side so it is out of question.

Community
  • 1
  • 1
Inacio
  • 127
  • 12
  • Possible duplicate : http://stackoverflow.com/questions/5401501/how-to-post-data-to-specific-url-using-webclient-in-c-sharp –  Jul 10 '16 at 14:18
  • where is servlet / controller? – umeshkumar sohaliya Jul 10 '16 at 15:10
  • I was not clear enough. Servlet and controller is in webserver and C# is the client side. – Inacio Jul 10 '16 at 15:20
  • You're posting to `http://localhost:8080/Test/views/user/index.jsp`, but your form action is `/Test/views/user/success.jsp`. If you want to submit that form programmatically, your WebRequest should post to `http://localhost:8080/Test/views/user/success.jsp`. – Tap Jul 11 '16 at 12:47
  • @Tap So I can post my form to success.jsp huh? Thanks alot! Oh and I assume same thing applies to mapped URLs right? like localhost:8080/Test/login.do and write C# form POST submit code? – Inacio Jul 12 '16 at 07:28
  • 1
    @Inacio, yes, of course. You can post to any url that has a resource listening. – Tap Jul 12 '16 at 12:28

0 Answers0