I am trying to load the contents of a page using JSoup. If the whole operation (open connection + read) takes more than 8 seconds, I want to cancel immediately. I suggested that the purpose of the method timeout(int millis)does just that. According to javadoc:
Set request timeouts (connect and read). If a timeout occurs, an IOException will be thrown. The default timeout is 3 seconds (3000 Millis). A zero timeout is considered an infinite timeout.
I wrote simple code that mimics this operation:
final int TIME_OUT = 8000;
final String USER_AGENT_STRING = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)";
final String url = "http://reguler-pmb-tanggamus.va.web.id/";
long time = System.currentTimeMillis();
try {
Document doc = Jsoup.connect(url).userAgent(USER_AGENT_STRING).timeout(TIME_OUT).get();
System.out.println("Done crawling " + url + ", took " + (System.currentTimeMillis() - time) + " millis");
System.out.println("Content: " + doc);
} catch (Exception e) {
System.out.println("Failed after " + (System.currentTimeMillis() - time) + " millis");
e.printStackTrace();
}
script "" .
, , 8 (8000 ).
, , ( ) :
Done crawling http:
Content: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> ...
( ) fail (SocketTimeoutException) .
- ?