-
Notifications
You must be signed in to change notification settings - Fork 0
HttpClient with SSL (Without Auth)
allesgutevn edited this page Apr 11, 2018
·
1 revision
import javax.net.ssl.SSLContext; import java.security.cert.CertificateException; import java.security.cert.X509Certificate;
// https://hc.apache.org/httpcomponents-client-4.5.x/index.html import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.conn.ssl.TrustStrategy; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.ssl.SSLContexts; import org.apache.http.util.EntityUtils;
public class MyHttpClient {
public final static void main(String[] args) throws Exception {
// Setup a Trust Strategy that allows all certificates.
// !!! DO NOT USE THIS IN PRODUCTION !!!
SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() {
public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
return true;
}
}).build();
// Allow TLSv1 protocol only
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sslcontext,
new String[] { "TLSv1" },
null,
SSLConnectionSocketFactory.getDefaultHostnameVerifier()
);
CloseableHttpClient httpclient = HttpClients.custom()
.setSSLSocketFactory(sslsf)
.build();
try {
// Get URL
HttpGet httpget = new HttpGet("https://input.livetracking.io/time");
System.out.println("Executing request " + httpget.getRequestLine());
CloseableHttpResponse response = httpclient.execute(httpget);
try {
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
System.out.println(EntityUtils.toString(entity));
} finally {
response.close();
}
} finally {
httpclient.close();
}
}
}
test sidebar