问题
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