繁体   English   中英

使用Java将数据传递到html POST表单

[英]Passing data to an html POST form using Java

我有一个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并在提交表单时读取data2的页面。 由于这是一个方法=帖子,我不能使用http://somesite.com/confirm.asp?data1=foo&data2=foo

可以帮忙吗?

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

这是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();
    }
  }
}

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

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

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

请尝试以下代码

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM