Languages
[Edit]
EN

Java Jsoup - Connection.Request how to set size of response body to bigger one?

13 points
Created by:
Root-ssh
6457

1. Problem description

How do I change the size of Jsoup response?

When I get larger response Jsoup get only 1 MB of document or json.

Code example:

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;

public class JsoupMaxResponseSizeExample {

    public static void main(String[] args) throws IOException {

        String url = "http://localhost:8080/get-all-users";
        Document document = Jsoup.connect(url)
                .get();

        System.out.println(document);
        System.out.println(document.toString().length());
    }
}

2. Solution

Fix is to use .maxBodySize(0)

Jsoup.connect("http://localhost:8080/get-all-users")
    // FIX - this line set max response size without limits
    // the only limit is your machine 
	.maxBodySize(0)

Merged questions Full code example:

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;

public class JsoupMaxResponseSizeExampleFix {

    public static void main(String[] args) throws IOException {

        String url = "http://localhost:8080/get-all-users";
        Document document = Jsoup.connect(url)

                // FIX: A max size of zero is treated as an infinite amount
                // (bounded only by your patience and the memory available
                // on your machine).
                .maxBodySize(0)

                .get();

        System.out.println(document);
        System.out.println(document.toString().length());
    }
}

Merged questions

  1. How to fetch large json response with Jsoup?
  2. Jsoup can't download entire document - fix
  3. How do I get full document data with Jsoup connect?
Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.