Call this method before access:
/***************************************************************************
* Get web contents using HttpURLConnection class.
**************************************************************************/
public static void handleSSL(String urlString) {
if (urlString == null || urlString.indexOf("https:") < 0) {
return;
}
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
} };
// Install the all-trusting trust manager
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection
.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
e.printStackTrace();
}
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) {
System.out.println("Warning: URL Host: " + urlHostName
+ " vs. " + session.getPeerHost());
return true;
}
};
HttpsURLConnection.setDefaultHostnameVerifier(hv);
}
Ussage:
/*********************************************************************
* Get web contents using HttpURLConnection class (no cookie).
*********************************************************************/
public static String httpContentNoCookie(String urlString) {
log.info("Get content of " + urlString);
String incoming = "";
DataInputStream dis;
handleSSL(urlString);
try {
URL url = new URL(urlString);
HttpURLConnection uconn = (HttpURLConnection) url.openConnection();
uconn.setDoOutput(true);
uconn.setDoInput(true);
dis = new DataInputStream(uconn.getInputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(dis));
String line = "";
while ((line = in.readLine()) != null) {
incoming += line;
}
in.close();
uconn.disconnect();
} catch (Exception e) {
e.printStackTrace();
incoming = "error: " + e.getMessage();
log.error(e);
}
return incoming;
}