HttpsURLConnection: Connection Timed out error

前端 未结 5 1841
情深已故
情深已故 2020-12-07 04:21

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;
         


        
5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-07 04:43

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

提交回复
热议问题