I am trying to download a website using HttpURLConnection. I created the connection using urlConnection = (HttpURLConnection) url.openConnection() and then I set up the timeout using urlConnection.setReadTimeout(Timeout) and urlConnection.setConnectTimeout(Timeout). The problem is when I try to execute stream = urlConnection.getInputStream(). If you read the code below, you will see that I am counting the time before/after the getInputStream(). The time is larger than the TIMEOUT value I used for the urlConnection.setReadTimeout(Timeout) and urlConnection.setConnectTimeout(Timeout). I created this application as a Thread and I have cases where the delay is more than 5 seconds!
My problem is that the TIMEOUT is not working in the way I am expecting.
Any ideas how to be sure that the getInputStream() instruction timeout after a time that I set ?
Thanks!
Gustavo
// Additional variables
URL url=null;
HttpURLConnection urlConnection=null;
BufferedReader in = null;
InputStream stream;
String s;
try {
if (Port == 0){
url = new URL(Address);
}else{
url = new URL(Address+":"+String.valueOf(Port));
}
urlConnection = (HttpURLConnection) url.openConnection();
// Set timeout
urlConnection.setReadTimeout(Timeout);
urlConnection.setConnectTimeout(Timeout);
long start2 = System.currentTimeMillis();
stream = urlConnection.getInputStream();
long start3 = System.currentTimeMillis();
long res = start3-start2;
System.out.println("STREAM:"+res);
System.out.println("CONNECTTIMEOUT:"+urlConnection.getConnectTimeout());
System.out.println("READTIMEOUT:"+urlConnection.getReadTimeout());
in = new BufferedReader(new InputStreamReader(stream));
TmpPage = "";
while ((s = in.readLine()) != null) {
//System.out.println(s);
TmpPage = TmpPage + s;
}
PageSize = TmpPage.length();
} catch ... etc.etc.etc.