I am trying to connect to the internet with the user defined proxy settings. I have set the setReadTimeout
to 5 seconds. If the configured proxy is not correct, then we wont be able to connect to internet and i am using the following code, but the read time out is not at all happening.
URL u = new URL("http://www.google.com/");
System.out.println("Checking internet connection availability.....");
System.setProperty("java.net.useSystemProxies", "true");
System.setProperty("http.proxyHost", proxyHost);
System.setProperty("http.proxyPort", proxyPort);
HttpURLConnection uc = (HttpURLConnection) u.openConnection();
uc.setReadTimeout(5000);
System.out.println("Response code : " + uc.getResponseCode());
System.out.println("Internet connection is available.....");
If i run the above code, then program is keep on executing and not getting terminated in 5 seconds.
Can anybody help me out here about the problem in my code ?
Thanks in advance.
Try also adding uc.setConnectTimeout(5000);
Edit: Final solution
Use uc.connect();
before getting the response code.
Also, add the proxy configuration while opening the HttpURLConnection.
Like this :
HttpURLConnection uc = (HttpURLConnection) u.openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(myProxyHost, Integer.parseInt(myProxyPort))));