制作一个包含 MultipartEntity 的 HttpRequest

发布于 2024-09-08 02:56:28 字数 701 浏览 5 评论 0原文

我一直在尝试弄清楚如何使用其中的多部分实体发出 http 请求,这让我感到沮丧。多部分有一个自定义边界,但我似乎无法设置它。我的下面的代码会生成一条响应消息,指出我的消息不包含多个部分。

HttpPut addDoc = new HttpPut(url);
addDoc.addHeader("Content-Type", "multipart/related; boundary=\"END_OF_PART\"");
String bodyString = "Test for multipart update";
String titleString = "Title Test for multipart update";
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
StringBody title = new StringBody(titleString, "application/atom+xml",Charset.forName("UTF-8"));
StringBody body = new StringBody(bodyString, "text/plain",Charset.forName("UTF-8"));
entity.addPart("title", title);
entity.addPart("body", body);
addDoc.setEntity(entity);

I've been frustrated at trying to figure out how to make an http request with a multipart entity in it. The multipart has a custom boundary but I can't seem to be able to set it. My code below results in a response message of saying that my message does not contain multiple parts.

HttpPut addDoc = new HttpPut(url);
addDoc.addHeader("Content-Type", "multipart/related; boundary=\"END_OF_PART\"");
String bodyString = "Test for multipart update";
String titleString = "Title Test for multipart update";
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
StringBody title = new StringBody(titleString, "application/atom+xml",Charset.forName("UTF-8"));
StringBody body = new StringBody(bodyString, "text/plain",Charset.forName("UTF-8"));
entity.addPart("title", title);
entity.addPart("body", body);
addDoc.setEntity(entity);

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

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

发布评论

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

评论(2

背叛残局 2024-09-15 02:56:28

您可以尝试从代码中删除 addHeader 部分并将构造函数更改为

MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE,null,Charset.forName("UTF-8"));

you can try to remove addHeader part from your code and change your constructor to

MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE,null,Charset.forName("UTF-8"));
老旧海报 2024-09-15 02:56:28

首先,您可以尝试使用:

MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();

这是初始化它的正确方法,然后:

addDoc.setEntity(entity.build());

至于边界,您的问题不清楚是否需要读取自定义边界或设置它,无论如何,自定义边界设置如下方式;

multipartEntity.setBoundary("some-mimetype-boundary-unique-string");

First of all you could try using:

MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();

which is the correct way to initialize it, and then:

addDoc.setEntity(entity.build());

as for the boundary your question is not clear whether you need to read a custom boundary or set it, anyway the custom boundary is set in the following way;

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