public interface Connection
To get a new Connection, use Jsoup.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.
Modifier and Type | Interface and Description |
---|---|
static interface |
Connection.Base<T extends Connection.Base>
Common methods for Requests and Responses
|
static interface |
Connection.KeyVal
A Key:Value tuple(+), used for form data.
|
static class |
Connection.Method
GET and POST http methods.
|
static interface |
Connection.Request
Represents a HTTP request.
|
static interface |
Connection.Response
Represents a HTTP response.
|
Modifier and Type | Method and Description |
---|---|
Connection |
cookie(java.lang.String name,
java.lang.String value)
Set a cookie to be sent in the request.
|
Connection |
cookies(java.util.Map<java.lang.String,java.lang.String> cookies)
Adds each of the supplied cookies to the request.
|
Connection |
data(java.util.Collection<Connection.KeyVal> data)
Adds all of the supplied data to the request data parameters
|
Connection |
data(java.util.Map<java.lang.String,java.lang.String> data)
Adds all of the supplied data to the request data parameters
|
Connection |
data(java.lang.String... keyvals)
Add a number of request data parameters.
|
Connection.KeyVal |
data(java.lang.String key)
Get the data KeyVal for this key, if any
|
Connection |
data(java.lang.String key,
java.lang.String value)
Add a request data parameter.
|
Connection |
data(java.lang.String key,
java.lang.String filename,
java.io.InputStream inputStream)
Add an input stream as a request data parameter.
|
Connection |
data(java.lang.String key,
java.lang.String filename,
java.io.InputStream inputStream,
java.lang.String contentType)
Add an input stream as a request data parameter.
|
Connection.Response |
execute()
Execute the request.
|
Connection |
followRedirects(boolean followRedirects)
Configures the connection to (not) follow server redirects.
|
Document |
get()
Execute the request as a GET, and parse the result.
|
Connection |
header(java.lang.String name,
java.lang.String value)
Set a request header.
|
Connection |
headers(java.util.Map<java.lang.String,java.lang.String> headers)
Adds each of the supplied headers to the request.
|
Connection |
ignoreContentType(boolean ignoreContentType)
Ignore the document's Content-Type when parsing the response.
|
Connection |
ignoreHttpErrors(boolean ignoreHttpErrors)
Configures the connection to not throw exceptions when a HTTP error occurs.
|
Connection |
maxBodySize(int bytes)
Set the maximum bytes to read from the (uncompressed) connection into the body, before the connection is closed,
and the input truncated.
|
Connection |
method(Connection.Method method)
Set the request method to use, GET or POST.
|
Connection |
parser(Parser parser)
Provide an alternate parser to use when parsing the response to a Document.
|
Document |
post()
Execute the request as a POST, and parse the result.
|
Connection |
postDataCharset(java.lang.String charset)
Sets the default post data character set for x-www-form-urlencoded post data
|
Connection |
proxy(java.net.Proxy proxy)
Set the proxy to use for this request.
|
Connection |
proxy(java.lang.String host,
int port)
Set the HTTP proxy to use for this request.
|
Connection |
referrer(java.lang.String referrer)
Set the request referrer (aka "referer") header.
|
Connection.Request |
request()
Get the request object associated with this connection
|
Connection |
request(Connection.Request request)
Set the connection's request
|
Connection |
requestBody(java.lang.String body)
Set a POST (or PUT) request body.
|
Connection.Response |
response()
Get the response, once the request has been executed
|
Connection |
response(Connection.Response response)
Set the connection's response
|
Connection |
sslSocketFactory(javax.net.ssl.SSLSocketFactory sslSocketFactory)
Set custom SSL socket factory
|
Connection |
timeout(int millis)
Set the total request timeout duration.
|
Connection |
url(java.lang.String url)
Set the request URL to fetch.
|
Connection |
url(java.net.URL url)
Set the request URL to fetch.
|
Connection |
userAgent(java.lang.String userAgent)
Set the request user-agent header.
|
Connection |
validateTLSCertificates(boolean value)
Deprecated.
as distributions (specifically Google Play) are starting to show warnings if these checks are
disabled.
|
Connection url(java.net.URL url)
url
- URL to connect toConnection url(java.lang.String url)
url
- URL to connect toConnection proxy(java.net.Proxy proxy)
null
to disable.proxy
- proxy to useConnection proxy(java.lang.String host, int port)
host
- the proxy hostnameport
- the proxy portConnection userAgent(java.lang.String userAgent)
userAgent
- user-agent to useHttpConnection.DEFAULT_UA
Connection timeout(int millis)
SocketTimeoutException
will be thrown.
The default timeout is 30 seconds (30,000 millis). A timeout of zero is treated as an infinite timeout.
Note that this timeout specifies the combined maximum duration of the connection time and the time to read the full response.
millis
- number of milliseconds (thousandths of a second) before timing out connects or reads.maxBodySize(int)
Connection maxBodySize(int bytes)
bytes
- number of bytes to read from the input before truncatingConnection referrer(java.lang.String referrer)
referrer
- referrer to useConnection followRedirects(boolean followRedirects)
followRedirects
- true if server redirects should be followed.Connection method(Connection.Method method)
method
- HTTP request methodConnection ignoreHttpErrors(boolean ignoreHttpErrors)
ignoreHttpErrors
- - false (default) if HTTP errors should be ignored.Connection ignoreContentType(boolean ignoreContentType)
ignoreContentType
- set to true if you would like the content type ignored on parsing the response into a
Document.Connection validateTLSCertificates(boolean value)
By default this is true; all connections over HTTPS perform normal validation of certificates, and will abort requests if the provided certificate does not validate.
Some servers use expired, self-generated certificates; or your JDK may not support SNI hosts. In which case, you may want to enable this setting.
Be careful and understand why you need to disable these validations.
value
- if should validate TLS (SSL) certificates. true by default.Connection sslSocketFactory(javax.net.ssl.SSLSocketFactory sslSocketFactory)
sslSocketFactory
- custom SSL socket factoryConnection data(java.lang.String key, java.lang.String value)
key
- data keyvalue
- data valueConnection data(java.lang.String key, java.lang.String filename, java.io.InputStream inputStream)
key
- data key (form item name)filename
- the name of the file to present to the remove server. Typically just the name, not path,
component.inputStream
- the input stream to upload, that you probably obtained from a FileInputStream
.
You must close the InputStream in a finally
block.if you want to set the uploaded file's mimetype.
Connection data(java.lang.String key, java.lang.String filename, java.io.InputStream inputStream, java.lang.String contentType)
key
- data key (form item name)filename
- the name of the file to present to the remove server. Typically just the name, not path,
component.inputStream
- the input stream to upload, that you probably obtained from a FileInputStream
.contentType
- the Content Type (aka mimetype) to specify for this file.
You must close the InputStream in a finally
block.Connection data(java.util.Collection<Connection.KeyVal> data)
data
- collection of data parametersConnection data(java.util.Map<java.lang.String,java.lang.String> data)
data
- map of data parametersConnection data(java.lang.String... keyvals)
.data("name",
"jsoup", "language", "Java", "language", "English");
creates a query string like:
?name=jsoup&language=Java&language=English
keyvals
- a set of key value pairs.Connection.KeyVal data(java.lang.String key)
key
- the data keyConnection requestBody(java.lang.String body)
Jsoup.connect(url)
.requestBody(json)
.header("Content-Type", "application/json")
.post();
If any data key/vals are supplied, they will be sent as URL query params.Connection header(java.lang.String name, java.lang.String value)
name
- header namevalue
- header valueConnection.Base.headers()
Connection headers(java.util.Map<java.lang.String,java.lang.String> headers)
headers
- map of headers name -> value pairsConnection.Base.headers()
Connection cookie(java.lang.String name, java.lang.String value)
name
- name of cookievalue
- value of cookieConnection cookies(java.util.Map<java.lang.String,java.lang.String> cookies)
cookies
- map of cookie name -> value pairsConnection parser(Parser parser)
parser
- alternate parserConnection postDataCharset(java.lang.String charset)
charset
- character set to encode post dataDocument get() throws java.io.IOException
java.net.MalformedURLException
- if the request URL is not a HTTP or HTTPS URL, or is otherwise malformedHttpStatusException
- if the response is not OK and HTTP response errors are not ignoredUnsupportedMimeTypeException
- if the response mime type is not supported and those errors are not ignoredjava.net.SocketTimeoutException
- if the connection times outjava.io.IOException
- on errorDocument post() throws java.io.IOException
java.net.MalformedURLException
- if the request URL is not a HTTP or HTTPS URL, or is otherwise malformedHttpStatusException
- if the response is not OK and HTTP response errors are not ignoredUnsupportedMimeTypeException
- if the response mime type is not supported and those errors are not ignoredjava.net.SocketTimeoutException
- if the connection times outjava.io.IOException
- on errorConnection.Response execute() throws java.io.IOException
java.net.MalformedURLException
- if the request URL is not a HTTP or HTTPS URL, or is otherwise malformedHttpStatusException
- if the response is not OK and HTTP response errors are not ignoredUnsupportedMimeTypeException
- if the response mime type is not supported and those errors are not ignoredjava.net.SocketTimeoutException
- if the connection times outjava.io.IOException
- on errorConnection.Request request()
Connection request(Connection.Request request)
request
- new request objectConnection.Response response()
Connection response(Connection.Response response)
response
- new response