package id.darodata.testing;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStream;
import java.net.URL;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
public class IgnoreInvalidSSL {
private static TrustManager[ ] get_trust_mgr() {
TrustManager[ ] certs = new TrustManager[ ] {
new X509TrustManager() {
public X509Certificate[ ] getAcceptedIssuers() { return null; }
public void checkClientTrusted(X509Certificate[ ] certs, String t) { }
public void checkServerTrusted(X509Certificate[ ] certs, String t) { }
}
};
return certs;
}
public static void main(String[] args) {
try{
String url = "https://test.darodata.id";
URL urlc = new URL(url);
java.lang.System.setProperty("https.protocols", "TLSv1.2");
SSLContext ssl_ctx = SSLContext.getInstance("TLS");
TrustManager[ ] trust_mgr = get_trust_mgr();
ssl_ctx.init(null, // key manager
trust_mgr, // trust manager
new SecureRandom()); // random number generator
HttpsURLConnection.setDefaultSSLSocketFactory(ssl_ctx.getSocketFactory());
HttpsURLConnection conn = (HttpsURLConnection)urlc.openConnection();
conn.setRequestProperty("User-Agent", "AppleWebKit/537.36");
conn.setDoInput(true);
conn.setDoOutput(true);
InputStream is = conn.getInputStream();
int read = -1;
StringBuffer sb = new StringBuffer();
File f = new File("D:\\test.txt");
while((read = is.read()) > -1){
sb.append((char)read);
}
FileWriter fw = new FileWriter(f);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(sb.toString());
bw.close();
is.close();
}catch (Exception e) {
e.printStackTrace();
}
}
}