HttpClient解决中文乱码

[size=medium]最近项目用到httpclient.代理游戏充值功能,在本系统充值完毕后,划拨到对方游戏。需要对方提供http服务,本系统充值成功调用。
传递参数和接收内容中涉及到中文,个人解决方法如下:[/size]
[size=medium]参数传递:[/size]

//创建请求参数
private HttpUriRequest createRequest(String method, String urlStr, Map<String, String> params){
try {
if (method.equalsIgnoreCase("get")) {
URI uri = makeURI(urlStr, params);
HttpGet httpGet = new HttpGet(uri);
return httpGet;
} else {
HttpPost httpPost = new HttpPost(urlStr);
List<NameValuePair> nvps=new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : params.entrySet()) {
nvps.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));
}
httpPost.setEntity(new UrlEncodedFormEntity(nvps,"UTF-8"));
return httpPost;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private URI makeURI(String urlStr, Map<String, String> params) {
URIBuilder uriBuilder = null;
URI uri = null;
try {
uriBuilder = new URIBuilder(urlStr);
for (Map.Entry<String, String> entry : params.entrySet()) {
uriBuilder.setParameter(entry.getKey(), entry.getValue());
}
//URIBuilder内部用utf-8编码
uri = uriBuilder.build();
} catch (URISyntaxException e) {
e.printStackTrace();
}
try {
log.debug("The uri = " + uri.toURL());
} catch (MalformedURLException e) {
e.printStackTrace();
}
return uri;
}



[size=medium]Get接收参数需要在tomcat的server.xml修改[/size]
<Connector port="8080" maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" URIEncoding='UTF-8' />

[size=medium]POST处理编码需要在servlet中添加[/size]
 request.setCharacterEncoding("UTF-8");    
response.setCharacterEncoding("UTF-8");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值