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

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

问题描述

我有一小段代码,它在同一个 POST 中提交一个 XML 实体和二进制数据.为此,我使用了 httpclienthttpmime.

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.

这个的正确用法是什么?

What's the correct usage for this?

    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天全站免登陆