Created
February 5, 2011 22:32
-
-
Save manzke/812869 to your computer and use it in GitHub Desktop.
This ServiceFactory is an Example how to ignore SSL-Certificates and Hostnames. This can be used, if you always trusted the endpoint or if you just want to give it a try!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.saperion.webservice; | |
import java.io.IOException; | |
import java.net.URL; | |
import java.security.cert.X509Certificate; | |
import javax.net.ssl.HostnameVerifier; | |
import javax.net.ssl.HttpsURLConnection; | |
import javax.net.ssl.SSLContext; | |
import javax.net.ssl.SSLSession; | |
import javax.net.ssl.TrustManager; | |
import javax.net.ssl.X509TrustManager; | |
import javax.xml.namespace.QName; | |
public class ServiceFactory { | |
public static MyService createService(){ | |
... | |
} | |
static X509TrustManager xtm = new X509TrustManager() { | |
@Override | |
public void checkClientTrusted(X509Certificate[] arg0, String arg1) { | |
return; | |
} | |
@Override | |
public void checkServerTrusted(X509Certificate[] arg0, String arg1) { | |
return; | |
} | |
@Override | |
public X509Certificate[] getAcceptedIssuers() { | |
return null; | |
} | |
}; | |
static TrustManager tm[] = { xtm }; | |
static HostnameVerifier hv = new HostnameVerifier() { | |
@Override | |
public boolean verify(String hostname, SSLSession session) { | |
return true; | |
} | |
}; | |
static { | |
try { | |
SSLContext ctx = SSLContext.getInstance("SSL"); | |
ctx.init(null, tm, null); | |
HttpsURLConnection.setDefaultSSLSocketFactory(ctx.getSocketFactory()); | |
HttpsURLConnection.setDefaultHostnameVerifier(hv); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment