I'm writing some code in Java to dowload stuff from urls and in my configuration some downloads should be handled by a proxy and others without it.
So I wrote this code (it works) to download all URL types but and I'd like to reduce the delay time before a ConnectException is thrown so the code can execute faster.
URL global_url = new URL("http://google.com");
Scanner sc = null;
try {
sc = new Scanner(global_url.openStream());
}
catch (ConnectException e) {
try {
System.setProperty("http.proxyHost", "my.host");
System.setProperty("http.proxyPort", "my.port");
sc = new Scanner(global_url.openStream());
System.setProperty("http.proxyHost", "");
System.setProperty("http.proxyPort", "");
}
catch (ConnectException exc) {
//Do stuff
}
}
Right now it takes approx. 10s before the exception is thrown and I'd like to reduce this time to 2s or 3s max.
Could I get some help? Thanks !
You can set the timeout like this:
long connectTimeout = 3000;
URL global_url = new URL(urlPath);
URLConnection con = global_url.openConnection();
con.setConnectTimeout(connectTimeout);
where connectTimeout you can set as in milliseconds. As you need 3s timeout so set it as 3000.