Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds easy request session-state and corrects cookie domain and path rules #1476

Merged
merged 4 commits into from
Jan 20, 2021

Conversation

jhy
Copy link
Owner

@jhy jhy commented Jan 16, 2021

This extends the Connection implementation to support (optional) sessions, which allow request defaults (timeout, proxy, etc) to be set once and then applied to all requests within that session.

Cookies are re-implemented to correctly support path and domain filtering when used within a session. A default in-memory cookie store is used for the session, or a custom implementation (perhaps disk-persistent, or pre-set) can be used instead.

Forms submitted using the FormElement#submit() use the same session that was used to fetch the document and so pass cookies and other defaults appropriately.

The session is multi-thread safe and can execute multiple requests concurrently. If the user accidentally tries to execute the same request object across multiple threads (vs calling Connection#newRequest()), that is detected cleanly and a clear exception is thrown (vs weird blowups in input stream reading, or forcing everything through a synchronized bottleneck.

Examples:

Connection session = Jsoup.newSession()
     .timeout(20 * 1000)
     .userAgent("FooBar 2000");

Document doc1 = session.newRequest()
     .url("https://jsoup.org/").data("ref", "example")
     .get();
Document doc2 = session.newRequest()
     .url("https://en.wikipedia.org/wiki/Main_Page")
     .get();
Connection con3 = session.newRequest();

@jhy jhy added this to the 1.14.1 milestone Jan 16, 2021
@jhy jhy merged commit b431fb2 into master Jan 20, 2021
@jhy
Copy link
Owner Author

jhy commented Jan 20, 2021

(Reminder, need to add details to the changelog, particularly a change note for those who may have implemented Connection directly, vs extending HttpConnection)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

cookies with duplicate names not properly sent FormElement#submit() and cookies
1 participant