title: UnirestException: connect timed out on Tomcat 7
body: <p>At work, I am trying to deploy a java 8-based application on Tomcat 7 server. </p>
  
  <p>The application should call some RESTful api based on another server (which is still ours). By reading some resources online I decided to use a HTTP request client library called <a href="http://unirest.io/java.html" rel="nofollow noreferrer">Unirest</a>. </p>
  
  <p>I tried to call the api using Postman and everything works fine.
  I deployed my application on my LOCAL Tomcat 7 server and it runs smoothly without problems and Unirest does its job correctly. </p>
  
  <p>When I deploy the application on the Tomcat 7 server, every Unirest call, leads to a connect timeout. </p>
  
  <p>This is the stacktrace:</p>
  
  <pre><code>    com.mashape.unirest.http.exceptions.UnirestException: org.apache.http.conn.ConnectTimeoutException: Connect to srv-erp2web.rold.corp:80 [srv-erp2web.rold.corp/192.168.5.100] failed: connect timed out
          at com.mashape.unirest.http.HttpClientHelper.request(HttpClientHelper.java:143)
          at com.mashape.unirest.request.BaseRequest.asJson(BaseRequest.java:68)
          at com.rold.industry.api.common.ApiCaller.getAccessToken(ApiCaller.java:66)
          at com.rold.industry.context.ContextListener.contextInitialized(ContextListener.java:118)
          at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5010)
          at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5504)
          at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
          at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
          at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
          at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:632)
          at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:1073)
          at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:553)
          at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1648)
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
          at java.lang.reflect.Method.invoke(Method.java:498)
          at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)
          at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
          at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
          at org.apache.catalina.manager.ManagerServlet.check(ManagerServlet.java:1445)
          at org.apache.catalina.manager.HTMLManagerServlet.upload(HTMLManagerServlet.java:284)
          at org.apache.catalina.manager.HTMLManagerServlet.doPost(HTMLManagerServlet.java:206)
          at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
          at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
          at org.apache.catalina.filters.CsrfPreventionFilter.doFilter(CsrfPreventionFilter.java:213)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
          at org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:108)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
          at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
          at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
          at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:610)
          at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
          at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
          at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
          at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
          at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
          at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
          at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
          at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
          at java.lang.Thread.run(Thread.java:745)
  Caused by: org.apache.http.conn.ConnectTimeoutException: Connect to srv-erp2web.rold.corp:80 [srv-erp2web.rold.corp/192.168.5.100] failed: connect timed out
          at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:134)
          at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:319)
          at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:363)
          at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:219)
                  at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:363)
          at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:219)
          at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195)
          at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86)
          at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
          at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
          at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
          at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)
          at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
          at com.mashape.unirest.http.HttpClientHelper.request(HttpClientHelper.java:138)
          ... 46 more
  Caused by: java.net.SocketTimeoutException: connect timed out
          at java.net.PlainSocketImpl.socketConnect(Native Method)
          at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
          at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
          at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
          at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
          at java.net.Socket.connect(Socket.java:589)
          at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:72)
          at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:125)
          ... 57 more
  </code></pre>
  
  <p>I haven't find any clue on the internet about how this can happen. The really strange thing is that Unirest calls correctly the api on my local Tomcat.</p>
  
  <p>Here a snapshot of the code that I use to call the api:</p>
  
  <pre><code>public static String getAccessToken() {
          logger.log(Level.FINE, "calling smartfabauth to get authentication token");
          String accessToken = "";
          try {
              Unirest.setTimeouts(0, 0); // NO TIMEOUT
              HttpResponse&lt;JsonNode&gt; response = Unirest.post(POST_TOKEN_ENDPOINT)
                      .header("content-type", "application/x-www-form-urlencoded")
                      .header("cache-control", "no-cache")
                      .body("grant_type=" + GRANT_TYPE + "&amp;client_id=" + CLIENT_ID + "&amp;client_secret=" + CLIENT_SECRET + "&amp;scope=" + SCOPE)
                      .asJson();
  
              accessToken = response.getBody().getObject().getString("access_token");
  
          } catch (UnirestException | JSONException e) {
              logger.log(Level.SEVERE, "[Exception] getAccessToken", e);
          }
  
          logger.log(Level.FINE, "authentication token obtained");
          return accessToken;
      }
  </code></pre>
  
  <p>Note that <code>POST_TOKEN_ENDPOINT</code>, <code>GRANT_TYPE</code>, <code>CLIENT_ID</code>, <code>CLIENT_SECRET</code> and <code>SCOPE</code> are some constants declared in class necessary to correctly call the api.</p>
  
  <p>Any help would be apreciated.</p>
  
  <p>Thanks in advance.</p>