title: HttpURLConnection Response code: 408 Request Timeout
body: <p>Solved!</p>
  
  <p>This gave me the correct hint:
  <a href="https://stackoverflow.com/questions/5536266/request-to-j-security-check-return-408-error-only-with-right-paramters">Request to j_security_check return 408 error only with right paramters</a></p>
  
  <p>Querying the url /j_security_check is where to send the request to in order to retrieve cookies and the website status code.</p>
  
  <hr>
  
  <p>I'm trying to send a HTTP 'get' request to the website of the company I work for in order to verify that it is up and running. It is hosted on an "Apache Tomcat" server (7.0.34). However, the response code / message always times out.</p>
  
  <p>When using the browser, the site clearly works (but it is slow). I need to be able to programmatically get the response code 200.</p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.net.HttpURLConnection;
  import java.net.URL;
  
  public class HTTPRequest
  {
      URL url;
      HttpURLConnection connection;
  
      public HTTPRequest() throws IOException
      {
          url = new URL("http://www.google.com");
  
          connection = (HttpURLConnection) url.openConnection();
      connection.setConnectTimeout(15000);
      connection.setReadTimeout(15000);
  
          System.out.println("Response code: " + connection.getResponseCode());
          System.out
                  .println("Response message: " + connection.getResponseMessage());
      }
  
      public static void main(String[] args) throws IOException
      {
          new HTTPRequest();
      }
  }
  </code></pre>
  
  <p>Output Google:</p>
  
  <pre><code>Response code: 200
  Response message: OK
  </code></pre>
  
  <p>Output company site:</p>
  
  <pre><code>Response code: 408
  Response message: Request Timeout
  </code></pre>
  
  <p>I used the methods setConnectTimeout() and setReadTimeout() but they both have no effect, the result is printed just as fast as before:
  <a href="https://stackoverflow.com/questions/6829801/httpurlconnection-setconnecttimeout-has-no-effect">HttpURLConnection setConnectTimeout() has no effect</a></p>