82

私は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--
4

3 に答える 3

114

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

これがお役に立てば幸いです。

(例として@mtomyコードを使用して、非推奨のMultipartEntityの代わりにMultipartEntityBuilderを使用するようにこの投稿を更新しました)

于 2010-06-05T17:46:24.043 に答える
19

MultipartEntityは非推奨として表示されるようになりました。私はapachehttpclient4.3.3を使用しています-代わりに何を使用するのか誰か知っていますか?グーグル検索はMultipartEntityの例でいっぱいで、何も見つかりません。–vextorspace2014年3月31日20:36

HttpClient4.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クラスを使用するには、 HttpClientのサブプロジェクトであるhttpmimeが必要です。

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

于 2015-01-22T01:35:46.040 に答える
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();
于 2017-09-06T13:02:14.720 に答える