5

I am invoking external system with rest template and it's working fine in my local without any timeout settings, but on my test server, it's giving me the following error:

I/O error on POST request for "https://externalsystem/url": Connection refused: connect; nested exception is java.net.ConnectException: Connection refused: 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);
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
user1441280
  • 51
  • 1
  • 1
  • 2
  • Possible duplicate of [java.net.ConnectException: Connection refused](https://stackoverflow.com/questions/6876266/java-net-connectexception-connection-refused) – Jonathan Coustick Mar 27 '18 at 15:46
  • no Jonathan, tat was a different issue! – user1441280 Mar 27 '18 at 15:52
  • @JonathanCoustick it's a different issue Jonathan – Suresh Jul 04 '18 at 13:37
  • @user1441280 did you manage to get a solution for this issue? I'm also facing the same issue now. However I observed this different scenario in my case. When I execute the url using REST API Client I'm able to get response but when the same url is accessed through code (same code as you shared here), it's throwing me the connection timedout error. let me know if you resolved this issue. – Suresh Jul 04 '18 at 13:40

2 Answers2

4

It's certainly a firewall issue Try to set the host-proxy and its port like so:

System.setProperty("proxyHost", "yourproxy.server");
System.setProperty("proxyPort", "portValue");
Meziane
  • 1,586
  • 1
  • 12
  • 22
3

I received this issue on JRE1.7 but worked fine with JDK 8. Steps to resolve this issue

  1. Update JCE with UnlimitedJCEPolicyJDK7. Download it from oracle java site - http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html

  2. include jar files httpclient-4.5.2.jar and httpcore-4.4.4.jar

  3. Use below code to 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;
    }
    

    Step 4: call restTemplate.exchange(resturl, HttpMethod.POST, null, String.class);

    Hope it will get correct results

  • 1
    Don't put the whole answer in a code format. This is not in your good practice. Try a different approach or at least mention some other important information including your code which may help the other user get a better understanding – Abhinav Jul 11 '18 at 10:07
  • at which point we need to mention the ciphers explicitly , any answers ? – Vimalkumar Natarajan Aug 05 '20 at 06:23