3

With my code (shown below), I get proper result randomly. But most of the time I get error.

EXCEPTION:

java.net.UnknownHostException: api.api.ai: Temporary failure in name resolution
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:928)
..<snip>...
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55)
at PostJSON.main(PostJSON.java:34)

SSCCE CODE:

private static final String URL = "https://api.api.ai/v1/query?v=20150910";
private static final String ACCESS_CODE = "XXXXXX";
public static final HttpClient client = HttpClientBuilder.create().build();
public static final HttpPost httppost = new HttpPost(URL);

private static final String str_json = "{ query: \"how are you\", lang: \"en\", sessionId: \"somerandomthing\" }";

public static void main(String[] args) {

    try {

        HttpEntity entity = new ByteArrayEntity(str_json.getBytes("UTF-8"));
        httppost.setHeader("Content-Type",
                "application/json; charset=utf-8");
        httppost.setHeader("Authorization", "Bearer " + ACCESS_CODE);
        httppost.setEntity(entity);
        HttpResponse response = client.execute(httppost);
        String result = EntityUtils.toString(response.getEntity());
        System.out.println(result);
    } catch (Exception ex) {
        ex.printStackTrace();
    } 
}

NSLOOKUP:

zeek@zeek:$ nslookup api.ai.ai
Server:     127.0.1.1  
Address:    127.0.1.1#53

Non-authoritative answer:  
Name:   api.ai.ai  
Address: 92.242.140.21

/ETC/HOSTS

127.0.0.1   localhost
127.0.1.1   zeek

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

Note: It does work sometime.

Please suggest what setting/config should I be checking from my side.

also note, my ping to api.api.ai works fine.

Bhargav Rao
  • 41,091
  • 27
  • 112
  • 129
Aman ZeeK Verma
  • 153
  • 1
  • 7
  • Can you post the output of `dig api.api.ai`....also run the command multiple times in order to check if it gets the right answer every time... – Hackerman Aug 24 '17 at 18:18
  • Always got a reply: zeek@zeek:$ dig api.api.ai ; <<>> DiG 9.10.3-P4-Ubuntu <<>> api.api.ai ;; global options: +cmd ;; Got answer: ;; ->>HEADER< – Aman ZeeK Verma Aug 24 '17 at 18:38
  • 1
    For what I am seeing, it seems that resolves to ip4 without issues...but your app sometimes try to resolve using ipv6 and that is why it is failing... – Hackerman Aug 24 '17 at 18:43
  • Is there a way to force ipv4 resolution ? – Aman ZeeK Verma Aug 24 '17 at 18:45
  • -Djava.net.preferIPv4Stack=true is doing the trick so far!!!! I will confirm after few more tries – Aman ZeeK Verma Aug 24 '17 at 18:47
  • I think I have executed it many times to "confirm" preferIPv4Stack works. Is there a way to fix ipv6 translation? Why does the code attempting ipv6? Is that my problem or api.api.ai problem? – Aman ZeeK Verma Aug 24 '17 at 18:58
  • ADDITIONAL INFO: When I unplug my Ethernet (which is via Wifi Extender) and connect to my main wifi. I dont get this resolution problem – Aman ZeeK Verma Aug 24 '17 at 20:09
  • I assume that you are in a Linux environment...since both network cards are different, it seems then that your wifi card has some extra configurations that your eth0 card lacks....finding what makes one different from the other one should do the trick...you can call me Sherlock xD – Hackerman Aug 24 '17 at 20:40
  • Please post an answer if you figured out a solution for other! If not, please add more to your question so we can help! – matthewayne Aug 26 '17 at 00:26

1 Answers1

1

Posting solution to help others facing same problem,

On the lines of what @Hackerman said:

My wifi-extender was not capable to handle IPV6 translation. Always check the wifi-extender spec when encountered with similar problem.

As a solution/hack (if you HAVE to use IPV4-only router) use following when starting java

-DpreferIPv4Stack
Aman ZeeK Verma
  • 153
  • 1
  • 7