How to submit a post request to a form?

别来无恙 提交于 2020-01-05 04:27:13

问题


Here is the test form

Let's say i need to submit first name x and last name y . So , I can submit the get request simply by entering the following url

http://www.w3schools.com/tags/demo_form_method.asp?fname=x&lname=y

Now, if I change method="post" then the above method does not work. How can I submit the post request programatically and then print the resulting page to console ?


I tried to use many methods . For example

PostMethod post = new PostMethod("http://www.w3schools.com/tags/demo_form_method.asp");
        NameValuePair[] data = {
          new NameValuePair("fname", "x"),
          new NameValuePair("lname", "y")
        };
        post.setRequestBody(data);

        ...
        InputStream in = post.getResponseBodyAsStream();

回答1:


I could see that your test form that doesn't support "post" method. I pasted the form snippet below

    <form action="demo_form_method.asp" **method="get"** target="_blank">

There is a lot of help on the Internet on how does form works for post method and how can one send the "post" requests through java.

Some random references below :

http://www.w3.org/TR/html401/interact/forms.html#h-17.13.1

http://alien.dowling.edu/~vassil/tutorials/javapost.php

HTH



来源:https://stackoverflow.com/questions/25466182/how-to-submit-a-post-request-to-a-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!