java HttpsURLConnection:连接超时错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16895762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:18:36  来源:igfitidea点击:

HttpsURLConnection: Connection Timed out error

javahttpsconnectexception

提问by DanMatlin

I have a simple code for setting up a https connection to google and printing the response obtained.

我有一个简单的代码,用于设置到 google 的 https 连接并打印获得的响应。

import java.io.OutputStreamWriter;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;


public class SendCertReq 
{
public static void main(String[] args) throws Exception 
{
     URL url = new URL("https://www.google.co.in/");
     HttpsURLConnection conn = (HttpsURLConnection)url.openConnection();
     conn.setRequestMethod("GET");
     conn.setDoOutput(true);
     OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
     wr.close();
     System.out.println(conn.getResponseMessage());
}
}

I get the following error when I try to run it.

当我尝试运行它时出现以下错误。

Exception in thread "main" java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.security.ssl.SSLSocketImpl.connect(Unknown Source)
at sun.security.ssl.BaseSSLSocketImpl.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.<init>(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.New(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(Unknown Source)
at SendCertReq.main(SendCertReq.java:16)

Can anyone please guide me. I've been eating my head since morning trying to figure it out.

任何人都可以请指导我。从早上开始我就一直在吃我的头试图弄清楚。

回答by Anirudh Ramanathan

There is no way to tell exactly what is wrong since timing out is not expected behavior, even when sending a malformed request, the way you are. This is the general procedure I use to debug, however.

没有办法确切地说出什么是错误的,因为超时不是预期的行为,即使在发送格式错误的请求时也是如此。然而,这是我用来调试的一般过程。

Steps

脚步

回答by AlexR

You should not try to writeinto stream when you are performing HTTP GET. You should read from input stream instead:

当您执行 HTTP GET 时,您不应该尝试写入流。您应该改为从输入流中读取:

BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line = null;
while((line = reader.readLine()) != null) {
   //........
}

回答by Daniel S.

Either leave out conn.setDoOutput(true);or conn.setRequestMethod("GET");because these two statements are contradicting. GET does not allow output and output on the other side means you can't use GET as request method.

要么省略,conn.setDoOutput(true);要么conn.setRequestMethod("GET");因为这两个陈述相互矛盾。GET 不允许输出,而另一端的输出意味着您不能使用 GET 作为请求方法。

It seems that you are trying to fetch the certificate from the SSL layer of the HTTPS protocol. For this, you do not need to send anythig (hence doOutput is not needed). Instead, the information that you want to get is sent to you as part of the SSL handshake inside of the connection establishing code of the HttpsURLConnection, and the SSLSocket which is part of this.

您似乎正在尝试从 HTTPS 协议的 SSL 层获取证书。为此,您不需要发送任何内容(因此不需要 doOutput)。相反,您想要获取的信息作为 HttpsURLConnection 的连接建立代码内的 SSL 握手的一部分发送给您,并且 SSLSocket 是其中的一部分。

This will help you do what you are after: http://www.xinotes.org/notes/note/1088/

这将帮助你做你想做的事:http: //www.xinotes.org/notes/note/1088/

回答by Hung

I have same problems. When I turn off my Antivirut, I resolve this problem :).

我有同样的问题。当我关闭我的 Antivirut 时,我解决了这个问题:)。

回答by Yanish Pradhananga

If you are behind the proxy and faced this Exception then, this solution will work for you.

如果您在代理后面并遇到此异常,则此解决方案对您有用。

public class SendCertReq {
public static void main(String[] args) throws Exception {   
URL url = new URL("https://www.google.co.in/");
//Remember to Add proxy IP Address where 192.168.0.1 is my Proxy Address and Port is 8080.
// Change as per your proxy setting
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.168.0.1", 8080));
HttpsURLConnection conn = (HttpsURLConnection)url.openConnection(proxy);
conn.setRequestMethod("GET");
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.close();
System.out.println(conn.getResponseMessage());
     }
}