1

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;

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)

Everytime I am getting time out. But if I use same URL in Browser I am getting result.

Can someone help me to point out issue? Whether its setup issue or code issue?

Tried almost all codes available on internet. I am beginner for API testing and don't have knowledge of in depth of HTTP workflow.

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());
    }
}

Using Java .net package

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&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());
        }

    }
}

1 Answer 1

6

If the same URL works in browser then there are only three possibilities.

  1. The URL expects headers like User-Agent. You can set request headers needed like this: request.setHeader("User-Agent", "Mozilla");

  2. You are in a corporate or restricted environment and need a proxy to connect to external URLs. Your browser might already be setup to use proxy server. In this case, you will need to pass proxy credentials to http client API.

    Example: https://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org/apache/http/examples/client/ClientProxyAuthentication.java

  3. All outgoing requests are blocked in your environment by firewall or something. In this case, you will need to ask your network admin to allow network connection.

11
  • I tried SetHeader method but didnt work, I get same error message. I dont find getCredentialsProvider method in Apache HttpClient 4.5.3. My company wont allow to change network connection but I will try on my personal machine later. Hoping that will work, but first two options didnt work. Apr 12, 2017 at 15:06
  • Editing with an example of using http proxy. But do you know if your environment requires proxy to connect to external URLs? Apr 12, 2017 at 15:12
  • I check my browser settings, we are not using any proxy to connect external network. Apr 12, 2017 at 19:40
  • If you are using chrom, open developer console, go to network tab. Then try your url. From network tab, select the url you tried and copy Headers and paste to your question. This way we can compare how browser is sending request. Apr 12, 2017 at 19:54
  • Request URL:maps.googleapis.com/maps/api/geocode/… Request Method:GET Status Code:200 OK Remote Address:170.137.249.117:80 Referrer Policy:no-referrer-when-downgrade Apr 12, 2017 at 23:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.