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)