httClient 发送 https 请求返回中文乱码

 

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

①:

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;
    }

}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值