解决办法---------------
①:
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")。
本人使用第一种方法解决了,第二种方法好像不行
package jrx.anyest.third.utils;
import org.apache.commons.io.Charsets;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
public class HttpsClient {
public static String post(String uri) throws Exception {
return post(uri, null);
}
public static String post(String uri, String data) throws Exception {
String result = "";
CloseableHttpClient httpclient = null;
CloseableHttpResponse response = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
httpclient = getSSLHttpClient();
HttpPost httpPost = new HttpPost(uri);
//设置超时
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(3000).setConnectionRequestTimeout(2000).setSocketTimeout(3000).build();
httpPost.setConfig(requestConfig);
if (StringUtils.isNotBlank(data)) httpPost.setEntity(new StringEntity(data, Charsets.UTF_8));
httpPost.setHeader("Content-Type", "application/json");
httpPost.setHeader("charset", Charsets.UTF_8.displayName());
response = httpclient.execute(httpPost);
HttpEntity httpEntity = response.getEntity();
// if (httpEntity != null) result = EntityUtils.toString(httpEntity);
if (null != httpEntity) {
InputStream is = httpEntity.getContent();
int len = 0;
byte[] b = new byte[2048];
while((len = is.read(b)) != -1){
baos.write(b, 0, len);
}
}
result = new String(baos.toByteArray());
} finally {
if (response != null) response.close();
if (httpclient != null) httpclient.close();
if (baos != null) baos.close();
}
return result;
}
private static CloseableHttpClient getSSLHttpClient() throws Exception {
CloseableHttpClient httpclient;
X509TrustManager trustManager = new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
};
SSLContext sc = SSLContext.getInstance(SSLConnectionSocketFactory.SSL);
sc.init(null, new TrustManager[]{trustManager}, new SecureRandom());
SSLConnectionSocketFactory factory = new SSLConnectionSocketFactory(sc, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
httpclient = HttpClients.custom().setSSLSocketFactory(factory).build();
return httpclient;
}
public static String get(String uri) throws Exception {
String result = "";
CloseableHttpClient httpclient = null;
CloseableHttpResponse response = null;
try {
httpclient = getSSLHttpClient();
HttpGet httpget = new HttpGet(uri);
response = httpclient.execute(httpget);
HttpEntity httpEntity = response.getEntity();
if (httpEntity != null) {
result = EntityUtils.toString(httpEntity);
}
} finally {
if (response != null) response.close();
if (httpclient != null) httpclient.close();
}
return result;
}
}