问题描述
类似于已经存在的问题Apache HttpClient 制作多部分表单发布
想要生成一个 http 请求,保存一个文件和一个 key=val 对.
want to produce a http request, holding a file and a key=val pair.
目前代码如下:
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();
这只是作为实体应用于 HttpPost 对象并传递给客户端.
This is simply applied to HttpPost object as entity and passed to the client.
发送到运行 lighthttp 服务的 linux 类型设备(黑盒).问题是,当我发送这个请求时,我没有看到来自设备的响应(物理的,并且 HttpEntity 总是返回默认的 200 OK).
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).
通过 Wireshark,我注意到了两个不同之处,我非常希望能得到一些帮助:1. multipart 元素有一个额外的标头(与原始请求相比) - Content-transfer-encoding,我认为这可能是失败的原因.2. 内容长度差异很大.
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?
推荐答案
找到了失败的地方.需要这个补充:
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"))
这篇关于Apache HttpClient.如何使用编码正确生成多部分请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!