【问题标题】:I/O error on POST request for "url": Connection refused“url”的 POST 请求上的 I/O 错误:连接被拒绝
【发布时间】:2018-03-27 15:40:03
【问题描述】:

我正在使用 rest 模板调用外部系统,它在我的本地工作正常,没有任何超时设置,但在我的测试服务器上,它给了我以下错误:

https://externalsystem/url”的 POST 请求出现 I/O 错误:连接被拒绝:连接;嵌套异常是 java.net.ConnectException: Connection denied: connect

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);

【问题讨论】:

  • 不,乔纳森,那是另一个问题!
  • @JonathanCoustick 这是另一个问题,乔纳森
  • @user1441280 您是否设法找到解决此问题的方法?我现在也面临同样的问题。但是,在我的情况下,我观​​察到了这种不同的情况。当我使用 REST API 客户端执行 url 时,我能够获得响应,但是当通过代码(与您在此处共享的代码相同)访问相同的 url 时,它会抛出连接超时错误。如果您解决了这个问题,请告诉我。

标签: resttemplate


【解决方案1】:

肯定是防火墙问题 尝试像这样设置主机代理及其端口:

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

【讨论】:

    【解决方案2】:

    我在 JRE1.7 上收到了这个问题,但在 JDK 8 上运行良好。 解决此问题的步骤

    1. 使用 UnlimitedJCEPolicyJDK7 更新 JCE。从 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. 使用以下代码获取RestTemplate:

      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;
      }
      

      第四步:调用restTemplate.exchange(resturl, HttpMethod.POST, null, String.class);

      希望能得到正确的结果

    【讨论】:

    • 不要将整个答案放在代码格式中。这不是你的好习惯。尝试不同的方法或至少提及一些其他重要信息,包括您的代码,这可能有助于其他用户更好地理解
    • 此时我们需要明确提及密码,有答案吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-24
    • 2015-06-29
    • 2019-06-15
    • 1970-01-01
    • 2022-06-11
    • 1970-01-01
    • 2015-04-07
    相关资源
    最近更新 更多