Last active
November 20, 2020 09:50
-
-
Save ezhov-da/b693f374ed680388c27f53dd61773c09 to your computer and use it in GitHub Desktop.
trust all https
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 ru.ezhov; | |
import javax.net.ssl.*; | |
import java.io.BufferedInputStream; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import java.security.cert.X509Certificate; | |
import java.util.Scanner; | |
import java.util.logging.Logger; | |
public class HttpsTest { | |
private static final Logger LOG = Logger.getLogger(HttpsTest.class.getName()); | |
public static void main(String[] args) { | |
try { | |
URL url = new URL("https://google.ru"); | |
HttpsURLConnection https = (HttpsURLConnection) url.openConnection(); | |
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) { | |
} | |
}}; | |
// Install the all-trusting trust manager | |
SSLContext sc = null; | |
sc = SSLContext.getInstance("SSL"); | |
sc.init(null, trustAllCerts, new java.security.SecureRandom()); | |
https.setSSLSocketFactory(sc.getSocketFactory()); | |
// Create all-trusting host name verifier | |
HostnameVerifier allHostsValid = new HostnameVerifier() { | |
public boolean verify(String hostname, SSLSession session) { | |
return true; | |
} | |
}; | |
// Install the all-trusting host verifier | |
https.setHostnameVerifier(allHostsValid); | |
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); | |
try (Scanner scanner = | |
new Scanner( | |
new BufferedInputStream( | |
httpURLConnection.getInputStream() | |
) | |
) | |
) { | |
while (scanner.hasNextLine()) { | |
System.out.println(scanner.nextLine()); | |
} | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment