Created
August 21, 2012 17:37
-
-
Save andrewpthorp/3417669 to your computer and use it in GitHub Desktop.
Java Ignore SSL Cert Errors
This file contains hidden or 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
String url = "https://path/to/url/service"; | |
HttpClient client = new HttpClient(); | |
PostMethod method = new PostMethod(url); | |
// Test whether to ignore cert errors | |
if (ignoreCertErrors){ | |
TrustManager[] trustAllCerts = new TrustManager[]{ | |
new X509TrustManager(){ | |
public X509Certificate[] getAcceptedIssuers(){ return null; } | |
public void checkClientTrusted(X509Certificate[] certs, String authType) {} | |
public void checkServerTrusted(X509Certificate[] certs, String authType) {} | |
} | |
}; | |
try { | |
SSLContext sslContext = SSLContext.getInstance("SSL"); | |
sslContext.init(null, trustAllCerts, new SecureRandom()); | |
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory()); | |
} catch (Exception e){ | |
e.printStackTrace(); | |
} | |
} | |
try { | |
// Execute the method (Post) and set the results to the responseBodyAsString() | |
int statusCode = client.executeMethod(method); | |
resultsBody = method.getResponseBodyAsString(); | |
} catch (HttpException e){ | |
e.printStackTrace(); | |
} catch (IOException e){ | |
e.printStackTrace(); | |
} finally { | |
method.releaseConnection(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment