3

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

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

4

5 回答 5

14

由于超时不是预期的行为,因此无法准确判断出了什么问题,即使发送格式不正确的请求也是如此。然而,这是我用来调试的一般过程。

脚步

于 2013-06-03T12:46:11.960 回答
2

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

BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line = null;
while((line = reader.readLine()) != null) {
   //........
}
于 2013-06-03T11:43:18.250 回答
2

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

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

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

于 2013-06-03T11:43:19.590 回答
0

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

于 2015-04-17T13:37:40.183 回答
0

如果您在代理后面并遇到此异常,那么此解决方案将为您工作。

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());
     }
}
于 2018-05-14T11:53:27.807 回答