Gandalf StormCrow Gandalf StormCrow - 1 year ago 147
Java Question

Read from URL java

I'm trying to read URL in java, and it works as long as the URL is loading in browser.

But if it is just cylcing in the browser and not loading that page when I'm trying to open it in my browser, my java app just hangs, it will probably wait forever given enough time. How do I set timeout on that or something, if its loading for more than 20 seconds that I stop my application?

I'm using URL

Here is a relevant part of the code :

URL url = null;
String inputLine;

try {
url = new URL(surl);
} catch (MalformedURLException e) {
BufferedReader in;
try {
in = new BufferedReader(new InputStreamReader(url.openStream()));
while ((inputLine = in.readLine()) != null) {

} catch (IOException e) {

Answer Source

I don't know how u are using the URL class. It would have been better if post a snippet. But here is a way that works for me. See if it helps in your case:

    URL url = new URL(urlPath);
    URLConnection con = url.openConnection();
    InputStream in = con.getInputStream();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download