I am trying to perform a REST request to one of my client URL to get the response using proxy.Most of the time i am able to get the response using my code.But sometimes when i try to send a request using my code i am receiving "407 Proxy Authentication Required" error.This happens rarely but once i get this error,for every consecutive requests i am getting the same error.But when i use POSTMAN tool from chrome to send the same request that was generated to the same URL ,I will be getting the response.But as soon as i get response from POSTMAN if i try with my code again i start getting the response not only from my local machine but from different machines where i run the code as well.I am very confused regarding the issue and not able to figure out why this strange scenario occurs.Is there something i am missing in my code.Kindly help me out guys.I have given my code below :
public Map<String ,Object> ConnectRestService(MyRequest myRequest, String postURL, String httpProxy,int timeout int httpPort, Map<String ,Object> responseMap)
throws KeyManagementException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, CustomException{
MyResponse myResponse = new MyResponse();
Map<String ,Object> responseReturnMap = new HashMap<>();
String output = "";
TrustStrategy acceptingTrustStrategy = new TrustStrategy() {
@Override
public boolean isTrusted(java.security.cert.X509Certificate[] chain, String authType)
throws java.security.cert.CertificateException {
// TODO Auto-generated method stub
return true;
}
};
SSLContext sslContext = SSLContexts.custom()
.loadTrustMaterial(null, acceptingTrustStrategy)
.build();
SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);
HttpClient httpClient;
httpClient = HttpClients.custom().setSSLSocketFactory(csf).setProxy(new HttpHost(httpProxy, httpPort)).build();
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Content-type", "text/xml");
httpHeaders.add("Accept", "text/xml");
httpHeaders.add("access-control-allow-origin", "*");
httpHeaders.add("content-encoding", "UTF-8");
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
requestFactory.setConnectTimeout(timeout);
try {
RestTemplate restTemplate = new RestTemplate(requestFactory);
HttpEntity<MyRequest> entity = new org.springframework.http.HttpEntity<MyRequest>(
myRequest, httpHeaders);
ResponseEntity<String> response = restTemplate.exchange(postURL, HttpMethod.POST, entity, String.class);
} catch (Exception ex) {
logger.error(ex.getMessage(), ex);
}
return responseReturnMap;
}
Kindly help me out figuring out what i am missing here.
In case of a 407 error, the response that you are receiving must contain a special Proxy-Authenticate header. This header will tell you what kind of authentication the proxy server is expecting.
What you need to do is include a Proxy-Authorization header in your request. The typical syntax for a Proxy-Authorization header is Proxy-Authorization:<type-of-authentication-scheme> <credentials-for-authentication-at-proxy-server>.
Proxy-Authenticate header will let you know the type of authentication scheme that you need to use.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With