我应该为MultipartEntity设置Content-Type标头吗? [英] Should I set the Content-Type header for a MultipartEntity?

查看:682
本文介绍了我应该为MultipartEntity设置Content-Type标头吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一小段代码,它在同一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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆