Java中的HTTP POST请求(带文件上传)

15

我想做的是在Java应用程序中提交Web表单。 我需要填写的表单位于此处:http://cando-dna-origami.org/

当提交表单时,服务器会向给定的电子邮件地址发送确认电子邮件,目前我只是手动检查。 我已经尝试手动填写表单,电子邮件正常发送。(应该注意的是,当表单填写不正确时,页面只会刷新而不提供任何反馈)。

我以前从未使用过http,但是我找了一段时间,并编写了以下代码,它应该将POST请求发送到服务器:

    String data = "name=M+V&affiliation=Company&email="
            + URLEncoder.encode("m.v@gmail.com", "UTF-8")
            + "&axialRise=0.34&helixDiameter=2.25&axialStiffness=1100&bendingStiffness=230" +
            "&torsionalStiffness=460&nickStiffness=0.01&resolution=course&jsonUpload="
            + URLEncoder.encode("C:/Users/Marjie/Downloads/twisted_DNA_bundles/monotwist.L1.v1.json",
            "UTF-8") + "&type=square";

    URL page = new URL("http://cando-dna-origami.org/");
    HttpURLConnection con = (HttpURLConnection) page.openConnection();

    con.setDoOutput(true);
    con.setRequestMethod("POST");
    con.connect();

    OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());
    out.write(data);
    out.flush();

    System.out.println(con.getResponseCode());
    System.out.println(con.getResponseMessage());

    out.close();
    con.disconnect();

然而,当它运行时,似乎什么都没有发生——也就是说,我没有收到任何电子邮件,尽管该程序确实会将“200 OK”打印到System.out中,这似乎表明从服务器接收到了一些数据,虽然我不确定它具体意味着什么。我认为问题可能出在文件上传上,因为我不确定该数据类型是否需要不同的格式。

这是否是使用Java发送POST请求的正确方法?对于文件上传,我需要做一些不同的事情吗?谢谢!


阅读了Adam的帖子后,我使用了Apache HttpClient,并编写了以下代码:

    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("type", "square"));
    //... add more parameters

    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, HTTP.UTF_8);

    HttpPost post = new HttpPost("http://cando-dna-origami.org/");
    post.setEntity(entity);

    HttpResponse response = new DefaultHttpClient().execute(post);
    post = new HttpPost("http://cando-dna-origami.org/");

    post.setEntity(new FileEntity(new File("C:/Users/Marjie/Downloads/twisted_DNA_bundles/monotwist.L1.v1.json"), "text/plain; charset=\"UTF-8\""));
    HttpResponse responseTwo = new DefaultHttpClient().execute(post);

不过它似乎还是没有起作用;因为我不确定上传的文件如何与表单相对应,所以我尝试发送两个独立的POST请求,一个包含表单,另一个包含其他数据。我仍在寻找一种将它们合并为一个请求的方法;有人知道吗?

5个回答

18

建议您使用类似于 Apache HttpClient 的工具,通过该工具,您可以编写代码来构建POST请求。

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://.../whatever");

List <NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("param1", "value1"));
params.add(new BasicNameValuePair("param2", "value2"));
...

httpost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));

HttpResponse response = httpclient.execute(httppost);

如果您需要在表单中上传文件,您需要使用MultipartEntity

MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("someParam", "someValue");
reqEntity.addPart("someFile", new FileBody("/some/file"));
....

httpost.setEntity(reqEntity);

他们的网站上有一些示例程序,可以到他们的网站查看。 "基于表单的登录"和"多部分编码请求实体"是很好的起点。

另外,值得一试的是测试连接并查看底层网络数据以了解发生了什么。类似Firebug的工具可以让您精确地查看浏览器中正在发生的事情,并且您还可以将HttpClient日志级别调高以查看程序中交换的所有数据。或者,您可以使用像Wireshark或Fiddler这样的工具实时监视您的网络流量。这可能会让您更好地了解您的浏览器正在做什么,而与您的程序正在做什么不同。


谢谢您的建议!我已经下载了Apache HttpClient并使用它编写了一段不同的代码;不幸的是,它似乎仍然没有做任何事情。问题仍然是我不确定如何将表单和文件上传组合在一起。请参见上文。 - Reyan
所以我建议您查看通过HTTP连接传输的基础数据(我的答案的最后一段),因为这将澄清浏览器使用(正常工作)和您的程序(无法正常工作)之间的差异。 - Adam Batkin
现在我明白了。我已经更新了答案,加入了有关添加文件上传的信息。 - Adam Batkin
HttpClientBuilder.create().build(); 是 DefaultHttpClient() 方法的替代,因为 DefaultHttpClient() 方法已经被弃用。 - o k
而MultipartEntity也已经被弃用,所以请使用:MultipartEntityBuilder builder = MultipartEntityBuilder.create(); - o k

4
由于大部分建议的Java HTTP POST请求代码都无法正常工作,所以我决定提供我的完全可行的代码,我相信您会发现对于在未来创建任何基于Java的POST请求非常有用。
此POST请求是multipart类型,允许将文件发送/上传到服务器。
多部分请求由一个主标头和一个分隔符字符串组成,称为边界,以告诉每个部分与其他部分不同(该分隔符在其之前使用"--"(两个破折号)字符串,每个部分都有自己的小标题来告诉它的类型和一些其他元数据。我的任务是使用一些在线服务创建PDF文件,但所有多部分POST示例都无法完成...我需要将HTML文档与其图片,JS和CSS文件打包在ZIP / TAR文件中,将其上传到在线html2pdf转换服务并将结果作为PDF文档返回给我作为服务的响应(流)。
我已经检查了当前的服务,使用以下代码:Htmlpdfapi.com ,但我相信通过小的调整,您将能够在任何其他服务中使用它。
该服务的方法调用看起来像这样: [class instance name].sendPOSTRequest("http://htmlpdfapi.com/api/v1/pdf", "Token 6hr4-AmqZDrFVjAcJGykjYyXfwG1wER4", "/home/user/project/srv/files/example.zip", "result.pdf");
以下是经过验证并且100%工作的代码:
public void sendPOSTRequest(String url, String authData, String attachmentFilePath, String outputFilePathName)
{
    String charset = "UTF-8";
    File binaryFile = new File(attachmentFilePath);
    String boundary = "------------------------" + Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value.
    String CRLF = "\r\n"; // Line separator required by multipart/form-data.
    int    responseCode = 0;

    try 
    {
        //Set POST general headers along with the boundary string (the seperator string of each part)
        URLConnection connection = new URL(url).openConnection();
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
        connection.addRequestProperty("User-Agent", "CheckpaySrv/1.0.0");
        connection.addRequestProperty("Accept", "*/*");
        connection.addRequestProperty("Authentication", authData);

        OutputStream output = connection.getOutputStream();
        PrintWriter writer  = new PrintWriter(new OutputStreamWriter(output, charset), true);

        // Send binary file - part
        // Part header
        writer.append("--" + boundary).append(CRLF);
        writer.append("Content-Disposition: form-data; name=\"file\"; filename=\"" + binaryFile.getName() + "\"").append(CRLF);
        writer.append("Content-Type: application/octet-stream").append(CRLF);// + URLConnection.guessContentTypeFromName(binaryFile.getName())).append(CRLF);
        writer.append(CRLF).flush();

        // File data
        Files.copy(binaryFile.toPath(), output);
        output.flush(); 

        // End of multipart/form-data.
        writer.append(CRLF).append("--" + boundary + "--").flush();

        responseCode = ((HttpURLConnection) connection).getResponseCode();


        if(responseCode !=200) //We operate only on HTTP code 200
            return;

        InputStream Instream = ((HttpURLConnection) connection).getInputStream();

        // Write PDF file 
        BufferedInputStream BISin = new BufferedInputStream(Instream);
        FileOutputStream FOSfile  = new FileOutputStream(outputFilePathName);
        BufferedOutputStream out  = new BufferedOutputStream(FOSfile);

        int i;
        while ((i = BISin.read()) != -1) {
            out.write(i);
        }

        // Cleanup
        out.flush();
        out.close();


    }
    catch(Exception e)
    {
        e.printStackTrace();
    }

}

2

我正在编写一个小型Web服务器,并测试您的请求客户端。我的服务器正在接收以下请求:

User-Agent: Java/1.6.0_20 
Host: localhost:1700 
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2 
Connection: keep-alive 
Content-type: application/x-www-form-urlencoded 
Content-Length: 287 
name=M+V&affiliation=Company&email=m.v%40gmail.com&axialRise=0.34&helixDiameter=2.25&axialStiffness=1100&bendingStiffness=230&torsionalStiffness=460&nickStiffness=0.01&resolution=course&jsonUpload=C%3A%2FUsers%2FMarjie%2FDownloads%2Ftwisted_DNA_bundles%2Fmonotwist.L1.v1.json&type=square

您应该检查发送的POST数据的格式,很可能服务器没有按照您期望的方式处理它。


感谢您的回答和测试代码! “POST数据”是什么意思?您是指表单数据还是客户端和连接数据?很抱歉,我不理解您在代码框中发布的信息的重要性。连接设置是否有问题? - Reyan
我指的是使用表单数据中的“data”变量。该框中的数据表示服务器接收到的标头和内容。 - Alex

2

下面是我成功运用了Apache HttpClient的一个例子。同时,别忘了添加这些依赖项:

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.4.1</version>
    </dependency>


    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpmime</artifactId>
        <version>4.4.1</version>
    </dependency>

代码如下:
HttpClient httpclient = HttpClientBuilder.create().build();         
HttpPost httppost = new HttpPost(DataSources.TORRENT_UPLOAD_URL);

MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            builder.addPart("a_field_name", new FileBody(torrentFile));

HttpEntity entity = builder.build();

httppost.setEntity(entity);

HttpResponse response = httpclient.execute(httppost);

2

您应该明确使用Apache的HTTPClient来完成这项工作!它能使生活更加轻松。下面是一个使用Apache HttpClient上传文件的示例。

byte[] data = outStream.toByteArray()
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://localhost:8080/YourResource");

ByteArrayBody byteArrayBody = new ByteArrayBody(data, "application/json", "some.json");
MultipartEntity multipartEntity = new MultipartEntity();
multipartEntity.addPart("upload", byteArrayBody);
httpPost.setEntity( multipartEntity );

HttpResponse response = client.execute(httpPost);
Reader reader = new InputStreamReader(response.getEntity().getContent());

如果您有进一步的问题,请告诉我。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接