Reputation: 508
I try to make a simple GET request with Spring's RestTemplate.
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers headers = new HttpHeaders() {
{
String auth = username + ":" + password;
byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes(StandardCharsets.US_ASCII));
String authHeader = "Basic " + new String(encodedAuth);
set(HttpHeaders.AUTHORIZATION, authHeader);
//set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON.toString());
}
};
ResponseEntity<FileTypeList> response = restTemplate.exchange(endpoint + FILES, HttpMethod.GET, new HttpEntity(headers), FileTypeList.class);
return response.getBody() != null ? response.getBody().getFileTypes() : Collections.emptyList();
This call gets a timeout error:
java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[na:1.8.0_161]
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85) ~[na:1.8.0_161]
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_161]
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_161]
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_161]
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) ~[na:1.8.0_161]
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_161]
at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_161]
at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:673) ~[na:1.8.0_161]
But when I try same call with Postman, it properly works.
The endpoint is https://verda.borsaistanbul.com/files
How Spring RestTemplate differs from Postman ?
Upvotes: 1
Views: 4216
Reputation: 716
Solutions of Connections time out:
A. Proxy Configuration:
SimpleClientHttpRequestFactory clientHttpReq = new SimpleClientHttpRequestFactory();
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(PROXY, 80));
clientHttpReq.setProxy(proxy);
B. Disable IPv6 stack use for IPv4 IPs on JRE?
i.e. use JVM argument as -Djava.net.preferIPv4Stack=true
Upvotes: 2
Reputation: 508
This solution worked for me; I achieved to add proxy to request header:
SimpleClientHttpRequestFactory clientHttpReq = new SimpleClientHttpRequestFactory();
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(PROXY, 80));
clientHttpReq.setProxy(proxy);
Upvotes: 2
Reputation: 17893
The Rest template lets you set the proxy. There are multiple ways to do that, most simple is as follows:
Before creating RestTemplate, following code is used to set the proxy:
Properties props = System.getProperties();
props.put("http.proxyHost", "your.proxy.server");
props.put("http.proxyPort", "80");
Please note that this will set the proxy for the while Java Appliction in JVM.
Another way to set the proxy is as follows which will set the proxy only for your current code:
SimpleClientHttpRequestFactory clientHttpReq = new SimpleClientHttpRequestFactory();
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("your.proxy.server", 80));
clientHttpReq.setProxy(proxy);
RestTemplate restTemplate = new RestTemplate(clientHttpReq);
Upvotes: 3