public final class HttpResponse
extends java.lang.Object
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.
Modifier and Type | Method and Description |
---|---|
void |
disconnect()
Close the HTTP response content using
ignore() , and disconnect using
LowLevelHttpResponse.disconnect() . |
void |
download(java.io.OutputStream outputStream)
Writes the content of the HTTP response into the given destination output stream.
|
java.io.InputStream |
getContent()
Returns the content of the HTTP response.
|
java.nio.charset.Charset |
getContentCharset()
Returns the
Charset specified in the Content-Type of this response or the
"ISO-8859-1" charset as a default. |
java.lang.String |
getContentEncoding()
Returns the content encoding or
null for none. |
int |
getContentLoggingLimit()
Returns the limit to the content size that will be logged during
getContent() . |
java.lang.String |
getContentType()
Returns the content type or
null for none. |
HttpHeaders |
getHeaders()
Returns the HTTP response headers.
|
HttpMediaType |
getMediaType()
Returns the parsed Content-Type in form of a
HttpMediaType or null if no
content-type was set. |
HttpRequest |
getRequest()
Returns the HTTP request.
|
int |
getStatusCode()
Returns the HTTP status code or
0 for none. |
java.lang.String |
getStatusMessage()
Returns the HTTP status message or
null for none. |
HttpTransport |
getTransport()
Returns the HTTP transport.
|
void |
ignore()
Closes the content of the HTTP response from
getContent() , ignoring any content. |
boolean |
isLoggingEnabled()
Returns whether logging should be enabled on this response.
|
boolean |
isSuccessStatusCode()
Returns whether received a successful HTTP status code
>= 200 && < 300 (see
getStatusCode() ). |
<T> T |
parseAs(java.lang.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() . |
java.lang.Object |
parseAs(java.lang.reflect.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() . |
java.lang.String |
parseAsString()
Parses the content of the HTTP response from
getContent() and reads it into a string. |
HttpResponse |
setContentLoggingLimit(int contentLoggingLimit)
Set the limit to the content size that will be logged during
getContent() . |
HttpResponse |
setLoggingEnabled(boolean loggingEnabled)
Sets whether logging should be enabled on this response.
|
public int getContentLoggingLimit()
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()
.
public HttpResponse setContentLoggingLimit(int contentLoggingLimit)
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()
.
public boolean isLoggingEnabled()
Defaults to HttpRequest.isLoggingEnabled()
.
public HttpResponse setLoggingEnabled(boolean loggingEnabled)
Defaults to HttpRequest.isLoggingEnabled()
.
public java.lang.String getContentEncoding()
null
for none.public java.lang.String getContentType()
null
for none.public HttpMediaType getMediaType()
HttpMediaType
or null
if no
content-type was set.public HttpHeaders getHeaders()
public boolean isSuccessStatusCode()
>= 200 && < 300
(see
getStatusCode()
).public int getStatusCode()
0
for none.public java.lang.String getStatusMessage()
null
for none.public HttpTransport getTransport()
public HttpRequest getRequest()
public java.io.InputStream getContent() throws java.io.IOException
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(); }
disconnect()
does not have to be called if the content is closed.
null
for nonejava.io.IOException
- I/O exceptionpublic void download(java.io.OutputStream outputStream) throws java.io.IOException
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.
outputStream
- destination output streamjava.io.IOException
- I/O exceptionpublic void ignore() throws java.io.IOException
getContent()
, ignoring any content.java.io.IOException
public void disconnect() throws java.io.IOException
ignore()
, and disconnect using
LowLevelHttpResponse.disconnect()
.java.io.IOException
public <T> T parseAs(java.lang.Class<T> dataClass) throws java.io.IOException
getContent()
and reads it into a data
class of key/value pairs using the parser returned by HttpRequest.getParser()
.
Reference: http://tools.ietf.org/html/rfc2616#section-4.3
null
for no contentjava.io.IOException
public java.lang.Object parseAs(java.lang.reflect.Type dataType) throws java.io.IOException
getContent()
and reads it into a data
type of key/value pairs using the parser returned by HttpRequest.getParser()
.null
for no contentjava.io.IOException
public java.lang.String parseAsString() throws java.io.IOException
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.
""
for no contentjava.io.IOException
- I/O exceptionpublic java.nio.charset.Charset getContentCharset()
Charset
specified in the Content-Type of this response or the
"ISO-8859-1"
charset as a default.Copyright © 2011-2018 Google. All Rights Reserved.