问题
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.
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());
回答1:
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.
来源:https://stackoverflow.com/questions/43510961/should-i-set-the-content-type-header-for-a-multipartentity