我正在使用rest模板调用外部系统,它在我的本地工作正常,没有任何超时设置,但在我的测试服务器上,它给了我以下错误:
“https://externalsystem/url”的POST请求上的I / O错误:连接被拒绝:连接;嵌套异常是java.net.ConnectException:连接被拒绝:连接
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
HttpEntity<MultiValueMap<String, String>> request = new
HttpEntity<MultiValueMap<String, String>>(map, headers);
map.add("key", value);
restTemplate.postForEntity(url, request, String.class);
答案 0 :(得分:0)
这肯定是防火墙问题 尝试设置host-proxy及其端口,如下所示:
System.setProperty("proxyHost", "yourproxy.server");
System.setProperty("proxyPort", "portValue");
答案 1 :(得分:0)
我在JRE1.7上收到了此问题,但在JDK 8上工作正常。 解决此问题的步骤
使用UnlimitedJCEPolicyJDK7更新JCE。从oracle java网站-http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html
包括jar文件httpclient-4.5.2.jar和httpcore-4.4.4.jar
使用以下代码获取getRestTemplate:
public static RestTemplate getRestTemplate()
throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
//TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
TrustStrategy acceptingTrustStrategy = new TrustStrategy() {
public boolean isTrusted(
final X509Certificate[] chain, String authType) throws CertificateException {
// Oh, I am easy...
return true;
}
};
SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy)
.build();
//SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);
SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(
sslContext,new String[]{"TLSv1.2"},
null,
new NoopHostnameVerifier());
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(csf).build();
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
/*HttpComponentsClientHttpRequestFactory requestFactory
= new HttpComponentsClientHttpRequestFactory(
HttpClientBuilder.create()
.setProxy(new HttpHost("proxycacheST.hewitt.com", 3228, "http"))
.build());*/
requestFactory.setHttpClient(httpClient);
RestTemplate restTemplate = new RestTemplate(requestFactory);
return restTemplate;
}
第4步:调用restTemplate.exchange(resturl,HttpMethod.POST,null,String.class);
希望它会得到正确的结果