public final class HttpResponse
HTTP response.
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.
Methods
<T>parseAs(Class<T> dataClass)
public T <T>parseAs(Class<T> dataClass)
Parses the content of the HTTP response from #getContent() and reads it into a data class of key/value pairs using the parser returned by HttpRequest#getParser().
Parameter | |
---|---|
Name | Description |
dataClass | Class<T> |
Returns | |
---|---|
Type | Description |
T | parsed data class or |
Exceptions | |
---|---|
Type | Description |
IOException |
disconnect()
public void disconnect()
Disconnect using LowLevelHttpResponse#disconnect(), then close the HTTP response content using #ignore.
Exceptions | |
---|---|
Type | Description |
IOException |
download(OutputStream outputStream)
public void download(OutputStream outputStream)
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.
Parameter | |
---|---|
Name | Description |
outputStream | OutputStream destination output stream |
Exceptions | |
---|---|
Type | Description |
IOException | I/O exception |
getContent()
public InputStream 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 returned InputStream is no longer needed. Example usage:
InputStream is = response.getContent(); try { // Process the input stream.. } finally { is.close(); }
HttpResponse#disconnect does not have to be called if the content is closed.
Returns | |
---|---|
Type | Description |
InputStream | input stream content of the HTTP response or |
Exceptions | |
---|---|
Type | Description |
IOException | I/O exception |
getContentCharset()
public Charset getContentCharset()
Returns the Charset specified in the Content-Type of this response or the ISO-8859-1 charset as a default.
Returns | |
---|---|
Type | Description |
Charset |
getContentEncoding()
public String getContentEncoding()
Returns the content encoding or null
for none.
Returns | |
---|---|
Type | Description |
String |
getContentLoggingLimit()
public int getContentLoggingLimit()
Returns the limit to the content size that will be logged during #getContent().
Content will only be logged if #isLoggingEnabled is true
.
If the content size is greater than this limit then it will not be logged.
Can be set to 0
to disable content logging. This is useful for example if content
has sensitive data such as authentication information.
Defaults to HttpRequest#getContentLoggingLimit().
Returns | |
---|---|
Type | Description |
int |
getContentType()
public String getContentType()
Returns the content type or null
for none.
Returns | |
---|---|
Type | Description |
String |
getHeaders()
public HttpHeaders getHeaders()
Returns the HTTP response headers.
Returns | |
---|---|
Type | Description |
HttpHeaders |
getMediaType()
public HttpMediaType getMediaType()
Returns the parsed Content-Type in form of a HttpMediaType or null
if no
content-type was set.
Returns | |
---|---|
Type | Description |
HttpMediaType |
getRequest()
public HttpRequest getRequest()
Returns the HTTP request.
Returns | |
---|---|
Type | Description |
HttpRequest |
getStatusCode()
public int getStatusCode()
Returns the HTTP status code or 0
for none.
Returns | |
---|---|
Type | Description |
int |
getStatusMessage()
public String getStatusMessage()
Returns the HTTP status message or null
for none.
Returns | |
---|---|
Type | Description |
String |
getTransport()
public HttpTransport getTransport()
Returns the HTTP transport.
Returns | |
---|---|
Type | Description |
HttpTransport |
ignore()
public void ignore()
Closes the content of the HTTP response from #getContent(), ignoring any content.
Exceptions | |
---|---|
Type | Description |
IOException |
isLoggingEnabled()
public boolean isLoggingEnabled()
Returns whether logging should be enabled on this response.
Defaults to HttpRequest#isLoggingEnabled().
Returns | |
---|---|
Type | Description |
boolean |
isSuccessStatusCode()
public boolean isSuccessStatusCode()
Returns whether received a successful HTTP status code >= 200 && < 300
(see #getStatusCode()).
Returns | |
---|---|
Type | Description |
boolean |
parseAs(Type dataType)
public Object parseAs(Type dataType)
Parses the content of the HTTP response from #getContent() and reads it into a data type of key/value pairs using the parser returned by HttpRequest#getParser().
Parameter | |
---|---|
Name | Description |
dataType | Type |
Returns | |
---|---|
Type | Description |
Object | parsed data type instance or |
Exceptions | |
---|---|
Type | Description |
IOException |
parseAsString()
public String parseAsString()
Parses the content of the HTTP response from #getContent() and reads it into a string.
Since this method returns ""
for no content, a simpler check for no content is to
check if #getContent() is null
.
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 | |
---|---|
Type | Description |
String | parsed string or |
Exceptions | |
---|---|
Type | Description |
IOException | I/O exception |
setContentLoggingLimit(int contentLoggingLimit)
public HttpResponse setContentLoggingLimit(int contentLoggingLimit)
Set the limit to the content size that will be logged during #getContent().
Content will only be logged if #isLoggingEnabled is true
.
If the content size is greater than this limit then it will not be logged.
Can be set to 0
to disable content logging. This is useful for example if content
has sensitive data such as authentication information.
Defaults to HttpRequest#getContentLoggingLimit().
Parameter | |
---|---|
Name | Description |
contentLoggingLimit | int |
Returns | |
---|---|
Type | Description |
HttpResponse |
setLoggingEnabled(boolean loggingEnabled)
public HttpResponse setLoggingEnabled(boolean loggingEnabled)
Sets whether logging should be enabled on this response.
Defaults to HttpRequest#isLoggingEnabled().
Parameter | |
---|---|
Name | Description |
loggingEnabled | boolean |
Returns | |
---|---|
Type | Description |
HttpResponse |