Apache HttpClient制作多部分表单发布

对于HttpClient来说,我很绿,而且我发现缺乏(和明显不正确)的文档让人非常沮丧。 我试图用Apache Http Client实现下面的post(下面列出),但不知道如何实际做到这一点。 我将在下周的文档中埋头,但也许更有经验的HttpClient编码人员可以更快地给我答案。

post:

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-- 

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

希望这会有所帮助。

(更新此帖以使用MultipartEntityBuilder而不是弃用的MultipartEntity,以@mtomy代码为例)

MultipartEntity现在显示为已弃用。 我正在使用Apache httpclient 4.3.3 – 有谁知道我们应该使用什么? 我发现谷歌search是如此充满了MultipartEntity的例子,我找不到任何东西。 – – vextorspace 14年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

如果使用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();