JSoup timeout not working properly

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://reguler-pmb-tanggamus.va.web.id/, took 68215 millis
Content: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> ...

( ) fail (SocketTimeoutException) .

- ?

+4
3

, , ​​Jsoup 1.8.3.

. @github.com/jhy/jsoup/issues (luksch)

​​OP: https://github.com/jhy/jsoup/issues/628

+1

JSoup (jhy) :

. . , , < 8 , -.

, , ( ), .

, ​​ .

0
/**
 * Set the maximum bytes to read from the (uncompressed) connection into the body, before the connection is closed,
 * and the input truncated. The default maximum is 1MB. A max size of zero is treated as an infinite amount (bounded
 * only by your patience and the memory available on your machine).
 * @param bytes number of bytes to read from the input before truncating
 * @return this Connection, for chaining
 */
Connection maxBodySize(int bytes);

recv jsoup 1

"Jsoup.connect(url).maxBodySize(0);" , !

-1

Source: https://habr.com/ru/post/1608202/


All Articles