1

I'm using the Google HTTP Client Library for Java to develop a client application for a webservice of mine.

The problem is when I try to run some code like this:

private static void send() throws IOException {
    HttpTransport httpTransport = new ApacheHttpTransport();
    HttpRequestFactory httpRequestFactory = httpTransport
            .createRequestFactory();

    GenericUrl requestUrl = new GenericUrl("https://www.google.com/");
    HttpRequest request = httpRequestFactory.buildGetRequest(requestUrl);

    HttpResponse response = request.execute();

    System.out.println(response.parseAsString());
}

public static void main(String args[]) throws InterruptedException {
    boolean sent = false;

    while (!sent) {
        try {
            send();
            sent = true;
        } catch (IOException e) {
            e.printStackTrace();
            Thread.sleep(1000);
        }
    }
}

I expected the program to keep trying to send the request each second until it's finnaly sent.

If I run the program with internet connected everything works fine.

If I disconnect from the internet and then run the program, I get a

java.net.UnknownHostException: www.google.com
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:178)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:618)
at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:333)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:123)
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:147)
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:108)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:415)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:641)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:576)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:554)
at com.google.api.client.http.apache.ApacheHttpRequest.execute(ApacheHttpRequest.java:67)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:965)
at test.client.MyClass.send(MyClass.java:112)
at test.client.MyClass.main(MyClass.java:124)

as expected.

The problem is that I keep getting this exception even after I reconnect to the internet.

Any solutions? Thanks

========================== UPDATE ==========================

The problem doesn't seem to be relate to the ApacheLibrary as it also happens when using the NetHttpTransport implementation.

Just replaced new ApacheHttpTransport(); by new NetHttpTransport(); on the code above generating a new looping stack trace:

java.net.UnknownHostException: www.google.com
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:178)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:618)
at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:275)
at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:371)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:932)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:153)
at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:93)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:965)
at test.client.MyClass.send(MyClass.java:112)
at test.client.MyClass.main(MyClass.java:124)

Also I'm using a Fedora 20 Linux O.S. Kernel 3.16.6-200.

========================== UPDATE ==========================

Followed RealSkeptic's suggestion and debugged the code to find out where and how the DNS lookup is done. I'm not familiar with java's low levew connection stuff, but I was able to produce the following almost equivalent code for the send function:

private static void send() throws IOException {
    URL connUrl = new URL("https://www.google.com/");
    URLConnection conn = connUrl.openConnection();
    HttpURLConnection connection = (HttpURLConnection) conn;
    connection.setRequestMethod("GET");

    connection.setInstanceFollowRedirects(false);

    connection.addRequestProperty("Accept-Encoding", "gzip");
    connection.addRequestProperty("User-Agent",
            "Google-HTTP-Java-Client/1.19.0 (gzip)");
    connection.setReadTimeout(20000);
    connection.setConnectTimeout(20000);

    boolean successfulConnection = false;
    try {
        connection.connect();
        //NetHttpResponse response = new NetHttpResponse(connection);
        successfulConnection = true;
    } finally {
        if (!successfulConnection) {
            connection.disconnect();
        }
    }
}

It's lacking on the response treatment.

With the code above, I keep getting the same result: A looping stacktrace that never ends.

Does anyone knows how to solve that?

Some enviroment information:

$cat /etc/resolv.conf
# Generated by NetworkManager
domain velox.com.br
search velox.com.br
nameserver 208.67.222.222
nameserver 8.8.8.8

$ java -version
java version "1.7.0_71"
OpenJDK Runtime Environment (fedora-2.5.3.0.fc20-x86_64 u71-b14)
OpenJDK 64-Bit Server VM (build 24.65-b04, mixed mode)
ylima
  • 410
  • 5
  • 17
  • Do you keep getting the exception (after reconnecting to the internet) even if you stop the program and start it again, or only if the program keeps running while you're starting the internet connection? – RealSkeptic Feb 01 '15 at 16:01
  • Just when the program keeps running after I start the internet. – ylima Feb 01 '15 at 16:03
  • OK, next question: how long have you waited for it to change? I see your pause is 1000ms. Did you wait for it to change for more than 10 seconds? – RealSkeptic Feb 01 '15 at 16:06
  • Well, I'm actually still waiting since a couple of minutes before I wrote this question. Same stacktrace looping on the console... – ylima Feb 01 '15 at 16:11
  • 1
    Can you print the value of `java.security.Security.getProperty("networkaddress.cache.negative.ttl");`? – RealSkeptic Feb 01 '15 at 16:22
  • *System.out.println(java.security.Security.getProperty("networkaddress.cache.negative.ttl"));* >> 10 – ylima Feb 01 '15 at 16:27
  • Can you add the complete stack trace? I gather that the library is not using Java's DNS lookup mechanism, then. – RealSkeptic Feb 01 '15 at 17:44
  • Updated question with full stack trace – ylima Feb 01 '15 at 18:01
  • I'm a bit stuck at this point, as I tried to find through source code where the actual DNS lookup is done and got lost. If you can find that out through the debugger it may shed more light - I suspect somewhere in either Google's code or Apache's, they are using a custom DNS lookup. – RealSkeptic Feb 03 '15 at 18:05
  • OK, I'll try to fins it out – ylima Feb 03 '15 at 18:09
  • Are you on a direct internet connection? It is likely that these libraries don't use your system configured proxy automatically. – Thirler Feb 04 '15 at 12:25
  • Yes, I am on a direct internet connection. The program also works just fine when started connected to the internet. So I don't think that the problem may be related to any internet connection configuration. – ylima Feb 04 '15 at 12:35
  • @RealSkeptic does the new information above give you any clue? – ylima Feb 05 '15 at 19:40
  • Now that you changed it to pure standard library stuff I was able to test it on one of my machines, and unfortunately, as soon as I reconnected the network, the loop stopped as it should. It must be something local on your side. Things to look at: what is in your `resolv.conf`? Were you only testing it inside your IDE or also directly with standard JRE? Which Java are you using? – RealSkeptic Feb 05 '15 at 21:09
  • I've tried to run the applicatio throught command line without any IDE either: Same result. I also provided some enviroment information on the question. Any ideas? – ylima Feb 05 '15 at 23:45

0 Answers0