public class

HttpResponse

extends Object
java.lang.Object
   ↳ com.neomades.io.http.HttpResponse

Class Overview

HttpResponse object represents an http/https connection response.

Summary

Protected Constructors
HttpResponse(HttpRequest request)
Internal
Public Methods
long getContentLength()
Returns the response content length.
byte[] getData()
Returns the downloaded data byte array.
InputStream getDataStream()
Opens the downloaded data.
String getDataString()
Returns the downloaded data string.
String getHeader(String headerName)
Get a Header value.
Map<StringString> getHeaders()
Returns an unmodifiable Map of the header fields.
List<String> getHeaders(String headerName)
Get the list of values corresponding to a header's name.
int getHttpCode()
Gets the server response
String getMessage()
Returns the associated message (could be "OK" in case of Success, otherwise the reason of the error)
HttpRequest getRequest()
Get the associated request.
boolean isSuccess()
Return if the HTTP response is corresponding to success.
[Expand]
Inherited Methods
From class java.lang.Object

Protected Constructors

protected HttpResponse (HttpRequest request)

Internal

Public Methods

public long getContentLength ()

Returns the response content length.

Returns
  • the content length in bytes

public byte[] getData ()

Returns the downloaded data byte array.

Since the data byte array is kept in memory, prefer using getDataStream() to download big data.

Returns
  • the downloaded data as a byte array

public InputStream getDataStream ()

Opens the downloaded data.

Once the stream returned and consumed, it is released from memory and cannot be accessed again using this method. It enables to save memory usage for big response. To access several time to the HttpResponse data, use getDataString() or getData() .

Returns
  • the downloaded data as an InputStream

public String getDataString ()

Returns the downloaded data string.

Since the data string is kept in memory, prefer using getDataStream() to download big data.

Returns
  • the downloaded data as a String

public String getHeader (String headerName)

Get a Header value.

Note: If the header appears several times in the response, the first header value is returned. If one header contains a comma-separated value list, the value before the first comma will be returned.

Parameters
headerName the header's name
Returns
  • the value associated with the name, or null if the header does not exist in the response

public Map<StringString> getHeaders ()

Returns an unmodifiable Map of the header fields.

The Map keys are Strings that represent the response-header field names. Each Map value is the raw String that represents the corresponding field values.

Returns
  • a Map of header fields

public List<String> getHeaders (String headerName)

Get the list of values corresponding to a header's name.

A header may appear several times in the response. The values are ordered as they are sent over the connection.

Note: if a header contains a comma-separated value list, this method will split the value into several values in the result list.

Parameters
headerName the header's name
Returns
  • the list of header values associated with the name, or null if the header does not exist in the response

public int getHttpCode ()

Gets the server response

Returns
  • the HTTP response code

public String getMessage ()

Returns the associated message (could be "OK" in case of Success, otherwise the reason of the error)

Returns
  • the associated message (in case of failure)
See Also

public HttpRequest getRequest ()

Get the associated request.

Returns
  • the associated request from which the connection is made.

public boolean isSuccess ()

Return if the HTTP response is corresponding to success.

This method returns true if the HTTP Status code corresponds :

  • 2XX : The request has succeeded (e.g. 200)
  • 3XX
This method returns false if the HTTP Status code corresponds :
  • 4XX : Client error (e.g. 404)
  • 5XX : Server error

To get more details about HTTP status see the getHttpCode() method.

Returns
  • true if the connection has succeed.
See Also