I am trying to hit an SSL enabled Rest API by disabling the SSL certificate checks as follows;
TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
}
};
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
SSLContext.setDefault(sc);
HostnameVerifier allHostsValid = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
String auth = "<<username>>:<<password>>";
byte[] authBytes = auth.getBytes(StandardCharsets.UTF_8);
String encodedAuth = Base64.getEncoder().encodeToString(authBytes);
String url = "myURLHere";
Client client = ClientBuilder.newBuilder().sslContext(sc).hostnameVerifier(allHostsValid).build();
String input = "myJsonHere";
Response response = client
.target(url)
.request()
.header("Authorization", "Basic " + encodedAuth)
.post(Entity.json(input));
Refer : https://stackoverflow.com/a/27398820/11226302
However, I am getting the following response;
InboundJaxrsResponse{context=ClientResponse{method=POST, uri=myURLHere, status=502, reason=cannotconnect}}
I also tried the similar logic using HttpsURLConnection.
Refer: https://stackoverflow.com/a/876785/11226302
Getting a similar error as below;
java.lang.RuntimeException: Failed : HTTP error code : 502cannotconnect
I need to do this for testing purposes, but am unable to proceed now.
I am able to get the right response using curl and Postman Client (as mentioned in the Title of this Question) Can anyone suggest what could be going wrong here?
I am using java 8
FYI, the production will have the certificate set up. I am doing this insecure configuration so that I may be able to call this Rest API from my local set up.