制作一个包含 MultipartEntity 的 HttpRequest
我一直在尝试弄清楚如何使用其中的多部分实体发出 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试从代码中删除 addHeader 部分并将构造函数更改为
you can try to remove addHeader part from your code and change your constructor to
首先,您可以尝试使用:
这是初始化它的正确方法,然后:
至于边界,您的问题不清楚是否需要读取自定义边界或设置它,无论如何,自定义边界设置如下方式;
First of all you could try using:
which is the correct way to initialize it, and then:
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;