java httpclient调用https接口报错javax.net.ssl.SSLException: hostname in certificate didn‘t match:

项目上使用httpclient调用https接口  ,报错如下,大概意思是没有找到对应的证书把

网上查的解决资料,记录下

新建一个类继承DefaultHttpClient。重写里面的方法,直接忽略校验https

package work.com.bsoft.utils;

import javax.net.ssl.SSLContext;

import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;

import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

/**
 * httpclinet调用https,忽略校验过程
 * @author gaom
 * 20200716
 *
 */
@SuppressWarnings("deprecation")
public class SSLClient extends DefaultHttpClient{
	public SSLClient() throws Exception{
        super();
        SSLContext ctx = SSLContext.getInstance("TLS");
        X509TrustManager tm = new X509TrustManager() {
                @Override
                public void checkClientTrusted(X509Certificate[] chain,
                        String authType) throws CertificateException {
                }
                @Override
                public void checkServerTrusted(X509Certificate[] chain,
                        String authType) throws CertificateException {
                }
                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
        };
        ctx.init(null, new TrustManager[]{tm}, null);
        SSLSocketFactory ssf = new SSLSocketFactory(ctx,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        ClientConnectionManager ccm = this.getConnectionManager();
        SchemeRegistry sr = ccm.getSchemeRegistry();
        sr.register(new Scheme("https", 443, ssf));
    }
}

 

httpclient客户端调用http接口部分代码,我使用的是httpclient-4.3.1版本


import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

        HttpPost httpPost=new HttpPost(apiUrl);
	       
	        switch (contentType){
	            case "application_json" :
	                StringEntity stringEntity=new StringEntity(bodyStr,"UTF-8");
	                httpPost.setHeader("Content-Type", "application/json; charset=UTF-8");
	                httpPost.setEntity(stringEntity);
	                break;
	            case "application_x_www_form_urlencoded":
	                httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");

	                List<NameValuePair> nameValuePairList=new ArrayList<NameValuePair>() ;

	                Set bodySet=bodyMap.keySet();
	                Iterator iterator=bodySet.iterator();
	                while(iterator.hasNext()){
	                    String key=iterator.next()+"";
	                    String value=bodyMap.get(key)+"";
	                  //  System.out.println("key:"+key+" value:"+value);
	                    nameValuePairList.add(new BasicNameValuePair(""+key+"",""+value+""));
	                }
	                 httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairList,Charset.forName("UTF-8")));
	             break;
	                default:new Exception("contentType为定义");break;
	        }


	        // HttpClient httpClient= HttpClients.createDefault();
	        HttpClient httpClient=new SSLClient();
	        HttpResponse httpResponse=httpClient.execute(httpPost);
	        String httpResponseStr= EntityUtils.toString(httpResponse.getEntity(),"UTF-8");
	        logger.info("callGmsrmyyApi call GmsrmyyApiUrl:"+GmsrmyyApiUrl+" end httpResponseStr:"+httpResponseStr);
	        ObjectMapper objectMapper=new ObjectMapper();
	        Map<String,Object> httpResponseMap=(Map<String, Object>)(objectMapper.readValue(httpResponseStr, Object.class));

主要区别使用SSLclinet类来实例化 HttpClient

 

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值