Skip to content

Instantly share code, notes, and snippets.

@kaka2507
Created May 18, 2017 07:16
Show Gist options
  • Save kaka2507/6f8d433ff2a12947c781c1a2168a9484 to your computer and use it in GitHub Desktop.
Save kaka2507/6f8d433ff2a12947c781c1a2168a9484 to your computer and use it in GitHub Desktop.
import sun.net.www.protocol.https.HttpsURLConnectionImpl;
import javax.net.ssl.*;
import java.net.HttpURLConnection;
import java.security.Principal;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
import java.util.Base64;
import java.net.URL;
/**
* Hello world!
*
*/
public class App
{
private static String https_url = "https://abc.com/...";
private static String username = "demo";
private static String password = "tavmDnAc4msb";
private static void makeSSLCall(final String URL) throws Exception {
try {
SSLContext sslContext = SSLContext.getInstance("SSL");
// set up a TrustManager that trusts everything
sslContext.init(null, new TrustManager[] { new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
System.out.println("getAcceptedIssuers =============");
return null;
}
public void checkClientTrusted(X509Certificate[] certs,
String authType) {
System.out.println("checkClientTrusted =============");
}
public void checkServerTrusted(X509Certificate[] certs,
String authType) {
System.out.println("checkServerTrusted =============");
}
} }, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(
sslContext.getSocketFactory());
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String s, SSLSession sslSession) {
System.out.println("hostnameVerifier =============");
return true;
}
};
System.out.println("hv:" + hv);
HttpsURLConnection.setDefaultHostnameVerifier(hv);
URL url = new URL(URL);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setHostnameVerifier(hv);
System.out.println("hv:" + conn.getHostnameVerifier());
String encoded = Base64.getEncoder().encodeToString((username+":"+password).getBytes(StandardCharsets.UTF_8)); //Java 8
conn.setRequestProperty("Authorization", "Basic "+encoded);
BufferedReader reader =
new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println("responseCode-->"+line+" for mailerURL : "+URL);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main( String[] args ) {
try {
makeSSLCall(https_url);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment