I am using Unirest (java version) to make GET and POST request.But I encounter a problem when accessing SSL encrypted site , since my program is behind a corporate network and t
Unfortunately, Unirest does not have a native way to configure SSL, so providing a custom HttpClient instance looks like the only option. Here is a solution, which does not use deprecated classes (like 'DefaultHttpClient') and works with self-signed certificates:
protected void prepareHttpsClient() {
HttpClientBuilder clientBuilder = HttpClientBuilder.create();
try {
String certificateStorage = <>;
String certificatePassword = <>;
SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(
new File(certificateStorage), certificatePassword.toCharArray(),
new TrustSelfSignedStrategy()).build();
SSLConnectionSocketFactory sslFactory = new SSLConnectionSocketFactory(sslContext,
new String[]{"TLSv1"}, null,
SSLConnectionSocketFactory.getDefaultHostnameVerifier());
clientBuilder.setSSLSocketFactory(sslFactory);
}
catch (Exception e) {
throw new IllegalArgumentException("Error configuring server certificates.", e);
}
HttpClient httpClient = clientBuilder.build();
Unirest.setHttpClient(httpClient);
}