public interface

Connection

org.jsoup.Connection
Known Indirect Subclasses

Class Overview

A Connection provides a convenient interface to fetch content from the web, and parse them into Documents.

To get a new Connection, use connect(String). Connections contain Connection.Request and Connection.Response objects. The request objects are reusable as prototype requests.

Request configuration can be made using either the shortcut methods in Connection (e.g. userAgent(String)), or by methods in the Connection.Request object directly. All request configuration must be made before the request is executed.

The Connection interface is currently in beta and subject to change. Comments, suggestions, and bug reports are welcome.

Summary

Nested Classes
interface Connection.Base<T extends Base> Common methods for Requests and Responses 
interface Connection.KeyVal A Key Value tuple. 
enum Connection.Method GET and POST http methods. 
interface Connection.Request Represents a HTTP request. 
interface Connection.Response Represents a HTTP response. 
Public Methods
abstract Connection cookie(String name, String value)
Set a cookie to be sent in the request
abstract Connection data(String key, String value)
Add a request data parameter.
abstract Connection data(String... keyvals)
Add a number of request data parameters.
abstract Connection data(Map<StringString> data)
Adds all of the supplied data to the request data parameters
abstract Connection.Response execute()
Execute the request.
abstract Connection followRedirects(boolean followRedirects)
Configures the connection to (not) follow server redirects.
abstract Document get()
Execute the request as a GET, and parse the result.
abstract Connection header(String name, String value)
Set a request header.
abstract Connection method(Connection.Method method)
Set the request method to use, GET or POST.
abstract Document post()
Execute the request as a POST, and parse the result.
abstract Connection referrer(String referrer)
Set the request referrer (aka "referer") header.
abstract Connection request(Connection.Request request)
Set the connection's request
abstract Connection.Request request()
Get the request object associatated with this connection
abstract Connection response(Connection.Response response)
Set the conenction's response
abstract Connection.Response response()
Get the response, once the request has been executed
abstract Connection timeout(int millis)
Set the request timeouts (connect and read).
abstract Connection url(String url)
Set the request URL to fetch.
abstract Connection url(URL url)
Set the request URL to fetch.
abstract Connection userAgent(String userAgent)
Set the request user-agent header.

Public Methods

public abstract Connection cookie (String name, String value)

Set a cookie to be sent in the request

Parameters
name name of cookie
value value of cookie
Returns
  • this Connection, for chaining

public abstract Connection data (String key, String value)

Add a request data parameter. Request parameters are sent in the request query string for GETs, and in the request body for POSTs. A request may have multiple values of the same name.

Parameters
key data key
value data value
Returns
  • this Connection, for chaining

public abstract Connection data (String... keyvals)

Add a number of request data parameters. Multiple parameters may be set at once, e.g.: .data("name", "jsoup", "language", "Java", "language", "English"); creates a query string like: ?name=jsoup&language=Java&language=English

Parameters
keyvals a set of key value pairs.
Returns
  • this Connection, for chaining

public abstract Connection data (Map<StringString> data)

Adds all of the supplied data to the request data parameters

Parameters
data map of data parameters
Returns
  • this Connection, for chaining

public abstract Connection.Response execute ()

Execute the request.

Returns
  • a response object
Throws
IOException on error

public abstract Connection followRedirects (boolean followRedirects)

Configures the connection to (not) follow server redirects. By default this is true.

Parameters
followRedirects true if server redirects should be followed.
Returns
  • this Connection, for chaining

public abstract Document get ()

Execute the request as a GET, and parse the result.

Returns
  • parsed Document
Throws
IOException on error

public abstract Connection header (String name, String value)

Set a request header.

Parameters
name header name
value header value
Returns
  • this Connection, for chaining
See Also

public abstract Connection method (Connection.Method method)

Set the request method to use, GET or POST. Default is GET.

Parameters
method HTTP request method
Returns
  • this Connection, for chaining

public abstract Document post ()

Execute the request as a POST, and parse the result.

Returns
  • parsed Document
Throws
IOException on error

public abstract Connection referrer (String referrer)

Set the request referrer (aka "referer") header.

Parameters
referrer referrer to use
Returns
  • this Connection, for chaining

public abstract Connection request (Connection.Request request)

Set the connection's request

Parameters
request new request object
Returns
  • this Connection, for chaining

public abstract Connection.Request request ()

Get the request object associatated with this connection

Returns
  • request

public abstract Connection response (Connection.Response response)

Set the conenction's response

Parameters
response new response
Returns
  • this Connection, for chaining

public abstract Connection.Response response ()

Get the response, once the request has been executed

Returns
  • response

public abstract Connection timeout (int millis)

Set the request timeouts (connect and read). If a timeout occurs, an IOException will be thrown. The default timeout is 3 seconds (3000 millis). A timeout of zero is treated as an infinite timeout.

Parameters
millis number of milliseconds (thousandths of a second) before timing out connects or reads.
Returns
  • this Connection, for chaining

public abstract Connection url (String url)

Set the request URL to fetch. The protocol must be HTTP or HTTPS.

Parameters
url URL to connect to
Returns
  • this Connection, for chaining

public abstract Connection url (URL url)

Set the request URL to fetch. The protocol must be HTTP or HTTPS.

Parameters
url URL to connect to
Returns
  • this Connection, for chaining

public abstract Connection userAgent (String userAgent)

Set the request user-agent header.

Parameters
userAgent user-agent to use
Returns
  • this Connection, for chaining