December 28, 2017

How to handle "java net unknownhostexception" exception

While creating restTemplate, we have to configure proxy settings like:

@Bean
RestTemplate restTemplate() {
final String username = "xxxxx";
final String password = "XXXXXX";
final String proxyUrl = "XXXX.com";
final int port = 8080;

CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(proxyUrl, port),
new UsernamePasswordCredentials(username, password));
HttpHost myProxy = new HttpHost(proxyUrl, port);
HttpClientBuilder clientBuilder = HttpClientBuilder.create();
clientBuilder.setProxy(myProxy).setDefaultCredentialsProvider(credsProvider).disableCookieManagement();
HttpClient httpClient = clientBuilder.build();
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
factory.setHttpClient(httpClient);      
return new RestTemplate(factory);
}
Driver program:
String getUrl = "https://XXXX/XXXX";//ex: "http://localhost:8080/get?id=1&name='Dasu'&age=30";
ResponseEntity getResponse = restTemplate().getForEntity(getUrl, Object.class);
if (getResponse.getBody() != null) {
System.out.println("Response for Get Request: " + getResponse.getBody().toString());
} else {
System.out.println("Response for Get Request: NULL");
}

No comments:

Post a Comment

I'm certainly not an expert, but I'll try my hardest to explain what I do know and research what I don't know.

My Favorite Site's List

#update below script more than 500 posts