---
title: "httpClient发送 返回报文乱码,httpClient发送https请求"
author: "程序员是怎么炼成的"
date: "2015-10-29T10:35:04Z"
tags: ["HttpClient", "乱码"]
categories: ["编程语言"]
slug: "httpclient-httpclient-https"
draft: false
lastmod: "2015-10-29T10:40:07Z"
id: 2252888
url: "https://95028410.iteye.com/blog/2252888"
---

httpClient发送 返回报文乱码,返回的报文确实为utf-8，然而用

HttpEntity entity = response.getEntity();

String retule=EntityUtils.toString(entity, "UTF-8");

指定实体的编码返回的报文还是乱码。

 

 

解决办法---------------

①：

HttpEntity entity = response.getEntity()

InputStream  is =entity.getContent();

 

用输出流接收is中的内容就行了，这样的话第三方放回什么内容，此处就会接收到什么内容，不会出现乱码的问题。

 

②：

使用String类的构造方法进行编码转换

String temp=EntityUtils.toString(entity, "UTF-8");

String result=new String(temp.getBytes("ISO-8859-1"),"utf-8")。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

public static String sendSSLPostRequest\_unitedB2C(String reqURL,

Map&lt;String, String&gt; params) {

String responseContent = null; // 响应内容

HttpClient httpClient = new DefaultHttpClient(); // 创建默认的httpClient实例

X509TrustManager xtm = new X509TrustManager() { // 创建TrustManager

public void checkClientTrusted(X509Certificate\[] chain,

String authType) throws CertificateException {

}

 

public void checkServerTrusted(X509Certificate\[] chain,

String authType) throws CertificateException {

}

 

public X509Certificate\[] getAcceptedIssuers() {

return null;

}

};

try {

// TLS1.0与SSL3.0基本上没有太大的差别，可粗略理解为TLS是SSL的继承者，但它们使用的是相同的SSLContext

SSLContext ctx = SSLContext.getInstance("TLS");

// 使用TrustManager来初始化该上下文，TrustManager只是被SSL的Socket所使用

ctx.init(null, new TrustManager\[] { xtm }, null);

// 创建SSLSocketFactory

SSLSocketFactory socketFactory = new SSLSocketFactory(ctx);

// 通过SchemeRegistry将SSLSocketFactory注册到我们的HttpClient上

httpClient.getConnectionManager().getSchemeRegistry()

.register(new Scheme("https", 443, socketFactory));

// 请求超时

httpClient.getParams().setParameter(

CoreConnectionPNames.CONNECTION\_TIMEOUT, 20000);

// 读取超时

httpClient.getParams().setParameter(

CoreConnectionPNames.SO\_TIMEOUT, 20000);

HttpPost httpPost = new HttpPost(reqURL); // 创建HttpPost

List&lt;NameValuePair&gt; formParams = new ArrayList&lt;NameValuePair&gt;(); // 构建POST请求的表单参数

for (Map.Entry&lt;String, String&gt; entry : params.entrySet()) {

formParams.add(new BasicNameValuePair(entry.getKey(), entry

.getValue()));

}

httpPost.setEntity(new UrlEncodedFormEntity(formParams, "UTF-8"));

 

HttpResponse response = httpClient.execute(httpPost); // 执行POST请求

HttpEntity entity = response.getEntity(); // 获取响应实体

 

ByteArrayOutputStream baos = new ByteArrayOutputStream();

if (null != entity) {

InputStream is = entity.getContent();

 

int len=0;

byte\[] b=new byte\[2014];

 

   while((len = is.read(b)) !=  -1){

       baos.write(b, 0, len);

   }

}

responseContent=new String(baos.toByteArray());

} catch (Exception e) {

logger.error("HttpClientUtil SSL POST请求异常,堆栈信息如下", e);

} finally {

httpClient.getConnectionManager().shutdown(); // 关闭连接,释放资源

}

return responseContent;

}