public class HttpConnection extends java.lang.Object implements Connection
Connection.Jsoup.connect(String)| Modifier and Type | Class and Description |
|---|---|
private static class |
HttpConnection.Base<T extends Connection.Base<T>> |
static class |
HttpConnection.KeyVal |
static class |
HttpConnection.Request |
static class |
HttpConnection.Response |
Connection.Method| Modifier and Type | Field and Description |
|---|---|
static java.lang.String |
CONTENT_ENCODING |
static java.lang.String |
CONTENT_TYPE |
static java.lang.String |
DEFAULT_UA
Many users would get caught by not setting a user-agent and therefore getting different responses on their desktop
vs in jsoup, which would otherwise default to
Java. |
private static java.lang.String |
DefaultUploadType |
static java.lang.String |
FORM_URL_ENCODED |
private static int |
HTTP_TEMP_REDIR |
private static java.nio.charset.Charset |
ISO_8859_1 |
static java.lang.String |
MULTIPART_FORM_DATA |
private HttpConnection.Request |
req |
private Connection.Response |
res |
private static java.lang.String |
USER_AGENT |
| Modifier | Constructor and Description |
|---|---|
|
HttpConnection()
Create a new, empty HttpConnection.
|
(package private) |
HttpConnection(HttpConnection.Request copy)
Create a new Request by deep-copying an existing Request.
|
private |
HttpConnection(HttpConnection.Request req,
HttpConnection.Response res)
Create a new Connection that just wraps the provided Request and Response
|
| Modifier and Type | Method and Description |
|---|---|
static Connection |
connect(java.lang.String url)
Create a new Connection, with the request URL specified.
|
static Connection |
connect(java.net.URL url)
Create a new Connection, with the request URL specified.
|
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.
|
java.net.CookieStore |
cookieStore()
Get the cookie store used by this Connection.
|
Connection |
cookieStore(java.net.CookieStore cookieStore)
Provide a custom or pre-filled CookieStore to be used on requests made by this Connection.
|
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 one or more request
key, val data parameter pairs. |
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.
|
private static java.lang.String |
encodeMimeName(java.lang.String val) |
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 (i.e.
|
Connection |
method(Connection.Method method)
Set the request method to use, GET or POST.
|
private static boolean |
needsMultipart(Connection.Request req) |
Connection |
newRequest()
Creates a new request, using this Connection as the session-state and to initialize the connection settings (which may then be independently on the returned Connection.Request object).
|
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.
|
public static final java.lang.String CONTENT_ENCODING
public static final java.lang.String DEFAULT_UA
Java. So by default, use a desktop UA.private static final java.lang.String USER_AGENT
public static final java.lang.String CONTENT_TYPE
public static final java.lang.String MULTIPART_FORM_DATA
public static final java.lang.String FORM_URL_ENCODED
private static final int HTTP_TEMP_REDIR
private static final java.lang.String DefaultUploadType
private static final java.nio.charset.Charset ISO_8859_1
private HttpConnection.Request req
@Nullable private Connection.Response res
public HttpConnection()
HttpConnection(HttpConnection.Request copy)
copy - the request to copyprivate HttpConnection(HttpConnection.Request req, HttpConnection.Response res)
public static Connection connect(java.lang.String url)
url - the URL to fetch frompublic static Connection connect(java.net.URL url)
url - the URL to fetch fromprivate static java.lang.String encodeMimeName(java.lang.String val)
public Connection newRequest()
ConnectionnewRequest in interface Connectionpublic Connection url(java.net.URL url)
Connectionurl in interface Connectionurl - URL to connect topublic Connection url(java.lang.String url)
Connectionurl in interface Connectionurl - URL to connect topublic Connection proxy(@Nullable java.net.Proxy proxy)
Connectionnull to disable a previously set proxy.proxy in interface Connectionproxy - proxy to usepublic Connection proxy(java.lang.String host, int port)
Connectionproxy in interface Connectionhost - the proxy hostnameport - the proxy portpublic Connection userAgent(java.lang.String userAgent)
ConnectionuserAgent in interface ConnectionuserAgent - user-agent to useDEFAULT_UApublic Connection timeout(int millis)
ConnectionSocketTimeoutException 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.
timeout in interface Connectionmillis - number of milliseconds (thousandths of a second) before timing out connects or reads.Connection.maxBodySize(int)public Connection maxBodySize(int bytes)
Connection0 is treated as an infinite amount (bounded only by your patience and the memory available on your
machine).maxBodySize in interface Connectionbytes - number of bytes to read from the input before truncatingpublic Connection followRedirects(boolean followRedirects)
ConnectionfollowRedirects in interface ConnectionfollowRedirects - true if server redirects should be followed.public Connection referrer(java.lang.String referrer)
Connectionreferrer in interface Connectionreferrer - referrer to usepublic Connection method(Connection.Method method)
Connectionmethod in interface Connectionmethod - HTTP request methodpublic Connection ignoreHttpErrors(boolean ignoreHttpErrors)
ConnectionignoreHttpErrors in interface ConnectionignoreHttpErrors - - false (default) if HTTP errors should be ignored.public Connection ignoreContentType(boolean ignoreContentType)
ConnectionignoreContentType in interface ConnectionignoreContentType - set to true if you would like the content type ignored on parsing the response into a
Document.public Connection data(java.lang.String key, java.lang.String value)
Connectiondata in interface Connectionkey - data keyvalue - data valuepublic Connection sslSocketFactory(javax.net.ssl.SSLSocketFactory sslSocketFactory)
ConnectionsslSocketFactory in interface ConnectionsslSocketFactory - custom SSL socket factorypublic Connection data(java.lang.String key, java.lang.String filename, java.io.InputStream inputStream)
Connectiondata in interface Connectionkey - 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.public Connection data(java.lang.String key, java.lang.String filename, java.io.InputStream inputStream, java.lang.String contentType)
Connectiondata in interface Connectionkey - 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.public Connection data(java.util.Map<java.lang.String,java.lang.String> data)
Connectiondata in interface Connectiondata - map of data parameterspublic Connection data(java.lang.String... keyvals)
Connectionkey, val data parameter pairs.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
For GET requests, data parameters will be sent on the request query string. For POST (and other methods that
contain a body), they will be sent as body form parameters, unless the body is explicitly set by Connection.requestBody(String), in which case they will be query string parameters.
data in interface Connectionkeyvals - a set of key value pairs.public Connection data(java.util.Collection<Connection.KeyVal> data)
Connectiondata in interface Connectiondata - collection of data parameterspublic Connection.KeyVal data(java.lang.String key)
Connectiondata in interface Connectionkey - the data keypublic Connection requestBody(java.lang.String body)
ConnectionJsoup.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.requestBody in interface Connectionpublic Connection header(java.lang.String name, java.lang.String value)
Connectionheader in interface Connectionname - header namevalue - header valueConnection.Base.headers()public Connection headers(java.util.Map<java.lang.String,java.lang.String> headers)
Connectionheaders in interface Connectionheaders - map of headers name -> value pairsConnection.Base.headers()public Connection cookie(java.lang.String name, java.lang.String value)
Connectioncookie in interface Connectionname - name of cookievalue - value of cookiepublic Connection cookies(java.util.Map<java.lang.String,java.lang.String> cookies)
Connectioncookies in interface Connectioncookies - map of cookie name -> value pairspublic Connection cookieStore(java.net.CookieStore cookieStore)
ConnectioncookieStore in interface ConnectioncookieStore - a cookie store to use for subsequent requestspublic java.net.CookieStore cookieStore()
ConnectioncookieStore in interface Connectionpublic Connection parser(Parser parser)
Connectionparser in interface Connectionparser - alternate parserpublic Document get() throws java.io.IOException
Connectionget in interface Connectionjava.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 errorpublic Document post() throws java.io.IOException
Connectionpost in interface Connectionjava.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 errorpublic Connection.Response execute() throws java.io.IOException
Connectionexecute in interface Connectionjava.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 errorpublic Connection.Request request()
Connectionrequest in interface Connectionpublic Connection request(Connection.Request request)
Connectionrequest in interface Connectionrequest - new request objectpublic Connection.Response response()
Connectionresponse in interface Connectionpublic Connection response(Connection.Response response)
Connectionresponse in interface Connectionresponse - new responsepublic Connection postDataCharset(java.lang.String charset)
ConnectionpostDataCharset in interface Connectioncharset - character set to encode post dataprivate static boolean needsMultipart(Connection.Request req)