- java.lang.Object
-
- net.dongliu.cute.http.ResponseContext
-
- 所有已实现的接口:
AutoCloseable
public class ResponseContext extends Object implements AutoCloseable
Raw blocking http response. The http headers already received, the http body not consumed, can be get as InputStream. It you do not consume http response body, withreadToString(),readToBytes(),writeTo(Path)etc.., you need to close this raw response manually.This class is not thread-safe.
- 作者:
- Liu Dong
-
-
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 ResponseContextcheckStatusCode(IntPredicate predicate)Check the response status code.voidclose()<T> Response<T>decodeJson(Class<T> type)Unmarshal response body as json.<T> Response<T>decodeJson(Class<T> type, Charset charset)Unmarshal response body as json<T> Response<T>decodeJson(net.dongliu.commons.reflect.TypeInfer<T> typeInfer)Unmarshal response body as json.<T> Response<T>decodeJson(net.dongliu.commons.reflect.TypeInfer<T> typeInfer, Charset charset)Unmarshal response body as jsonResponseContextdecompressBody(boolean decompressBody)If decompress http response body automatically.Response<Void>discard()Consume and discard this response body.<T> Response<T>handle(ResponseHandler<InputStream,T> responseHandler)Handle response body with handler, return a new response with content as handler result.<T> Response<T>handleAsReader(ResponseHandler<Reader,T> handler)Handle response content as reader.<T> Response<T>handleAsReader(ResponseHandler<Reader,T> handler, Charset charset)Handle response content as reader.Response<byte[]>readToBytes()Convert to response, with body as byte arrayResponse<String>readToString()Convert to response, with body as text.Response<String>readToString(Charset charset)Convert to response, with body as text.Response<Void>writeTo(OutputStream out)Write response body to OutputStream.Response<Void>writeTo(Writer writer)Write response body to Writer, use charset detected from response header to decode response body.Response<Void>writeTo(Writer writer, Charset charset)Write response body to Writer.Response<Path>writeTo(Path path)Write response body to file
-
-
-
方法详细资料
-
decompressBody
public ResponseContext decompressBody(boolean decompressBody)
If decompress http response body automatically. Default True.
-
checkStatusCode
public ResponseContext checkStatusCode(IntPredicate predicate) throws IllegalStatusCodeException
Check the response status code. If not pass the predicate, throwIllegalStatusCodeException- 抛出:
IllegalArgumentException- if check failed.IllegalStatusCodeException
-
handle
public <T> Response<T> handle(ResponseHandler<InputStream,T> responseHandler)
Handle response body with handler, return a new response with content as handler result. HTTPResponseHandler should consume all InputStream data, or connection may close and cannot reuse. The response is closed whether this call succeed or failed with exception.
-
handleAsReader
public <T> Response<T> handleAsReader(ResponseHandler<Reader,T> handler, Charset charset)
Handle response content as reader. The reader passed to handler would be closed when handler finished or error occurred.- 类型参数:
T- the response body type to convert to- 参数:
charset- the set used to decode the response streamhandler- the handler
-
handleAsReader
public <T> Response<T> handleAsReader(ResponseHandler<Reader,T> handler)
Handle response content as reader. This method will try to get response charset from header, if not set, will use UTF8. The reader passed to handler would be closed when handler finished or error occurred.- 类型参数:
T- the response body type to convert to- 参数:
handler- the handler
-
readToString
public Response<String> readToString()
Convert to response, with body as text. This method will try to get response charset from header, if not set, will use UTF8.
-
readToString
public Response<String> readToString(Charset charset)
Convert to response, with body as text.- 参数:
charset- the charset to decode response body
-
readToBytes
public Response<byte[]> readToBytes()
Convert to response, with body as byte array
-
decodeJson
public <T> Response<T> decodeJson(Class<T> type)
Unmarshal response body as json. This method will try to get response charset from header, if not set, will use UTF8.- 类型参数:
T- The json value type
-
decodeJson
public <T> Response<T> decodeJson(net.dongliu.commons.reflect.TypeInfer<T> typeInfer)
Unmarshal response body as json. This method will try to get response charset from header, if not set, will use UTF8.- 类型参数:
T- The json value type- 参数:
typeInfer- for getting actual generic type
-
decodeJson
public <T> Response<T> decodeJson(Class<T> type, Charset charset)
Unmarshal response body as json- 类型参数:
T- The json value type- 参数:
charset- the charset to decode response body
-
decodeJson
public <T> Response<T> decodeJson(net.dongliu.commons.reflect.TypeInfer<T> typeInfer, Charset charset)
Unmarshal response body as json- 类型参数:
T- The json value type- 参数:
typeInfer- for getting actual generic typecharset- the charset to decode response body
-
writeTo
public Response<Void> writeTo(OutputStream out)
Write response body to OutputStream. OutputStream will not be closed.
-
writeTo
public Response<Void> writeTo(Writer writer)
Write response body to Writer, use charset detected from response header to decode response body. If charset in header not set, will use utf-8 The Writer will be leaved unclosed when finished or exception occurred.
-
writeTo
public Response<Void> writeTo(Writer writer, Charset charset)
Write response body to Writer. The Writer will be leaved unclosed when finished or exception occurred.- 参数:
charset- the charset to decode response body
-
close
public void close()
- 指定者:
close在接口中AutoCloseable
-
-