简体   繁体   中英

Java URL Connection Time Out

I am attempting to connect to a website where I'd like to extract its HTML contents. My application will never connect to the site - only time out.

Here is my code:

URL url = new URL("www.website.com");
URLConnection connection = url.openConnection();
connection.setConnectTimeout(2000);
connection.setReadTimeOut(2000);
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream());
String line;

while ((line = reader.readLine()) != null) {
  // do stuff with line
}

reader.close();

Any ideas would be greatly appreciated. Thanks!

I believe the url should be (ie. you need a protocol):

URL url = new URL("http://www.website.com"); 

If that doesn't help then post your real SSCCE that demonstrates the problem so we don't have to guess what you are really doing because we can't tell if you are using your try/catch block correctly or if you are just ignoring exceptions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM