Class HttpConnection.Response
- All Implemented Interfaces:
Connection.Base<Connection.Response>,Connection.Response
- Enclosing class:
HttpConnection
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate ControllableInputStreamprivate ByteBufferprivate String(package private) int(package private) Stringprivate boolean(package private) RequestExecutorprivate booleanprivate static final Stringprivate static final intprivate intprivate final HttpConnection.Request(package private) int(package private) Stringprivate static final PatternFields inherited from class org.jsoup.helper.HttpConnection.Base
cookies, headers, method, url -
Constructor Summary
ConstructorsConstructorDescriptionResponse()Internal only! Creates a dummy HttpConnection.Response, useful for testing.Response(HttpConnection.Request request) -
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.(package private) static HttpConnection.Response(package private) static HttpConnection.Responseexecute(HttpConnection.Request req, HttpConnection.Response prevRes) (package private) static StringfixHeaderEncoding(String val) Servers may encode response headers in UTF-8 instead of RFC defined 8859.private static voidimplWritePost(HttpConnection.Request req, BufferedWriter w, OutputStream outputStream) private static booleanlooksLikeUtf8(byte[] input) parse()Read and parse the body of the response as a Document.private ControllableInputStreamCalled from parse() or streamParser(), validates and prepares the input stream, and aligns common settings.(package private) voidprepareResponse(Map<String, List<String>> resHeaders, HttpConnection.Response previousResponse) (package private) voidprocessResponseHeaders(Map<String, List<String>> resHeaders) readBody()Read the response body, and returns it as a plain String.private voidReads the body, but throws an UncheckedIOException if an IOException occurs.Reads the bodyStream into byteData.private voidCall on completion of stream read, to close the body (or error) stream.private static voidprivate static voidintGet the status code of the response.Get the status message of the response.Returns aStreamParserthat will parse the Response progressively.(package private) static voidwritePost(HttpConnection.Request req, OutputStream outputStream) 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, urlMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods 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
int statusCode -
statusMessage
String statusMessage -
byteData
-
bodyStream
-
executor
RequestExecutor executor -
charset
-
contentType
String contentType -
contentLength
int contentLength -
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
Response(HttpConnection.Request request)
-
-
Method Details
-
execute
- Throws:
IOException
-
execute
static HttpConnection.Response execute(HttpConnection.Request req, HttpConnection.Response prevRes) throws IOException - Throws:
IOException
-
statusCode
public int statusCode()Description copied from interface:Connection.ResponseGet the status code of the response.- Specified by:
statusCodein interfaceConnection.Response- Returns:
- status code
-
statusMessage
Description copied from interface:Connection.ResponseGet the status message of the response.- Specified by:
statusMessagein interfaceConnection.Response- Returns:
- status message
-
charset
Description copied from interface:Connection.ResponseGet the character set name of the response, derived from the content-type header.- Specified by:
charsetin interfaceConnection.Response- Returns:
- character set name if set, null if not
-
charset
Description copied from interface:Connection.ResponseSet / override the response character set. When the document body is parsed it will be with this charset.- Specified by:
charsetin interfaceConnection.Response- Parameters:
charset- to decode body as- Returns:
- this Response, for chaining
-
contentType
Description copied from interface:Connection.ResponseGet the response content type (e.g. "text/html");- Specified by:
contentTypein 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.ResponseRead and parse the body of the response as a Document. If you intend to parse the same response multiple times, you shouldConnection.Response.readFully()first, which will buffer the body into memory.- Specified by:
parsein interfaceConnection.Response- Returns:
- a parsed Document
- Throws:
IOException- if an IO exception occurs whilst reading the body.- See Also:
-
streamParser
Description copied from interface:Connection.ResponseReturns aStreamParserthat will parse the Response progressively.- Specified by:
streamParserin interfaceConnection.Response- Returns:
- a StreamParser, prepared to parse this response.
- Throws:
IOException- if an IO exception occurs preparing the parser.
-
readFully
Reads the bodyStream into byteData. A no-op if already executed.- Specified by:
readFullyin interfaceConnection.Response- Returns:
- this response, for chaining
- Throws:
IOException- if an IO exception occurs during buffering.
-
readByteDataUnchecked
private void readByteDataUnchecked()Reads the body, but throws an UncheckedIOException if an IOException occurs.- Throws:
UncheckedIOException- if an IOException occurs
-
readBody
Description copied from interface:Connection.ResponseRead the response body, and returns it as a plain String.- Specified by:
readBodyin interfaceConnection.Response- Returns:
- body
- Throws:
IOException- if an IO exception occurs whilst reading the body.
-
body
Description copied from interface:Connection.ResponseGet the body of the response as a plain String.Will throw an UncheckedIOException if the body has not been buffered and an error occurs whilst reading the body; use
Connection.Response.readFully()first to buffer the body and catch any exceptions explicitly. Or more simply,Connection.Response.readBody().- Specified by:
bodyin interfaceConnection.Response- Returns:
- body
- See Also:
-
bodyAsBytes
public byte[] bodyAsBytes()Description copied from interface:Connection.ResponseGet the body of the response as an array of bytes.Will throw an UncheckedIOException if the body has not been buffered and an error occurs whilst reading the body; use
Connection.Response.readFully()first to buffer the body and catch any exceptions explicitly.- Specified by:
bodyAsBytesin interfaceConnection.Response- Returns:
- body bytes
- See Also:
-
bufferUp
Description copied from interface:Connection.ResponseRead 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:
bufferUpin interfaceConnection.Response- Returns:
- this response, for chaining
-
bodyStream
Description copied from interface:Connection.ResponseGet 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 readFully, 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.readFully()is called prior.This method is useful for writing large responses to disk, without buffering them completely into memory first.
- Specified by:
bodyStreamin interfaceConnection.Response- Returns:
- the response body input stream
-
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). -
prepareResponse
void prepareResponse(Map<String, List<String>> resHeaders, HttpConnection.Response previousResponse) throws IOException- Throws:
IOException
-
processResponseHeaders
-
fixHeaderEncoding
Servers may encode response headers in UTF-8 instead of RFC defined 8859. The JVM decodes the headers (before we see them) as 8859, which can lead to mojibake data.This method attempts to detect that and re-decode the string as UTF-8.
However on Android, the headers will be decoded as UTF8, so we can detect and pass those directly.
- 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
- Throws:
IOException
-
implWritePost
private static void implWritePost(HttpConnection.Request req, BufferedWriter w, OutputStream outputStream) throws IOException - Throws:
IOException
-
serialiseRequestUrl
- Throws:
IOException
-