我需要创建包含字段的多部分 POST 请求:
update[image_title] = String
update[image] = image-data itself
. 如您所见,两者都在称为“更新”的关联数组中。我怎么能用 HTTPClient 4.1 做到这一点,因为我只找到了这个库的 3.x 行的示例。
先感谢您。
我需要创建包含字段的多部分 POST 请求:
update[image_title] = String
update[image] = image-data itself
. 如您所见,两者都在称为“更新”的关联数组中。我怎么能用 HTTPClient 4.1 做到这一点,因为我只找到了这个库的 3.x 行的示例。
先感谢您。
可能为时已晚,但可能会帮助某人。我有完全相同的问题。假设您有一个文件对象,其中包含有关图像的必要信息
HttpPost post = new HttpPost(YOUR_URL);
MultipartEntity entity = new MultipartEntity();
ByteArrayBody body = new ByteArrayBody(file.getData(), file.getName());
String imageTitle = new StringBody(file.getName());
entity.addPart("imageTitle", imageTitle);
entity.addPart("image", body);
post.setEntity(entity);
HttpClient client = new DefaultHttpClient();
HttpResponse response = null;
try {
response = client.execute(post);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
请注意,这MultiPartEntity
是HttpMime
模块的一部分。因此,您需要将该 jar 放在 lib 目录中或包含为(maven/gradle)依赖项。
是的,我发现找到 HTTP Client 4 示例等真的很痛苦,因为全能的谷歌几乎总是指向 HTTP 3。
无论如何,此页面上的最后一个示例 - http://hc.apache.org/httpcomponents-client-ga/examples.html应该是您想要的。