Apache HttpClient.如何使用编码正确生成多部分请求 [英] Apache HttpClient. How to properly produce multipart requests with Encoding

查看:167
本文介绍了Apache HttpClient.如何使用编码正确生成多部分请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类似于已经存在的问题 Apache HttpClient进行多部分表单发布

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

想要产生一个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.多部分元素具有一个附加的标头(与原始请求相比)-内容传输编码,我认为这可能是失败的原因. 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.如何使用编码正确生成多部分请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆