org.jsoup.helper
Class HttpConnection

java.lang.Object
  extended by org.jsoup.helper.HttpConnection
All Implemented Interfaces:
Connection

public class HttpConnection
     
extends Object
implements Connection

Implementation of Connection.

See Also:
Jsoup.connect(String)

Nested Class Summary
static class HttpConnection.KeyVal
           
static class HttpConnection.Request
           
static class HttpConnection.Response
           
 
Nested classes/interfaces inherited from interface org.jsoup.Connection
Connection.Method
 
Method Summary
static Connection connect(String url)
           
static Connection connect(URL url)
           
 Connection cookie(String name, String value)
          Set a cookie to be sent in the request.
 Connection cookies(Map<String,String> cookies)
          Adds each of the supplied cookies to the request.
 Connection data(Collection<Connection.KeyVal> data)
          Adds all of the supplied data to the request data parameters
 Connection data(Map<String,String> data)
          Adds all of the supplied data to the request data parameters
 Connection data(String... keyvals)
          Add a number of request data parameters.
 Connection data(String key, String value)
          Add 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(String name, String value)
          Set a request header.
 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 referrer(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.Response response()
          Get the response, once the request has been executed
 Connection response(Connection.Response response)
          Set the connection's response
 Connection timeout(int millis)
          Set the request timeouts (connect and read).
 Connection url(String url)
          Set the request URL to fetch.
 Connection url(URL url)
          Set the request URL to fetch.
 Connection userAgent(String userAgent)
          Set the request user-agent header.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

connect

public static Connection connect(String url)

connect

public static Connection connect(URL url)

url

public Connection url(URL url)
Description copied from interface: Connection
Set the request URL to fetch. The protocol must be HTTP or HTTPS.

Specified by:
url in interface Connection
Parameters:
url - URL to connect to
Returns:
this Connection, for chaining

url

public Connection url(String url)
Description copied from interface: Connection
Set the request URL to fetch. The protocol must be HTTP or HTTPS.

Specified by:
url in interface Connection
Parameters:
url - URL to connect to
Returns:
this Connection, for chaining

userAgent

public Connection userAgent(String userAgent)
Description copied from interface: Connection
Set the request user-agent header.

Specified by:
userAgent in interface Connection
Parameters:
userAgent - user-agent to use
Returns:
this Connection, for chaining

timeout

public Connection timeout(int millis)
Description copied from interface: Connection
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.

Specified by:
timeout in interface Connection
Parameters:
millis - number of milliseconds (thousandths of a second) before timing out connects or reads.
Returns:
this Connection, for chaining

maxBodySize

public Connection maxBodySize(int bytes)
Description copied from interface: Connection
Set the maximum bytes to read from the (uncompressed) connection into the body, before the connection is closed, and the input truncated. The default maximum is 1MB. A max size of zero is treated as an infinite amount (bounded only by your patience and the memory available on your machine).

Specified by:
maxBodySize in interface Connection
Parameters:
bytes - number of bytes to read from the input before truncating
Returns:
this Connection, for chaining

followRedirects

public Connection followRedirects(boolean followRedirects)
Description copied from interface: Connection
Configures the connection to (not) follow server redirects. By default this is true.

Specified by:
followRedirects in interface Connection
Parameters:
followRedirects - true if server redirects should be followed.
Returns:
this Connection, for chaining

referrer

public Connection referrer(String referrer)
Description copied from interface: Connection
Set the request referrer (aka "referer") header.

Specified by:
referrer in interface Connection
Parameters:
referrer - referrer to use
Returns:
this Connection, for chaining

method

public Connection method(Connection.Method method)
Description copied from interface: Connection
Set the request method to use, GET or POST. Default is GET.

Specified by:
method in interface Connection
Parameters:
method - HTTP request method
Returns:
this Connection, for chaining

ignoreHttpErrors

public Connection ignoreHttpErrors(boolean ignoreHttpErrors)
Description copied from interface: Connection
Configures the connection to not throw exceptions when a HTTP error occurs. (4xx - 5xx, e.g. 404 or 500). By default this is false; an IOException is thrown if an error is encountered. If set to true, the response is populated with the error body, and the status message will reflect the error.

Specified by:
ignoreHttpErrors in interface Connection
Parameters:
ignoreHttpErrors - - false (default) if HTTP errors should be ignored.
Returns:
this Connection, for chaining

ignoreContentType

public Connection ignoreContentType(boolean ignoreContentType)
Description copied from interface: Connection
Ignore the document's Content-Type when parsing the response. By default this is false, an unrecognised content-type will cause an IOException to be thrown. (This is to prevent producing garbage by attempting to parse a JPEG binary image, for example.) Set to true to force a parse attempt regardless of content type.

Specified by:
ignoreContentType in interface Connection
Parameters:
ignoreContentType - set to true if you would like the content type ignored on parsing the response into a Document.
Returns:
this Connection, for chaining

data

public Connection data(String key,
                       String value)
Description copied from interface: Connection
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.

Specified by:
data in interface Connection
Parameters:
key - data key
value - data value
Returns:
this Connection, for chaining

data

public Connection data(Map<String,String> data)
Description copied from interface: Connection
Adds all of the supplied data to the request data parameters

Specified by:
data in interface Connection
Parameters:
data - map of data parameters
Returns:
this Connection, for chaining

data

public Connection data(String... keyvals)
Description copied from interface: Connection
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

Specified by:
data in interface Connection
Parameters:
keyvals - a set of key value pairs.
Returns:
this Connection, for chaining

data

public Connection data(Collection<Connection.KeyVal> data)
Description copied from interface: Connection
Adds all of the supplied data to the request data parameters

Specified by:
data in interface Connection
Parameters:
data - collection of data parameters
Returns:
this Connection, for chaining

header

public Connection header(String name,
                         String value)
Description copied from interface: Connection
Set a request header.

Specified by:
header in interface Connection
Parameters:
name - header name
value - header value
Returns:
this Connection, for chaining
See Also:
Connection.Base.headers()

cookie

public Connection cookie(String name,
                         String value)
Description copied from interface: Connection
Set a cookie to be sent in the request.

Specified by:
cookie in interface Connection
Parameters:
name - name of cookie
value - value of cookie
Returns:
this Connection, for chaining

cookies

public Connection cookies(Map<String,String> cookies)
Description copied from interface: Connection
Adds each of the supplied cookies to the request.

Specified by:
cookies in interface Connection
Parameters:
cookies - map of cookie name -> value pairs
Returns:
this Connection, for chaining

parser

public Connection parser(Parser parser)
Description copied from interface: Connection
Provide an alternate parser to use when parsing the response to a Document.

Specified by:
parser in interface Connection
Parameters:
parser - alternate parser
Returns:
this Connection, for chaining

get

public Document get()
             throws IOException
Description copied from interface: Connection
Execute the request as a GET, and parse the result.

Specified by:
get in interface Connection
Returns:
parsed Document
Throws:
MalformedURLException - if the request URL is not a HTTP or HTTPS URL, or is otherwise malformed
HttpStatusException - if the response is not OK and HTTP response errors are not ignored
UnsupportedMimeTypeException - if the response mime type is not supported and those errors are not ignored
SocketTimeoutException - if the connection times out
IOException - on error

post

public Document post()
              throws IOException
Description copied from interface: Connection
Execute the request as a POST, and parse the result.

Specified by:
post in interface Connection
Returns:
parsed Document
Throws:
MalformedURLException - if the request URL is not a HTTP or HTTPS URL, or is otherwise malformed
HttpStatusException - if the response is not OK and HTTP response errors are not ignored
UnsupportedMimeTypeException - if the response mime type is not supported and those errors are not ignored
SocketTimeoutException - if the connection times out
IOException - on error

execute

public Connection.Response execute()
                            throws IOException
Description copied from interface: Connection
Execute the request.

Specified by:
execute in interface Connection
Returns:
a response object
Throws:
MalformedURLException - if the request URL is not a HTTP or HTTPS URL, or is otherwise malformed
HttpStatusException - if the response is not OK and HTTP response errors are not ignored
UnsupportedMimeTypeException - if the response mime type is not supported and those errors are not ignored
SocketTimeoutException - if the connection times out
IOException - on error

request

public Connection.Request request()
Description copied from interface: Connection
Get the request object associated with this connection

Specified by:
request in interface Connection
Returns:
request

request

public Connection request(Connection.Request request)
Description copied from interface: Connection
Set the connection's request

Specified by:
request in interface Connection
Parameters:
request - new request object
Returns:
this Connection, for chaining

response

public Connection.Response response()
Description copied from interface: Connection
Get the response, once the request has been executed

Specified by:
response in interface Connection
Returns:
response

response

public Connection response(Connection.Response response)
Description copied from interface: Connection
Set the connection's response

Specified by:
response in interface Connection
Parameters:
response - new response
Returns:
this Connection, for chaining


Copyright © 2009-2013 Jonathan Hedley. All Rights Reserved.