Skip to content

Instantly share code, notes, and snippets.

@nettree
Created April 7, 2016 00:41
Show Gist options
  • Save nettree/bab2bd171964ce5f938bb4cd61790f89 to your computer and use it in GitHub Desktop.
Save nettree/bab2bd171964ce5f938bb4cd61790f89 to your computer and use it in GitHub Desktop.
/**
* By pass the SSL validation
* @param url URL used to establish connection
* @return
* @throws Exception
*/
private HttpsURLConnection getSSLConnection(String url) throws Exception {
URL accessUrl = new URL(url);
TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
}
};
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
// Create all-trusting host name verifier
HostnameVerifier allHostsValid = (hostname, session) -> true;
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
HttpsURLConnection conn = (HttpsURLConnection) accessUrl.openConnection();
conn.setRequestProperty("Authorization", basicAuth());
conn.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Accept", "*/*" );
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0;Windows98;DigExt)");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod(HttpMethods.POST);
conn.connect();
return conn;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment