I /关于“URL” POST请求O错误:连接被拒绝

问题描述 投票:1回答:2

我调用外部系统与其他模板,它的工作在我的本地罚款没有任何超时设置,但我的测试服务器上,它给我下面的错误:

关于 “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);
resttemplate
2个回答
2
投票

这当然是一个防火墙问题尝试设置主机代理和港口,像这样:

System.setProperty("proxyHost", "yourproxy.server");
System.setProperty("proxyPort", "portValue");

0
投票

我收到这个问题上JRE1.7但JDK的罚款8步骤来解决此问题

  1. 更新JCE与UnlimitedJCEPolicyJDK7。从Oracle的Java网站上下载 - http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html
  2. 包括jar文件的HttpClient-4.5.2.jar和的HttpCore-4.4.4.jar
  3. 使用下面的代码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); 希望它会得到正确的结果
© www.soinside.com 2019 - 2024. All rights reserved.