【问题标题】:HttpsURLConnection: Connection Timed out errorHttpsURLConnection:连接超时错误
【发布时间】:2013-05-29 13:06:34
【问题描述】:

我有一个简单的代码,用于建立与谷歌的 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());
}
}

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

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)

谁能指导我。从早上起我就一直在吃脑袋想弄明白。

【问题讨论】:

  • 检查您的防火墙设置,了解javajavaw 进程。您可以远程登录到同一站点上的 443 端口吗?
  • 你应该在完成后关闭连接。
  • Zeutheus:在命令提示符下使用 telnet 进行连接测试:telnet www.google.co.in 443
  • 我收到以下 telnet 错误:连接到 www.google.co.in...无法在端口 443 上打开与主机的连接:连接失败
  • 连接SSL通常需要证书,你确定不是这种情况?

标签: java https connectexception


【解决方案1】:

如果您在代理之后并遇到此异常,那么此解决方案将适合您。

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());
     }
}

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 有效吗?我的意思是,这不是一个很好的答案,但如果杀毒软件是造成这种情况的原因,那么很高兴知道。
    【解决方案3】:

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

    【讨论】:

    • 图片说得好...谢谢。
    【解决方案4】:

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

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

    这将帮助你做你所追求的:http://www.xinotes.org/notes/note/1088/

    【讨论】:

      【解决方案5】:

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

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

      【讨论】:

      • 没错,但即使这样也不应该超时。
      • 原来这段代码在我家工作得很好......可能是我的工作网络有问题......谢谢大家顺便说一句
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-30
      • 2017-07-12
      相关资源
      最近更新 更多