0

I'm facing strange behavior when i request my own ping server with java HttpComponent client. Sometimes, an unknownHostException is thrown with no reason.

This exception is principally thrown after switching network, for example when i changed the default network route (from eth0 to wifi or from wifi to other mobile NIC)

For information, my wifi connection is enabled through mobile access point. (Could this point reason of my issue ? ) I'm running on a linux OS, in embededded context with limited linux command.

Below, code example:

        HttpGet get = new HttpGet("someUri");
        if (networkInterfaceName != null) { // used to ping through specific route
            get.setConfig(RequestConfig.custom()
                    .setLocalAddress(networkInterfaceToInetAdress(networkInterfaceName ))
                    .setConnectionRequestTimeout(15000)
                    .setConnectTimeout(15000)
                    .setSocketTimeout(15000).build());
        }
        HttpResponse response = HttpClientBuilder.create().useSystemProperties().build().execute(get);

So, if a specific interface is provided, local address is set in the requestConfig, otherwise we use the default route.

I'm currently not abled to identify the root cause and some actions are tried to identify the root cause.

First action, i checked on each httpRequest, route and /etc/resolv.conf and everything seems ok. Ping cmd result is also ok when unknown host exception is thrown.

I checked the httpClient code, and it seems that it create a new client on each call, so there is no httpClient cache for me.I used HttpClientBuilder class and it seems that create method call return always a new builder. Also, cloaseableHttpClient is not closed explicitly, should it impact next call ? I checked httpClient method, it enable setting a dnsResolver, but in my case, but i've no control to remote/targeted ip.

I set JVM system properties in code (and not as Java ARGS directly):

java.security.Security.setProperty("networkaddress.cache.ttl", "0");
java.security.Security.setProperty("networkaddress.cache.negative.ttl", "0");
System.setProperty("java.net.preferIPv4Stack", "true");
System.setProperty("sun.net.inetaddr.ttl", "0");

Also, i'm going to use dnsjava lib when unknowhostexception is thrown as dig/dnslookup are not available in my limited OS.

Any hint about this issue ? or other thing that i need to ckeck ? may be it's not a DNS issue but something in Java socket or connection that i passed by ?

UPDATE I tried to run same logic in another JVM and httpGet request are ok while httpGet fail in my original program that is running in another JVM

Thanks for your help.

Otman El
  • 49
  • 4
  • This is not a programming question but more related to your OS. Try asking on https://serverfault.com/ – Queeg Aug 17 '22 at 10:16
  • Hi, thanks a lot for your response. Currently we don't know if it's an OS issue or something due to JAVA/JVM .... But i'm going to ask an other question into your link. Thanks – Otman El Aug 17 '22 at 10:22
  • I just tried to run another Java program, so another JVM with same logic and httpGet and ping are ok. So, may be not OS issue and possible programming question – Otman El Aug 18 '22 at 13:12

0 Answers0