Apache HttpClient. How to properly produce multipart requests with Encoding

浪子不回头ぞ 提交于 2019-12-11 15:57:37

问题


Similar to the already existing question Apache HttpClient making multipart form post

want to produce a http request, holding a file and a key=val pair.

Currently the code looks like:

HttpPost post = new HttpPost("http://localhost/mainform.cgi/auto_config.htm");

HttpEntity ent = MultipartEntityBuilder.create()
        .addTextBody("TYPE", "6",ContentType.TEXT_BINARY)
        .addBinaryBody("upname", new File("factory.cfg"),ContentType.APPLICATION_OCTET_STREAM,"factory.cfg")
        .build();

This is simply applied to HttpPost object as entity and passed to the client.

Which is sent to a linux-type device (blackbox) with a lighthttp service running. The problem is that when I send this request I do not see a response from the device(physical, and the HttpEntity always returns a default 200 OK).

Through Wireshark I've noticed two differences, on which i would really appreciate to get some help: 1. The multipart elements have an additional header(comparing to the original request) - Content-transfer-encoding, which i assume may be the reason of the fail. 2. Content length differs drastically.

So the first question would be - how to deal with the Encoding?


回答1:


Found the point, on which it was failing. Needed this addition:

.addTextBody("TYPE", "6",ContentType.WILDCARD)
.setMode(HttpMultipartMode.BROWSER_COMPATIBLE).setCharset(Charset.forName("UTF-8"))


来源:https://stackoverflow.com/questions/49918204/apache-httpclient-how-to-properly-produce-multipart-requests-with-encoding

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