类 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, with readToString(), readToBytes(), writeTo(Path) etc.., you need to close this raw response manually.

    This class is not thread-safe.

    作者:
    Liu Dong
    • 方法详细资料

      • decompressBody

        public ResponseContext decompressBody​(boolean decompressBody)
        If decompress http response body automatically. Default True.
      • 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 stream
        handler - 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 type
        charset - the charset to decode response body
      • writeTo

        public Response<Path> writeTo​(Path path)
        Write response body to file
      • 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
      • discard

        public Response<Void> discard()
        Consume and discard this response body.