Skip to content
  • jsoup
  • News
  • Bugs
  • Discussion
  • Download
  • API Reference
  • Cookbook
  • Try jsoup
jsoup » Cookbook » Working with the web » Maintaining a request session

Maintaining a request session

Jul 11, 2024

Problem

You want to perform multiple HTTP requests using the same configuration, and retain cookies across these requests.

Solution

Use the Jsoup.newSession() method to create a new session, represented by the Connection interface:

// Create a new session with settings applied to all requests:
Connection session = Jsoup.newSession()  
    .timeout(45 * 1000)  
    .maxBodySize(5 * 1024 * 1024);  
  
// Make the first request:  
Document req1 = session.newRequest("https://example.com/auth")  
    .data("auth-code", "my-secret-token")  
    .post();  
  
// Make a following request with the same settings, and cookies set from req1:
Document req2 = session.newRequest("https://example.com/admin/")
    .get();

Description

The session created by newSession() supports making multiple requests with the same configuration. Any request-level settings applied on that session will be applied to each actual request.

Cookies set by responses to those requests will be kept in a cookie jar for use in later requests.

The newRequest(String url) method returns a Connection object that is pre-configured with the session settings, but those settings can be overridden for that specific request.

Sessions are thread-safe, meaning multiple threads can call newRequest() on the same session concurrently. Each request object should only be used by a single worker thread at once.

The session’s cookie store is accessible via Connection.cookieStore(). This is maintained in memory for the lifetime of the session. For longer sessions, you can save the cookie store to disk by serializing it.

Cookbook

Introduction

  1. Parsing and traversing a Document

Input

  1. Parse a document from a String
  2. Parsing a body fragment
  3. Load a Document from a URL
  4. Load a Document from a File
  5. Parse large documents efficiently with StreamParser

Extracting data

  1. Use DOM methods to navigate a document
  2. Use CSS selectors to find elements
  3. Use XPath selectors to find elements and nodes
  4. Extract attributes, text, and HTML from elements
  5. Working with relative and absolute URLs
  6. Example program: list links

Modifying data

  1. Set attribute values
  2. Set the HTML of an element
  3. Setting the text content of elements

Cleaning HTML

  1. Sanitize untrusted HTML (to prevent XSS)

Working with the web

  1. Maintaining a request session
jsoup HTML parser © 2009 - 2026 Jonathan Hedley