【问题标题】:Passing data to an html POST form using Java使用 Java 将数据传递到 html POST 表单
【发布时间】:2012-07-14 21:27:50
【问题描述】:

我有一个看起来像这样的 HTML 表单:

<form name="form1" method="post" action="/confirm.asp">
    <input type="text" name="data1" size="20" value=""><br>
    <input type="text" name="data2" size="20" value=""><br>
    <input type="Submit" name=submit value="Submit">
</form>

我想使用Java 将数据传递给data1data2,并在提交表单时读取随后的页面。由于这是一个method=post,我不能使用http://somesite.com/confirm.asp?data1=foo&amp;data2=foo

有人可以帮忙吗?

【问题讨论】:

  • 看到这个答案:stackoverflow.com/a/2793153/851273
  • 这对于我正在寻找的东西来说太笼统了。
  • 您确定要使用 Java 而不是 Javascript 吗?
  • 是的,我想用 Java 来做。
  • 使用查询参数触发 HTTP POST 请求部分下,它为您提供示例代码并告诉您如何处理“data1”和“data2”。

标签: java


【解决方案1】:
/* create a new URL and open a connection */
URL url = new URL("http://somesite.com/confirm.asp");
URLConnection con = url.openConnection();
con.setDoOutput(true);

/* wrapper the output stream of the connection with PrintWiter so that we can write plain text to the stream */
PrintWriter wr = new PrintWriter(con.getOutputStream(), true);

/* set up the parameters into a string and send it via the output stream */
StringBuilder parameters = new StringBuilder();
parameters.append("data1=" + URLEncoder.encode("value1", "UTF-8"));
parameters.append("&");
parameters.append("data2=" + URLEncoder.encode("value2", "UTF-8"));
wr.println(parameters);
wr.close();

/* wrapper the input stream of the connection with BufferedReader to read plain text, and print the response to the console */
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line;
while((line = br.readLine()) != null) System.out.println(line);
br.close();

【讨论】:

  • 我是 Java 新手。你能解释一下代码吗?我必须改变什么? value1value2 到我要传递的数据?然后是line表单提交后页面的html源?
  • 参数不会是“data1=value1data2=value2”吗?
  • 我使用了URLEncoder,以防使用空格或+等特殊字符。
  • 不,我的意思是两对之间没有任何区别,(没有“&”)
  • Eng.Fouad,先生,您能回答我的问题吗?我想这就是我正在寻找的东西!
【解决方案2】:

这是来自Link 的代码。希望这对你有帮助:)

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpPostForm
{
  public static void main(String[] args)
  {
    try
    {
      URL url = new URL( "http://www.aaaa.com/xyz.asp" );

      HttpURLConnection hConnection = (HttpURLConnection)
                             url.openConnection();
      HttpURLConnection.setFollowRedirects( true );

      hConnection.setDoOutput( true );
      hConnection.setRequestMethod("POST"); 

      PrintStream ps = new PrintStream( hConnection.getOutputStream() );
      ps.print("param1=abcd&amp;param2=10341");
      ps.close();

      hConnection.connect();

      if( HttpURLConnection.HTTP_OK == hConnection.getResponseCode() )
      {
        InputStream is = hConnection.getInputStream();
        OutputStream os = new FileOutputStream("output.html");
        int data;
        while((data=is.read()) != -1)
        {
          os.write(data);
        }
        is.close();
        os.close();
        hConnection.disconnect();
      }
    }
    catch(Exception ex)
    {
      ex.printStackTrace();
    }
  }
}

【讨论】:

    【解决方案3】:

    要在 Java 中编写 POST 请求,您应该通过 URLConnection 连接到目标 URL,然后在其中写入头部边界字节、边界消息(键、值和任何其他请求数据所在的位置) , 和结束边界。

    我为我的一个应用程序编写了一个 PostProcess 类,它允许异步 POST 请求上传、键值参数、文件参数(即表单中的文件上传输入)和上传进度跟踪。它还记录服务器响应。

    为了篇幅和可读性,我已将代码外部上传到http://textu.be/T

    【讨论】:

      【解决方案4】:

      试试下面的代码

      String url="www.somesite.com";     
      Document doc = Jsoup.connect(url).data("data1", "foo").data("data2","foo")
                      .userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6")
                      .referrer("http://www.google.com").post();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-07-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-12
        • 2012-11-02
        相关资源
        最近更新 更多