httpclient https忽略证书直接请求

原创 2015年06月11日 18:00:38

httpclient请求https   忽略证书直接请求:

封装https忽略证书代码

package com.sunrise.web.utils;

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

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;


public class CertificateAuthorityHttpClientUtil extends DefaultHttpClient{
    CertificateAuthorityHttpClientUtil() 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 httpclient = new CertificateAuthorityHttpClientUtil();
	String headOfficePath = properties.getProperty("headOfficePath");


相关文章推荐

HttpClient忽略证书访问HTTPS接口

HttpClient 如何忽略证书验证 - ALLOW_ALL_HOSTNAME_VERIFIER   1。设置可以访问HTTPS   Function - getNewHttpClient ...

轻松把玩HttpClient之配置ssl,采用绕过证书验证实现https

上篇文章说道httpclient不能直接访问https的资源,这次就来模拟一下环境,然后配置https测试一下。在前面的文章中,分享了一篇自己生成并在tomcat中配置ssl的文章《Tomcat配置S...

HttpClient忽略用户证书验证的方法

程序中出了这样的异常:javax.net.ssl.SSLException: hostname in certificate didn't match。httpClient.execute(httpG...

httpclient忽略证书验证允许https请求

public static void enableSSL(DefaultHttpClient httpclient) {     // 调用ssl     try {         SSLCo...
  • whuyi
  • whuyi
  • 2016年02月22日 16:46
  • 3768

httpclient 4.5.2 https协议时忽略服务器证书

1maven dependence dependency> groupId>org.apache.httpcomponentsgroupId> artifactId>httpclien...

Apache HttpClient4.3.x忽略证书验证

今天需要忽略证书验证的时候,找了一圈没见靠谱的代码,无奈只能去官网看文档了,结果记录一下。官方文档看这里//此处跳过证书验证的方式适用于apache httpclient 4.3.x版本,并不一定适用...

支持https但不验证证书的HttpClient

import java.io.IOException; import java.net.Socket; import java.net.UnknownHostException; import jav...
  • noodies
  • noodies
  • 2013年12月10日 10:55
  • 20852

HttpClient忽略证书访问HTTPS接口()

转载自:http://blog.csdn.net/kobejayandy/article/details/44284765 HttpClient 如何忽略证书验证访问HTTPS接口,解决步骤...

java 访问https忽略证书

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.i...

httpclient https忽略证书

org.apache.httpcomponents httpclient 4.3.6 import java.security.cert.CertificateException; impor...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:httpclient https忽略证书直接请求
举报原因:
原因补充:

(最多只允许输入30个字)