UnknownHostException in java app, in Postman there is not problem

6.3k Views Asked by At

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?

1

There are 1 best solutions below

0
On

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