HttpsURLConnection: Connection Timed out error

自古美人都是妖i 提交于 2019-11-28 14:03:38

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.

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) {
   //........
}

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/

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

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());
     }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!