如何向表单提交帖子请求?
以下是测试表单
假设我需要提交名字 x
和姓氏 y
。所以,我只需输入以下 url
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
现在,如果我更改 method =post
,则上述方法不起作用。如何以编程方式提交 post
请求,然后将 print
生成的页面提交到 console
?
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();
我可以看到你的测试表格没有支持后方法。我粘贴了下面的表格摘要
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">
互联网上有很多关于表单如何用于post方法的帮助,怎么能有一个通过java发送post请求。
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.
下面的一些随机引用:
http://www.w3.org/TR/html401/interact/forms.html #h-17.13.1
http://alien.dowling.edu/~vassil/tutorials/javapost.php
HTH