title: Connection Failed: Timeout for Testing API using Java, Apache HTTP Client
body: <p>I am trying to test API using Java. I am using Java 8, Apache HTTP client 4.5.3 to test it. I tried many different ways to testing using Java .net class, Apache HTTP client but every time same issue; </p>
  
  <blockquote>
    <p>Exception in thread "main"
    org.apache.http.conn.HttpHostConnectException: Connect to
    api.github.com:443 [api.github.com/192.30.253.116,
    api.github.com/192.30.253.117] failed: Connection timed out: connect
      at
    org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:159)</p>
  </blockquote>
  
  <p>Everytime I am getting time out. But if I use same URL in Browser I am getting result.</p>
  
  <p>Can someone help me to point out issue? Whether its setup issue or code issue?</p>
  
  <p>Tried almost all codes available on internet. I am beginner for API testing and don't have knowledge of in depth of HTTP workflow.</p>
  
  <pre><code>import org.apache.http.HttpResponse;
  import org.apache.http.client.methods.HttpGet;
  import org.apache.http.client.methods.HttpUriRequest;
  import org.apache.http.impl.client.HttpClientBuilder;
  
  import java.io.IOException;
  import java.net.*;
  
  public class API {
  
  
      public static void main(String args[]) throws IOException, URISyntaxException {
  
  
          HttpUriRequest request = new HttpGet( "https://api.github.com" );
  
          // When
          HttpResponse response = HttpClientBuilder.create().build().execute( request );
  
          System.out.println(response.getStatusLine().getStatusCode());
      }
  }
  </code></pre>
  
  <p>Using Java .net package</p>
  
  <pre><code>import java.io.IOException;
  import java.net.*;
  
  public class API {
  
  
      public static void main(String args[]) throws  IOException, URISyntaxException {
  
  
          URL url = new URL("http://maps.googleapis.com/maps/api/geocode/json?address=chicago&amp;sensor=false");
          //URL url = uri.toURL();
  
          HttpURLConnection conn = (HttpURLConnection) url.openConnection();
          conn.setRequestMethod("GET");
          conn.setRequestProperty("Accept", "application/xml");
  
          if (conn.getResponseCode() != 200) {
              throw new RuntimeException("HTTP error code : "
                      + conn.getResponseCode());
          }
  
      }
  }
  </code></pre>