0

How to get the response from API using rest template over proxy network without setting proxy details?

Example: http://gturnquist-quoters.cfapps.io/api/random. I am getting response if opening the browsers, I am trying to consume the API through proxy network but I don't want to use proxyHost and proxyPort in code, getting error : Connection timed out: connect; nested exception is java.net.ConnectException: Connection timed out: connect

public class RestWithoutProxy {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    RestTemplate restTemplate = new RestTemplate(); 
    String url="http://gturnquist-quoters.cfapps.io/api/random";
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);
    System.out.println("... calling api");
    try{
        ResponseEntity<String> response = restTemplate
                .exchange(url, HttpMethod.GET, entity, String.class);
        System.out.println("response: "+ response.getBody());
    }catch(HttpStatusCodeException ex){
        int statusCode = ex.getStatusCode().value();
        System.out.println("error code: "+statusCode+"\n"+ex.getMessage());
    }catch (Exception ex) {
        System.out.println("......inside rest exception\n"+ ex.getMessage());
    }
}

}

Code is not working if using office network where proxy and firewall is enabled, but same code is working if using open network

  • The browser is probably getting the proxy settings from your machine. Have a look here: http://stackoverflow.com/questions/376101/setting-jvm-jre-to-use-windows-proxy-automatically – Thomas Apr 05 '16 at 13:17
  • Welcome to SO. I've your network configuration requires a proxy, then you can't ignore it. Please show us your code – JimHawkins Apr 05 '16 at 13:20
  • 1
    Make proxy settings optionally configurable. – Michal Foksa Apr 05 '16 at 14:05
  • Possible duplicate of [Using RestTemplate, how to send the request to a proxy first so I can use my junits with JMeter?](https://stackoverflow.com/questions/3687670/using-resttemplate-how-to-send-the-request-to-a-proxy-first-so-i-can-use-my-jun) – Alex K Jun 29 '18 at 08:34

1 Answers1

1

I faced the same issue and after two day of debug, finally found the solution. Thought it's better to share this info even though this question was asked few months back.

CAUSE: If you are getting Connection timed out exception then more likely you have proxy issue. SOLUTION: In this case, we have to create RestTemplate using SimpleClientHttpRequestFactory.

You need to get the Proxyhost and proxy port from your server where you have hosted your application. for example - In tomcat, these details can be fetch using below snippet -

System.getProperty("http.yourproxyHost.com")
System.getProperty("http.proxyPort")

Then you can use below Snippet:

SimpleClientHttpRequestFactory clientHttpReq = new SimpleClientHttpRequestFactory();
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("ur.proxy.com", port));
clientHttpReq.setProxy(proxy);

Now RestTemplate can be created in below way -

RestTemplate restTemplate = new RestTemplate(clientHttpReq);

After this you can use your code. It will surely work. Let me know if you face any issue.

Chait
  • 1,052
  • 2
  • 18
  • 30
aks2012
  • 121
  • 1
  • 7