HttpsURLConnection: Connection Timed out error

2020-02-07 04:22发布

问题:

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

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.

回答1:

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.



回答2:

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

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


回答3:

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.

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.

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



回答4:

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



回答5:

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