0

I am trying to hit an API that is on internet in a Java app. However, I am getting an UnknownHostException exception.

If I hit the same API using Postman, I get the results correctly.

I have tried several things I found on internet like for example specify the proxies when executing the java app:

-Dhttp.proxyHost=<some_proxy_url> -Dhttp.proxyPort=80 -Dhttps.proxyHost=<some_proxy_url> -Dhttps.proxyPort=80 -Dhttps.proxySet=true -Dhttp.proxySet=true

but this hasn't helped me at all.

This is the code I have to do the request:

    try (CloseableHttpClient httpClient = HttpClients.createDefault()) {

        HttpRequestBase request = new HttpPost(
                "https://some-host.com/some-api");

        HttpResponse response = httpClient.execute(request);

        System.out.println(response.getStatusLine().getStatusCode());
    } catch (IOException e) {

        e.printStackTrace();
    }

This is the full stack trace of the exception:

java.net.UnknownHostException: some-host.com: nodename nor servname provided, or not known
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:928)
at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1323)
at java.net.InetAddress.getAllByName0(InetAddress.java:1276)
at java.net.InetAddress.getAllByName(InetAddress.java:1192)
at java.net.InetAddress.getAllByName(InetAddress.java:1126)
at org.apache.http.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:45)
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:112)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:373)
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:394)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:237)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108)
at App.connect(App.java:25)
at App.main(App.java:15)

I have noticed that I have problems with any host that is outside my machine. I have even tried with the www.google.com host, and I get the same exception.

However, if I use an API of my localhost I have no issues whatsoever.

What am I missing?

Dante
  • 3,208
  • 9
  • 38
  • 56
  • If you don't normally use a proxy, you should use one either with Java. Also, please post the full exception stacktrace. – Mark Rotteveel Oct 09 '18 at 17:44
  • @MarkRotteveel, my company uses a proxy, however, I ran the app with proxies and no proxies and got the same result. – Dante Oct 09 '18 at 18:30
  • The error you're getting indicates there's a problem resolving the host name (e.g. some-host.com). Make sure you can resolve that name using something like `nslookup`. If you can't, it's likely you can't hit the API directly and you have to go through the proxy. If that's the case, make sure Java is actually using the proxy. Since you're using Apache HTTPClient, check out the "Request via a proxy" tutorial [here](https://hc.apache.org/httpcomponents-client-ga/examples.html). – Michael Powers Oct 09 '18 at 18:33
  • @MichaelPowers, indeed, `nslookup` can't either resolve the host name. But why I don't have any problems with Postman? – Dante Oct 09 '18 at 18:39
  • Postman is likely inheriting the proxy settings from your operating system. The default is "use system proxy", see [this section of the Postman manual](https://www.getpostman.com/docs/v6/postman/sending_api_requests/proxy#configuring-proxy-settings). – Michael Powers Oct 09 '18 at 18:44

1 Answers1

0

Based on the commentary above it sounds like the application endpoint you're trying to reach is only available via a proxy which Postman is configured to use and your Java application, using Apache HTTPClient currently isn't.

You should be able to configure HTTPClient to use a proxy by doing the following before you issue your request:

HttpHost target = new HttpHost("some-host.com", 443, "https");
HttpHost proxy = new HttpHost("<your-proxy-host>", /* proxy-port */, "http");

RequestConfig config = RequestConfig.custom()
        .setProxy(proxy)
        .build();
HttpGet request = new HttpPost("/some-api");
request.setConfig(config);
CloseableHttpResponse response = httpclient.execute(target, request);
Michael Powers
  • 1,970
  • 1
  • 7
  • 12