Reputation: 97
Basically I am trying to download a HTML content of a webpage. The method is very simple
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
BufferReader in = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
String line;
StringBuilder pageBuilder = new StringBuilder();
while ((line = in.readLine()) != null) {
pageBuilder.append(line + "\n");
}
But sometimes the program just hangs. I tried to debug and the thread stack trace tells me that it hangs when calling to method SocketRead0. I tried to set readtimeout for the connection but it does not work. Any ideas on how can I detect and get through the block of SocketRead0 ?
Edit: It seems that the problem I actually have is that getReponseCode implicitly calls some getInputStream and some read() methods and then hangs at readSocket0(). Is there anyway I can make sure that the call to getReponseCode() will be safe ? Here is the stack trace of one thread hanging: Level 0 is the most recent call
thread 24stacktrace
At 0level
at method socketRead0
at line -2
At 1level
at method read
at line 129
At 2level
at method fill
at line 218
At 3level
at method read1
at line 258
At 4level
at method read
at line 317
At 5level
at method parseHTTPHeader
at line 687
At 6level
at method parseHTTP
at line 632
At 7level
at method getInputStream
at line 1200
At 8level
at method getResponseCode
at line 379
At 9level
at method pushFinalRedirectedURL
at line 132
At 10level
at method process
at line 134
At 11level
at method run
at line 40
Upvotes: 1
Views: 6654
Reputation: 36746
I have this error too and solved it. This problem occurs because sometimes the software try to open a connection to a server that don't send an response, but don't give a error too.
The software still waiting the server answer, but it never comes.
To avoid this, you need to use the setConnectTimeout()
method, so if the server don't send a answer in determined time, the connection will be aborted.
Upvotes: 2
Reputation: 7467
Just an addition to the previous answers about socket read being blocking:
if method private native int socketRead0(FileDescriptor fd, byte b[], int off, int len, int timeout) throws IOException
gets the timeout 0 (the default), no timeout is used. Hence it can block instead of throwing an IOException.
Upvotes: 1
Reputation: 624
You need to make sure your buffer has data to be read before you call the readline function. As Peter mentioned, SocketRead is a blocking function which means when it is called it will sit and wait until data is placed on the stream.
Try this:
while (in.ready()) {
line = in.readLine();
pageBuilder.append(line + "\n");
}
Here is the link to the BufferedReader API.
Upvotes: 2
Reputation: 533820
socket read is a blocking operation. It will block until there is more data, the end of stream is reached or the connection is closed.
Upvotes: 2