我应该为MultipartEntity设置Content-Type标头吗? [英] Should I set the Content-Type header for a MultipartEntity?
问题描述
我有一小段代码,它在同一POST中提交XML实体以及二进制数据.我为此使用 httpclient 和 httpmime .
I've got a small piece of code which submits an XML entity along with binary data in the same POST. I'm using the httpclient and httpmime for this.
我不太确定是否应为此请求设置Content-Type
标头.毕竟Content-Type
既是application/xml
又是application/octet-stream
.
I'm not quite sure if I should set the Content-Type
header for this request. After all, the Content-Type
is both application/xml
and application/octet-stream
.
此的正确用法是什么?
post = new HttpPost(uri);
post.setHeader("Authorization", auth);
// Should I set Content-Type at all?
post.setHeader("Content-Type", mimeType + ";charset=UTF-8");
MultipartEntityBuilder b = MultipartEntityBuilder.create();
b.addTextBody("data", payload, ContentType.APPLICATION_XML);
b.addBinaryBody("file", file);
post.setEntity(b.build());
推荐答案
否,您不应该.您应该让HttpClient根据消息实体的属性自动生成Content-Type
以及其他内容元数据标头.
No, you should not. You should let HttpClient generate Content-Type
as well as other content metadata headers automatically based on properties of the message entity.
这篇关于我应该为MultipartEntity设置Content-Type标头吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!