Class HttpResponse
Callers should call disconnect() when the HTTP response object is no longer needed.
However, disconnect() does not have to be called if the response stream is properly
closed. Example usage:
HttpResponse response = request.execute();
try {
// process the HTTP response object
} finally {
response.disconnect();
}
Implementation is not thread-safe.
- Since:
- 1.0
- Author:
- Yaniv Inbar
-
Method Summary
Modifier and TypeMethodDescriptionvoidDisconnect usingLowLevelHttpResponse.disconnect(), then close the HTTP response content usingignore().voiddownload(OutputStream outputStream) Writes the content of the HTTP response into the given destination output stream.Returns the content of the HTTP response.Returns theCharsetspecified in the Content-Type of this response or the ISO-8859-1 charset as a default.Returns the content encoding ornullfor none.intReturns the limit to the content size that will be logged duringgetContent().Returns the content type ornullfor none.Returns the HTTP response headers.Returns the parsed Content-Type in form of aHttpMediaTypeornullif no content-type was set.Returns the HTTP request.intReturns the HTTP status code or0for none.Returns the HTTP status message ornullfor none.Returns the HTTP transport.voidignore()Closes the content of the HTTP response fromgetContent(), ignoring any content.booleanReturns whether logging should be enabled on this response.booleanReturns whether received a successful HTTP status code>= 200 && < 300(seegetStatusCode()).<T> TParses the content of the HTTP response fromgetContent()and reads it into a data class of key/value pairs using the parser returned byHttpRequest.getParser().Parses the content of the HTTP response fromgetContent()and reads it into a data type of key/value pairs using the parser returned byHttpRequest.getParser().Parses the content of the HTTP response fromgetContent()and reads it into a string.setContentLoggingLimit(int contentLoggingLimit) Set the limit to the content size that will be logged duringgetContent().setLoggingEnabled(boolean loggingEnabled) Sets whether logging should be enabled on this response.
-
Method Details
-
getContentLoggingLimit
public int getContentLoggingLimit()Returns the limit to the content size that will be logged duringgetContent().Content will only be logged if
isLoggingEnabled()istrue.If the content size is greater than this limit then it will not be logged.
Can be set to
0to disable content logging. This is useful for example if content has sensitive data such as authentication information.Defaults to
HttpRequest.getContentLoggingLimit().- Since:
- 1.7
-
setContentLoggingLimit
Set the limit to the content size that will be logged duringgetContent().Content will only be logged if
isLoggingEnabled()istrue.If the content size is greater than this limit then it will not be logged.
Can be set to
0to disable content logging. This is useful for example if content has sensitive data such as authentication information.Defaults to
HttpRequest.getContentLoggingLimit().- Since:
- 1.7
-
isLoggingEnabled
public boolean isLoggingEnabled()Returns whether logging should be enabled on this response.Defaults to
HttpRequest.isLoggingEnabled().- Since:
- 1.9
-
setLoggingEnabled
Sets whether logging should be enabled on this response.Defaults to
HttpRequest.isLoggingEnabled().- Since:
- 1.9
-
getContentEncoding
-
getContentType
-
getMediaType
Returns the parsed Content-Type in form of aHttpMediaTypeornullif no content-type was set.- Since:
- 1.10
-
getHeaders
-
isSuccessStatusCode
public boolean isSuccessStatusCode()Returns whether received a successful HTTP status code>= 200 && < 300(seegetStatusCode()).- Since:
- 1.5
-
getStatusCode
public int getStatusCode()Returns the HTTP status code or0for none.- Since:
- 1.5
-
getStatusMessage
-
getTransport
-
getRequest
-
getContent
Returns the content of the HTTP response.The result is cached, so subsequent calls will be fast.
Callers should call
InputStream.close()after the returnedInputStreamis no longer needed. Example usage:InputStream is = response.getContent(); try { // Process the input stream.. } finally { is.close(); }disconnect()does not have to be called if the content is closed.- Returns:
- input stream content of the HTTP response or
nullfor none - Throws:
IOException- I/O exception
-
download
Writes the content of the HTTP response into the given destination output stream.Sample usage:
HttpRequest request = requestFactory.buildGetRequest( new GenericUrl("https://www.google.com/images/srpr/logo3w.png")); OutputStream outputStream = new FileOutputStream(new File("/tmp/logo3w.png")); try { HttpResponse response = request.execute(); response.download(outputStream); } finally { outputStream.close(); }This method closes the content of the HTTP response from
getContent().This method does not close the given output stream.
- Parameters:
outputStream- destination output stream- Throws:
IOException- I/O exception- Since:
- 1.9
-
ignore
Closes the content of the HTTP response fromgetContent(), ignoring any content.- Throws:
IOException
-
disconnect
Disconnect usingLowLevelHttpResponse.disconnect(), then close the HTTP response content usingignore().- Throws:
IOException- Since:
- 1.4
-
parseAs
Parses the content of the HTTP response fromgetContent()and reads it into a data class of key/value pairs using the parser returned byHttpRequest.getParser().Reference: http://tools.ietf.org/html/rfc2616#section-4.3
- Returns:
- parsed data class or
nullfor no content - Throws:
IOException
-
parseAs
Parses the content of the HTTP response fromgetContent()and reads it into a data type of key/value pairs using the parser returned byHttpRequest.getParser().- Returns:
- parsed data type instance or
nullfor no content - Throws:
IOException- Since:
- 1.10
-
parseAsString
Parses the content of the HTTP response fromgetContent()and reads it into a string.Since this method returns
""for no content, a simpler check for no content is to check ifgetContent()isnull.All content is read from the input content stream rather than being limited by the Content-Length. For the character set, it follows the specification by parsing the "charset" parameter of the Content-Type header or by default
"ISO-8859-1"if the parameter is missing.- Returns:
- parsed string or
""for no content - Throws:
IOException- I/O exception
-
getContentCharset
-