使用org.apache.http.client post信息时中文乱码的解决办法

在使用Apache HttpClient向服务器发送包含中文信息时遇到乱码问题,原因是默认的ContentType编码为ISO_8859_1。解决方法是在创建StringBody时指定UTF-8编码,例如:`mb.addPart(name new StringBody(张三 ContentType.TEXT_PLAIN.withCharset(UTF-8)));`其他可行的方法包括使用ContentType.create()创建自定义编码的实例。

最近使用apache的httpclient向服务器发信息,发现收到的中文是乱码。我的代码类似下面:

MultipartEntityBuilder mb = MultipartEntityBuilder.create();
mb.setCharset(Charset.forName("UTF-8"));

// 设置信息
mb.addPart("name", new StringBody("张三", ContentType.TEXT_PLAIN));
mb.addPart("addr", new StringBody("XXX市XX区", ContentType.TEXT_PLAIN));

((HttpPost) request).setEntity(mb.build());
// 发起请求
response = client.execute(request);

通过查询资料,发现是ContentType.TEXT_PLAIN的编码问题。虽然在前面已经用setCharset(Charset.forName("UTF-8"))设置了编码。但在设置信息时,还需要注意ContentType.TEXT_PLAIN的编码问题。

从源码中可以看到常见的几种ContentType类型:

public static final ContentType APPLICATION_FORM_URLENCODED = create("application/x-www-form-urlencoded", Consts.ISO_8859_1);
public static final ContentType APPLICATION_JSON = create("application/json", Consts.UTF_8);
public static final ContentType APPLICATION_XML = create("application/xml", Consts.ISO_8859_1);
public static final ContentType MULTIPART_FORM_DATA = create("multipart/form-data", Consts.ISO_8859_1);
public static final ContentType TEXT_HTML = create("text/html", Consts.ISO_8859_1);
public static final ContentType TEXT_PLAIN = create("text/plain", Consts.ISO_8859_1);
public static final ContentType TEXT_XML = create("text/xml", Consts.ISO_8859_1);

从上面代码中可以看出,其实这些文本大多数都使用ISO_8859_1编码,这里也包括了我的代码中的TEXT_PLAIN类型。这样就导致了中文编码错误。

解决方法有很多,都是从编码角度来解决,创建一个utf-8编码的ContentType。比如可以写成下面的形式:

// 方法1(我用的方法)
mb.addPart("name", new StringBody("张三", ContentType.TEXT_PLAIN.withCharset("UTF-8")));
// 方法2(未测试)
mb.addPart("name", new StringBody("张三", ContentType.create("text/plain", MIME.UTF8_CHARSET)));
// 方法3(未测试)
mb.addPart("name", new StringBody("张三", ContentType.create("text/plain", Charset.forName("UTF-8")));
// 方法4(未测试)
mb.addPart("name", new StringBody("张三", ContentType.create(HTTP.PLAIN_TEXT_TYPE, HTTP.UTF_8);

 

POST http://221.178.78.110:29001/bit-id/3d-digital/avatar-api/v1/knowledge/file/upload POST data: --PnFqeU3sN_Qa_jtvSNrLg--79SC1AWwg9 Content-Disposition: form-data; name="knowledgeId" Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1498870953575186442 --PnFqeU3sN_Qa_jtvSNrLg--79SC1AWwg9 Content-Disposition: form-data; name="srcFile"; filename="?????????? ???????? ?????????????.docx" Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document Content-Transfer-Encoding: binary <actual file content, not shown here> --PnFqeU3sN_Qa_jtvSNrLg--79SC1AWwg9-- Cookie Data: Admin-Mobile=18123669786; Admin-Token=Bearer%20eyJhbGciOiJIUzI1NiJ9.eyJ0eXBlIjoid2ViIiwiYWNjb3VudCI6IjE4MTIzNjY5Nzg2Iiwic3ViIjoiMTAwMDE4IiwiaXNzIjoiYXZhdGFyLXVzZXItc2VydmVyIiwiaWF0IjoxNzYxNTM0NTUyLCJleHAiOjE3NjIxMzkzNTIsIm5iZiI6MTc2MTUzNDU1Mn0.EmKE-RIXeXX2EN7bDcPlZLSElqK4c7NHd-39thNqoow日志java.io.FileNotFoundException: C:\Users\EDY\Desktop\shuzhirenxiangmu\wodezhishiku\jituanxinxi\jituanxinxi\���������� �ұ���Ӧ�� �й��ƶ�֣�س�ŵ��.docx (系统找不到指定的路径。) at java.base/java.io.FileInputStream.open0(Native Method) at java.base/java.io.FileInputStream.open(FileInputStream.java:216) at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157) at org.apache.http.entity.mime.content.FileBody.writeTo(FileBody.java:116) at org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl$ViewableFileBody.writeTo(HTTPHC4Impl.java:1513) at org.apache.http.entity.mime.AbstractMultipartForm.doWriteTo(AbstractMultipartForm.java:134) at org.apache.http.entity.mime.AbstractMultipartForm.writeTo(AbstractMultipartForm.java:157) at org.apache.http.entity.mime.MultipartFormEntity.writeTo(MultipartFormEntity.java:113) at org.apache.http.impl.DefaultBHttpClientConnection.sendRequestEntity(DefaultBHttpClientConnection.java:156) at org.apache.http.impl.conn.CPoolProxy.sendRequestEntity(CPoolProxy.java:152) at org.apache.http.protocol.HttpRequestExecutor.doSendRequest(HttpRequestExecutor.java:238) at org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl$2.doSendRequest(HTTPHC4Impl.java:458) at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:123) at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:272) at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:186) at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89) at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110) at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185) at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83) at org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl.executeRequest(HTTPHC4Impl.java:935) at org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl.sample(HTTPHC4Impl.java:646) at org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy.sample(HTTPSamplerProxy.java:66) at org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase.sample(HTTPSamplerBase.java:1296) at org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase.sample(HTTPSamplerBase.java:1285) at org.apache.jmeter.threads.JMeterThread.doSampling(JMeterThread.java:638) at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:558) at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:489) at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:256) at java.base/java.lang.Thread.run(Thread.java:833)
10-28
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值