MultipartEntityBuilder: Omit Content-Type and Content-Transfer

你说的曾经没有我的故事 提交于 2020-05-13 02:40:29

问题


I'm trying to mimic my Browsers behaviour on a multipart/form-data POST request using org.apache.http.entity.mime.MultipartEntityBuilder

My browser only sends Content-Disposition, but no Content-Type or Content-Transfer-Encoding Headers. I tried to use MultipartEntityBuilder.addPart() and addTextBody() but both add those Headers by default:

What I want (what my chrome browser does):

POST .../some.jsp HTTP/1.1
Host: ...
Connection: keep-alive
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary6tcnkxC7txvqE5Xl


------WebKitFormBoundary6tcnkxC7txvqE5Xl
Content-Disposition: form-data; name="merkmal"

5

What I get from MultipartEntityBuilder

POST.../some.jsp HTTP/1.1
Host: ...
Content-Type: multipart/form-data; boundary=m9Zb2QD-QaH-j-HqgGQfI8KwDkToz17ULYkZ

--m9Zb2QD-QaH-j-HqgGQfI8KwDkToz17ULYkZ
Content-Disposition: form-data; name="merkmal"
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit

5

Why?: The designated server thinks that everything after name="merkmal" is the value of merkmal (including the Headers). Other possible reason: Could the whole request somehow have a wrong encoding (especially for newline) ?


回答1:


alright - intense googeling finally produced an answer.

1) There are apparently some servers that do not get on well with the "Content-Transfer-Encoding" header.

2) There is a browser compatibility mode in HttpComponents that is used like this:

MultipartEntityBuilder uploadEntityBuilder = MultipartEntityBuilder.create().setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

Using the compatibility code, both unwanted headers (Content-Transfer-Encoding and Content-Type) are not used any longer!

I hope this will some day help some poor sod like me ;)



来源:https://stackoverflow.com/questions/26000086/multipartentitybuilder-omit-content-type-and-content-transfer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!