Apache HttpClient制作多部分表单


77

我对HttpClient非常满意,并且发现缺少(或公然不正确的)文档非常令人沮丧。我正在尝试使用Apache Http Client实现以下文章(在下面列出),但不知道如何实际执行。下周我将把自己埋在文档中,但是也许更有经验的HttpClient编码人员可以早日给我答案。

发布:

Content-Type: multipart/form-data; boundary=---------------------------1294919323195
Content-Length: 502
-----------------------------1294919323195
Content-Disposition: form-data; name="number"

5555555555
-----------------------------1294919323195
Content-Disposition: form-data; name="clip"

rickroll
-----------------------------1294919323195
Content-Disposition: form-data; name="upload_file"; filename=""
Content-Type: application/octet-stream


-----------------------------1294919323195
Content-Disposition: form-data; name="tos"

agree
-----------------------------1294919323195--

3
感谢您提出与Web应用程序调试直接相关的问题...我在Firebug中发现了这一点,直到现在还不知道如何编写查询来模拟它!

Answers:


105

使用HttpMime库中的MultipartEntityBuilder来执行所需的请求。

在我的项目中,我这样做的方式是:

HttpEntity entity = MultipartEntityBuilder
    .create()
    .addTextBody("number", "5555555555")
    .addTextBody("clip", "rickroll")
    .addBinaryBody("upload_file", new File(filePath), ContentType.create("application/octet-stream"), "filename")
    .addTextBody("tos", "agree")
    .build();

HttpPost httpPost = new HttpPost("http://some-web-site");
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);
HttpEntity result = response.getEntity();

希望这会有所帮助。

(已更新本文,以@mtomy代码为例,使用MultipartEntityBuilder而不是不推荐使用的MultipartEntity)


4
现在,MultipartEntity显示为已弃用。我正在使用apache httpclient 4.3.3-有人知道我们应该使用什么吗?我发现Google搜索充满了MultipartEntity示例,我什么也找不到。
vextorspace 2014年

13
使用MultipartEntityBuilder。简短示例:HttpEntity实体= MultipartEntityBuilder.create()。addTextBody(“ field1”,“ value1”)。addBinaryBody(“ myfile”,新File(“ / path / file1.txt”),ContentType.create(“ application / octet -stream“),” file1.txt“)。build();
mtomy 2014年

请扩大您的答案以包括如何设置httpClient
8bitjunkie 18/09/20

我使用的是相同的示例,但
Santosh b,

我正在使用相同的示例,但它始终抛出此异常java.net.SocketException:管道损坏(写入失败)
Santosh b

15

现在,MultipartEntity显示为已弃用。我正在使用apache httpclient 4.3.3-有人知道我们应该使用什么吗?我发现Google搜索充满了MultipartEntity示例,我什么也找不到。– vextorspace 2014年3月31日20:36

这是HttpClient 4.3.x中的示例代码

http://hc.apache.org/httpcomponents-client-4.3.x/httpmime/examples/org/apache/http/examples/entity/mime/ClientMultipartFormPost.java

import org.apache.http.entity.mime.MultipartEntityBuilder;

HttpPost httppost = new HttpPost("http://localhost:8080" +
        "/servlets-examples/servlet/RequestInfoExample");

FileBody bin = new FileBody(new File(args[0]));
StringBody comment = new StringBody("A binary file of some kind", ContentType.TEXT_PLAIN);

HttpEntity reqEntity = MultipartEntityBuilder.create()
        .addPart("bin", bin)
        .addPart("comment", comment)
        .build();


httppost.setEntity(reqEntity);

要使用MultipartEntityBuilder类,您需要httpmime,它是HttpClient的子项目。

HttpClient 4.3.x:

http://hc.apache.org/httpcomponents-client-4.3.x/index.html

httpmime 4.3.x:

http://hc.apache.org/httpcomponents-client-4.3.x/httpmime/dependency-info.html


2

如果使用org.apache.commons.httpclient.HttpClient包,也许可以帮到您!

    HttpConnectionManager httpConnectionManager = new MultiThreadedHttpConnectionManager();
    //here should set HttpConnectionManagerParams but not important for you
    HttpClient httpClient = new HttpClient(httpConnectionManager);

    PostMethod postMethod = new PostMethod("http://localhost/media");

    FilePart filePart = new FilePart("file", new File(filepath));
    StringPart typePart = new StringPart("type", fileContent.getType(), "utf-8");
    StringPart fileNamePart = new StringPart("fileName", fileContent.getFileName(), "utf-8");
    StringPart timestampPart = new StringPart("timestamp", ""+fileContent.getTimestamp(),"utf-8");
    Part[] parts = { typePart, fileNamePart, timestampPart, filePart };

    MultipartRequestEntity multipartRequestEntity = new MultipartRequestEntity(parts, postMethod.getParams());
    postMethod.setRequestEntity(multipartRequestEntity);
    httpClient.executeMethod(postMethod);
    String responseStr = postMethod.getResponseBodyAsString();

它适用于HttpClient 3.x,不适用于4.x。
M2E67 '17

不适用于我,无法解决MultiThreadedHttpConnectionManager
8bitjunkie
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.