栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

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

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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;


@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 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

 

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/955151.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号