I am trying to read the url in java and it works as long as the url loads in the browser.
But if it just loops in the browser and doesn't load this page when I try to open it in my browser, my java application just freezes, it will probably wait forever if there is enough time. How to set a timeout on something or something if it takes more than 20 seconds to stop the application?
I am using URL
Here is the important part of the code:
URL url = null; String inputLine; try { url = new URL(surl); } catch (MalformedURLException e) { e.printStackTrace(); } BufferedReader in; try { in = new BufferedReader(new InputStreamReader(url.openStream())); while ((inputLine = in.readLine()) != null) { System.out.println(inputLine); } in.close(); } catch (IOException e) { e.printStackTrace(); }
source share