Disabling Certificate Validation in an HTTPS Connection


final static HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {
	public boolean verify(String hostname, SSLSession session) {
		return true;
	}
};

//Trust every server - dont check for any certificate 
private static void trustAllHosts() {
	// Create a trust manager that does not validate certificate chains
	TrustManager[] trustAllCerts = new TrustManager[] {
		new X509TrustManager() {
			public java.security.cert.X509Certificate[] getAcceptedIssuers() {
				return new java.security.cert.X509Certificate[] {};
			}
			public void checkClientTrusted(X509Certificate[] chain, String authType) {
			}
			public void checkServerTrusted(X509Certificate[] chain, String authType) {
			}
		}
	};
	// Install the all-trusting trust manager
	try {
		SSLContext sc = SSLContext.getInstance("TLS");
		sc.init(null, trustAllCerts, new java.security.SecureRandom());
		HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
	} catch (Exception e) {
		e.printStackTrace();
	}
}
URL url = new URL(effectiveUrl);
if (url.getProtocol().toLowerCase().equals("https")) {
	trustAllHosts();
	HttpsURLConnection https = (HttpsURLConnection) url.openConnection();
	https.setHostnameVerifier(DO_NOT_VERIFY);
	connection = https;
} else {
	connection = (HttpURLConnection) url.openConnection();
URL url = new URL("http://haat.cn/DAQgE"); 
URLConnection conn = url.openConnection();
((HttpURLConnection) conn).setInstanceFollowRedirects(false);//Location header  302
String val = conn.getHeaderField("Location");
System.out.println(val);

你可能感兴趣的:(certificate)