How to set the timeout with BufferedReader according to urlconnection in Java?
•
Java
I want to read the content of the URL, but I don't want to "hang" if the URL doesn't respond I have created a BufferedReader using the URL
URL theURL = new URL(url); URLConnection urlConn = theURL.openConnection(); urlConn.setDoOutput(true); BufferedReader urlReader = new BufferedReader(newInputStreamReader(urlConn.getInputStream()));
... and start looping through the content
do { buf = urlReader.readLine(); if (buf != null) { resultBuffer.append(buf); resultBuffer.append("\n"); } } while (buf != null);
... but if reading is suspended, the application is suspended
Is there a way to not grind code to the socket level and "timeout" reading if necessary?
Solution
I think urlconnection Setreadtimeout is what you are looking for
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码