java 绕过ssl访问webservice

private static void trustAllHttpsCertificates() throws Exception {
    javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1];
    javax.net.ssl.TrustManager tm = new miTM();
    trustAllCerts[0] = tm;
    javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext.getInstance("SSL");
    sc.init(null, trustAllCerts, null);
    javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
}
static class miTM implements javax.net.ssl.TrustManager, javax.net.ssl.X509TrustManager {
    public java.security.cert.X509Certificate[] getAcceptedIssuers() {
        return null;
    }
    public boolean isServerTrusted(java.security.cert.X509Certificate[] certs) {
        return true;
    }
    public boolean isClientTrusted(java.security.cert.X509Certificate[] certs) {
        return true;
    }
    public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType)
        throws java.security.cert.CertificateException {
        return;
    }
    public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType)
        throws java.security.cert.CertificateException {
        return;
    }
}


在你要

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

以前,运行下面几行代码:

trustAllHttpsCertificates();
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) {
    return true;
}
};
HttpsURLConnection.setDefaultHostnameVerifier(hv);

所包含的包:

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;


另一种方法

SSLSocketFactory.getSocketFactory().setHostnameVerifier(new AllowAllHostnameVerifier());



java 绕过ssl访问webservice


本站如无特别说明即为原创,转而告知:(http://iwonmo.com/archives/1301.html)

标签: java, webservice, soap绕过ssl, java绕过ssl

添加新评论