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接口代码

        HttpPost httpPost=new HttpPost(GmsrmyyApiUrl);
	       
	        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 nameValuePairList=new ArrayList() ;

	                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 httpResponseMap=(Map)(objectMapper.readValue(httpResponseStr, Object.class));

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

 

 

 

你可能感兴趣的:(软件)