RestTemplate 交换超时但 PostMan 工作
RestTemplate Exchange Timeouts but PostMan Works
我尝试使用 Spring 的 RestTemplate 发出一个简单的 GET 请求。
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();
此调用出现超时错误:
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]
但是当我尝试与 Postman 进行相同的调用时,它正常工作。
终点是https://verda.borsaistanbul.com/files
RestTemplate Spring 与 Postman 有何不同?
Rest 模板可让您设置代理。有多种方法可以做到这一点,最简单的方法如下:
在创建RestTemplate之前,使用以下代码设置代理:
Properties props = System.getProperties();
props.put("http.proxyHost", "your.proxy.server");
props.put("http.proxyPort", "80");
请注意,这将在 JVM 中为 while Java 应用程序设置代理。
另一种设置代理的方法如下,它将仅为您当前的代码设置代理:
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);
这个解决方案对我有用;我实现了向请求添加代理 header:
SimpleClientHttpRequestFactory clientHttpReq = new SimpleClientHttpRequestFactory();
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(PROXY, 80));
clientHttpReq.setProxy(proxy);
连接超时的解决方法:
一个。代理配置:
SimpleClientHttpRequestFactory clientHttpReq = new SimpleClientHttpRequestFactory();
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(PROXY, 80));
clientHttpReq.setProxy(proxy);
乙。在 JRE 上禁用 IPv6 堆栈用于 IPv4 IP?
即使用 JVM 参数作为 -Djava.net.preferIPv4Stack=true
我尝试使用 Spring 的 RestTemplate 发出一个简单的 GET 请求。
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();
此调用出现超时错误:
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]
但是当我尝试与 Postman 进行相同的调用时,它正常工作。
终点是https://verda.borsaistanbul.com/files
RestTemplate Spring 与 Postman 有何不同?
Rest 模板可让您设置代理。有多种方法可以做到这一点,最简单的方法如下:
在创建RestTemplate之前,使用以下代码设置代理:
Properties props = System.getProperties();
props.put("http.proxyHost", "your.proxy.server");
props.put("http.proxyPort", "80");
请注意,这将在 JVM 中为 while Java 应用程序设置代理。
另一种设置代理的方法如下,它将仅为您当前的代码设置代理:
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);
这个解决方案对我有用;我实现了向请求添加代理 header:
SimpleClientHttpRequestFactory clientHttpReq = new SimpleClientHttpRequestFactory();
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(PROXY, 80));
clientHttpReq.setProxy(proxy);
连接超时的解决方法:
一个。代理配置:
SimpleClientHttpRequestFactory clientHttpReq = new SimpleClientHttpRequestFactory();
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(PROXY, 80));
clientHttpReq.setProxy(proxy);
乙。在 JRE 上禁用 IPv6 堆栈用于 IPv4 IP?
即使用 JVM 参数作为 -Djava.net.preferIPv4Stack=true