【问题标题】:How to submit a post request to a form?如何向表单提交发布请求?
【发布时间】:2014-08-23 20:41:05
【问题描述】:

这里是test form

假设我需要提交名字 x 和姓氏 y 。所以,我只需输入以下url 即可提交get 请求

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

现在,如果我更改method="post",则上述方法不起作用。如何以编程方式提交 post 请求,然后将 print 生成的页面提交给 console ?


我尝试了很多方法。例如

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();

【问题讨论】:

  • 上述方法不起作用,因为您必须将参数作为请求正文的内容发送。请提供您用于启动请求的代码,否则这个问题根本不是 Java。
  • 在 POST 中,参数作为请求的 body 发送。
  • 我添加了我的一项尝试。
  • 嗯,看来网址不支持POST请求...

标签: java apache-commons-httpclient http-post


【解决方案1】:

我可以看到您的测试表单不支持“发布”方法。我在下面粘贴了表格sn-p

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

网上有很多关于表单如何用于post方法以及如何通过java发送“post”请求的帮助。

以下一些随机参考:

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

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

HTH

【讨论】:

    猜你喜欢
    • 2010-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-13
    • 2015-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多