Package org.jsoup.helper
Class HttpConnection.Response
- All Implemented Interfaces:
Connection.Base<Connection.Response>
,Connection.Response
- Enclosing class:
HttpConnection
public static class HttpConnection.Response
extends HttpConnection.Base<Connection.Response>
implements Connection.Response
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate ControllableInputStream
private ByteBuffer
private String
private HttpURLConnection
private final String
private boolean
private boolean
private static final String
private static final int
private int
private final HttpConnection.Request
private final int
private final String
private static final Pattern
Fields inherited from class org.jsoup.helper.HttpConnection.Base
cookies, headers, method, url
-
Constructor Summary
ConstructorsModifierConstructorDescription(package private)
Response()
Internal only! Creates a dummy HttpConnection.Response, useful for testing.private
Response
(HttpURLConnection conn, HttpConnection.Request request, HttpConnection.Response previousResponse) -
Method Summary
Modifier and TypeMethodDescriptionbody()
Get the body of the response as a plain string.byte[]
Get the body of the response as an array of bytes.Get the body of the response as a (buffered) InputStream.bufferUp()
Read the body of the response into a local buffer, so thatConnection.Response.parse()
may be called repeatedly on the same connection response.charset()
Get the character set name of the response, derived from the content-type header.Set / override the response character set.Get the response content type (e.g.private static HttpURLConnection
private static LinkedHashMap
<String, List<String>> (package private) static HttpConnection.Response
(package private) static HttpConnection.Response
execute
(HttpConnection.Request req, HttpConnection.Response previousResponse) private static String
fixHeaderEncoding
(String val) Servers may encode response headers in UTF-8 instead of RFC defined 8859.private static boolean
looksLikeUtf8
(byte[] input) parse()
Read and parse the body of the response as a Document.private void
private InputStream
Called from parse() or streamParser(), validates and prepares the input stream, and aligns common settings.(package private) void
processResponseHeaders
(Map<String, List<String>> resHeaders) private void
Call on completion of stream read, to close the body (or error) stream.private static void
private static String
int
Get the status code of the response.Get the status message of the response.Returns aStreamParser
that will parse the Response progressively.private static void
writePost
(Connection.Request req, OutputStream outputStream, String boundary) Methods inherited from class org.jsoup.helper.HttpConnection.Base
addHeader, cookie, cookie, cookies, hasCookie, hasHeader, hasHeaderWithValue, header, header, headers, headers, method, method, multiHeaders, removeCookie, removeHeader, url, url
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.jsoup.Connection.Base
addHeader, cookie, cookie, cookies, hasCookie, hasHeader, hasHeaderWithValue, header, header, headers, headers, method, method, multiHeaders, removeCookie, removeHeader, url, url
-
Field Details
-
MAX_REDIRECTS
private static final int MAX_REDIRECTS- See Also:
-
LOCATION
- See Also:
-
statusCode
private final int statusCode -
statusMessage
-
byteData
-
bodyStream
-
conn
-
charset
-
contentType
-
executed
private boolean executed -
inputStreamRead
private boolean inputStreamRead -
numRedirects
private int numRedirects -
req
-
xmlContentTypeRxp
-
-
Constructor Details
-
Response
Response()Internal only! Creates a dummy HttpConnection.Response, useful for testing. All actual responses are created from the HttpURLConnection and fields defined. -
Response
private Response(HttpURLConnection conn, HttpConnection.Request request, HttpConnection.Response previousResponse) throws IOException - Throws:
IOException
-
-
Method Details
-
execute
- Throws:
IOException
-
execute
static HttpConnection.Response execute(HttpConnection.Request req, HttpConnection.Response previousResponse) throws IOException - Throws:
IOException
-
statusCode
public int statusCode()Description copied from interface:Connection.Response
Get the status code of the response.- Specified by:
statusCode
in interfaceConnection.Response
- Returns:
- status code
-
statusMessage
Description copied from interface:Connection.Response
Get the status message of the response.- Specified by:
statusMessage
in interfaceConnection.Response
- Returns:
- status message
-
charset
Description copied from interface:Connection.Response
Get the character set name of the response, derived from the content-type header.- Specified by:
charset
in interfaceConnection.Response
- Returns:
- character set name if set, null if not
-
charset
Description copied from interface:Connection.Response
Set / override the response character set. When the document body is parsed it will be with this charset.- Specified by:
charset
in interfaceConnection.Response
- Parameters:
charset
- to decode body as- Returns:
- this Response, for chaining
-
contentType
Description copied from interface:Connection.Response
Get the response content type (e.g. "text/html");- Specified by:
contentType
in interfaceConnection.Response
- Returns:
- the response content type, or null if one was not set
-
prepareParse
Called from parse() or streamParser(), validates and prepares the input stream, and aligns common settings. -
parse
Description copied from interface:Connection.Response
Read and parse the body of the response as a Document. If you intend to parse the same response multiple times, you shouldConnection.Response.bufferUp()
first.- Specified by:
parse
in interfaceConnection.Response
- Returns:
- a parsed Document
- Throws:
IOException
- on error
-
streamParser
Description copied from interface:Connection.Response
Returns aStreamParser
that will parse the Response progressively.- Specified by:
streamParser
in interfaceConnection.Response
- Returns:
- a StreamParser, prepared to parse this response.
- Throws:
IOException
- if an IO exception occurs preparing the parser.
-
prepareByteData
private void prepareByteData() -
body
Description copied from interface:Connection.Response
Get the body of the response as a plain string.- Specified by:
body
in interfaceConnection.Response
- Returns:
- body
-
bodyAsBytes
public byte[] bodyAsBytes()Description copied from interface:Connection.Response
Get the body of the response as an array of bytes.- Specified by:
bodyAsBytes
in interfaceConnection.Response
- Returns:
- body bytes
-
bufferUp
Description copied from interface:Connection.Response
Read the body of the response into a local buffer, so thatConnection.Response.parse()
may be called repeatedly on the same connection response. Otherwise, once the response is read, its InputStream will have been drained and may not be re-read.Calling
Connection.Response.body()
orConnection.Response.bodyAsBytes()
has the same effect.- Specified by:
bufferUp
in interfaceConnection.Response
- Returns:
- this response, for chaining
-
bodyStream
Description copied from interface:Connection.Response
Get the body of the response as a (buffered) InputStream. You should close the input stream when you're done with it.Other body methods (like bufferUp, body, parse, etc) will generally not work in conjunction with this method, as it consumes the InputStream.
Any configured max size or maximum read timeout applied to the connection will not be applied to this stream, unless
Connection.Response.bufferUp()
is called prior.This method is useful for writing large responses to disk, without buffering them completely into memory first.
- Specified by:
bodyStream
in interfaceConnection.Response
- Returns:
- the response body input stream
-
createConnection
- Throws:
IOException
-
safeClose
private void safeClose()Call on completion of stream read, to close the body (or error) stream. The connection.disconnect allows keep-alives to work (as the underlying connection is actually held open, despite the name). -
createHeaderMap
-
processResponseHeaders
-
fixHeaderEncoding
Servers may encode response headers in UTF-8 instead of RFC defined 8859. This method attempts to detect that and re-decode the string as UTF-8.- Parameters:
val
- a header value string that may have been incorrectly decoded as 8859.- Returns:
- a potentially re-decoded string.
-
looksLikeUtf8
private static boolean looksLikeUtf8(byte[] input) -
setOutputContentType
-
writePost
private static void writePost(Connection.Request req, OutputStream outputStream, String boundary) throws IOException - Throws:
IOException
-
serialiseRequestUrl
- Throws:
IOException
-