我正在使用Google HTTP Client Library for Java为我的网络服务开发客户端应用程序。
问题是当我尝试运行这样的代码时:
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);
}
}
}
我希望程序每秒都会继续尝试发送请求,直到最终发送完毕。
如果我在连接互联网的情况下运行该程序,一切正常。
如果我断开互联网连接然后运行该程序,我会得到一个
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)
正如所料。
问题是,即使我重新连接到互联网,我仍然会收到此异常。
任何解决方案? 感谢
==========================更新==================== ==
问题似乎与ApacheLibrary无关,因为在使用NetHttpTransport实现时也会发生这种情况。
刚刚在上面的代码上用new ApacheHttpTransport();
替换了new NetHttpTransport();
,生成了一个新的循环堆栈跟踪:
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)
我也在使用Fedora 20 Linux O.S.内核3.16.6-200。
==========================更新==================== ==
遵循RealSkeptic的建议并调试代码以找出DNS查找的完成位置和方式。 我不熟悉java的低级连接,但我能够为send函数生成以下几乎相同的代码:
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();
}
}
}
缺乏应对治疗。
使用上面的代码,我会得到相同的结果:一个永远不会结束的循环堆栈跟踪。
有谁知道如何解决这个问题?
一些环境信息:
$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)