我调用外部系统与其他模板,它的工作在我的本地罚款没有任何超时设置,但我的测试服务器上,它给我下面的错误:
关于 “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);
这当然是一个防火墙问题尝试设置主机代理和港口,像这样:
System.setProperty("proxyHost", "yourproxy.server");
System.setProperty("proxyPort", "portValue");
我收到这个问题上JRE1.7但JDK的罚款8步骤来解决此问题
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);
希望它会得到正确的结果