Apache HttpClient 制作多部分表单帖子

发布于 2024-08-21 23:11:49 字数 773 浏览 5 评论 0原文

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

I'm pretty green to HttpClient and I'm finding the lack of (and or blatantly incorrect) documentation extremely frustrating. I'm trying to implement the following post (listed below) with Apache Http Client, but have no idea how to actually do it. I'm going to bury myself in documentation for the next week, but perhaps more experienced HttpClient coders could get me an answer sooner.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迷迭香的记忆 2024-08-28 23:11:49

使用 HttpMime 库 中的 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();

希望这会有所帮助。

(更新了这篇文章以使用 MultipartEntityBuilder 而不是已弃用的 MultipartEntity,使用 @mtomy 代码作为示例)

Use MultipartEntityBuilder from the HttpMime library to perform the request you want.

In my project I do that this way:

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

Hope this will help.

(Updated this post to use MultipartEntityBuilder instead of deprecated MultipartEntity, using @mtomy code as the example)

晚雾 2024-08-28 23:11:49

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:

< a href="http://hc.apache.org/httpcomponents-client-4.3.x/httpmime/dependency-info.html">http://hc.apache.org/httpcomponents-client-4.3.x/httpmime /dependency-info.html

MultipartEntity now shows up as deprecated. I am using apache
httpclient 4.3.3 - does anyone know what we are supposed to use
instead? I find the google searches to be so full of MultipartEntity
examples I can't find anything. – vextorspace Mar 31 '14 at 20:36

Here is the sample code in 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);

To use the class MultipartEntityBuilder, you need httpmime, which is a sub project of 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

孤蝉 2024-08-28 23:11:49

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

if use org.apache.commons.httpclient.HttpClient package, maybe that can help you!

    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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文