Interface HttpResponse

All Superinterfaces:
AutoCloseable
All Known Implementing Classes:
ApacheHC5HttpClientResponse, JdkHttpClientResponse

public interface HttpResponse extends AutoCloseable
Represents an HTTP response. Must be closed (via close() or try-with-resources) to release any underlying connection resources.
  • Method Details

    • getStatusCode

      int getStatusCode()
      Returns:
      the HTTP status code.
    • getReasonPhrase

      String getReasonPhrase()
      Returns:
      the HTTP reason phrase, or an empty string if not available.
    • getHeaders

      List<HttpHeader> getHeaders()
      Returns:
      all response headers.
    • getHeader

      default Optional<String> getHeader(String name)
      Returns the first header value for the given name (case-insensitive).
      Parameters:
      name - the header name.
      Returns:
      the first matching header value, or Optional.empty().
    • getHeaders

      default List<HttpHeader> getHeaders(String name)
      Returns all headers matching the given name (case-insensitive).
      Parameters:
      name - the header name.
      Returns:
      matching headers, possibly empty.
    • getBodyAsStream

      InputStream getBodyAsStream() throws IOException
      Returns an InputStream over the response body. For responses with no body (e.g. HTTP 204 No Content, or responses to HEAD requests), implementations must return an empty stream rather than null.
      Returns:
      an InputStream over the response body; never null
      Throws:
      IOException - if reading fails.
    • discard

      void discard() throws IOException
      Discards the response body, releasing any underlying resources.
      Throws:
      IOException - if discarding fails.
    • discardQuietly

      default void discardQuietly()
      Discards the response body, silently ignoring any IOException.
    • close

      void close()
      Closes this response, releasing any underlying connection or stream resources.
      Specified by:
      close in interface AutoCloseable
    • discardAndClose

      default void discardAndClose()
      Discards the response body and closes this response, releasing the underlying connection. Any IOException thrown by discard() is silently ignored; close() is always called.