Apache HttpClient 制作多部分表单帖子
Posted
技术标签:
【中文标题】Apache HttpClient 制作多部分表单帖子【英文标题】:Apache HttpClient making multipart form post 【发布时间】:2011-01-19 06:23:09 【问题描述】:我对 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--
【问题讨论】:
感谢您提出与 Web 应用程序调试直接相关的问题...我在 Firebug 中找到了这个,直到现在还不知道如何编写查询来模拟它! 【参考方案1】:使用来自HttpMime library 的 MultipartEntityBuilder 来执行您想要的请求。
在我的项目中,我是这样做的:
HttpEntity entity = MultipartEntityBuilder
.create()
.addTextBody("number", "5555555555")
.addTextBody("clip", "rickroll")
.addBinaryBody("upload_file", new File(filePath), ContentType.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)
【讨论】:
MultipartEntity 现在显示为已弃用。我正在使用 apache httpclient 4.3.3 - 有人知道我们应该使用什么吗?我发现谷歌搜索充满了 MultipartEntity 示例,我什么也找不到。 使用 MultipartEntityBuilder。简短示例: HttpEntity entity = MultipartEntityBuilder.create().addTextBody("field1", "value1").addBinaryBody("myfile", new File("/path/file1.txt"), ContentType.create("application/octet) -stream"), "file1.txt").build(); 请扩展您的答案以包括如何设置httpClient
我用的是同样的例子,但它的抛出
我使用相同的示例,但它总是抛出此异常 java.net.SocketException: Broken pipe (Write failed)【参考方案2】:
MultipartEntity 现在显示为已弃用。我正在使用阿帕奇 httpclient 4.3.3 - 有谁知道我们应该使用什么 反而?我发现谷歌搜索充满了 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
【讨论】:
【参考方案3】:如果使用 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。 对我不起作用,无法解析 MultiThreadedHttpConnectionManager以上是关于Apache HttpClient 制作多部分表单帖子的主要内容,如果未能解决你的问题,请参考以下文章
Java 9 HttpClient 发送多部分/表单数据请求
如何在 Jetty HttpClient 中进行多部分/表单数据发布