Class SimpleHttpRequest

java.lang.Object
com.nagra.otvaa.sdk.utils.SimpleHttpRequest

public class SimpleHttpRequest extends Object
SimpleHttpRequest is a helper class to facilitate HTTP/HTTPS communications with a server. The class is designed for synchronous operations, with some methods making it possible to operate in an asynchronous mode.

First, HTTP headers are placed in a Map<String, String> structure. For example:
Map<String, String> requestProperties = new HashMap<>(); requestProperties.put("Content-Encoding", "UTF-8");
Next, if required, some data intended for the body of the message is prepared:
byte[] requestData = ....
Only for asynchronous operations, we need to set-up a listener for successful and failed operations:
SimpleHttpRequest.ResponseHandler responseHandler = new SimpleHttpRequest.ResponseHandler() { @Override public void onSuccess(int statusCode, Map<String, List<String>> headers, byte[] successResponse) { // handle here a successful HTTP request } @Override public void onFailure(int statusCode, Map<String, List<String>> headers, byte[] errorResponse, Throwable e) { // handle here a failed HTTP request } };
Now the class can be instantiated.
SimpleHttpRequest request = new SimpleHttpRequest(theUrl, requestProperties, requestData, responseHandler);
Some request parameters can be configured prior to the request sent:
request.setConnectTimeout(2000); request.setReadTimeout(2000); And the actual message is initiated in the makeRequest() call
request.makeRequest(SimpleHttpRequest.HTTP_METHOD.GET);
Once makeRequest() returns, the response details can be extracted:
int responseCode = request.getResponseCode(); Map<String, List<String>> headers = getResponseHeaderFields(); if (200 == responseCode) { byte[] responseData = request.getResponseData(); String contentType = request.getContentType() } else { String errorMessage = request.getErrorMessage(); } :

  • Constructor Details

  • Method Details

    • setConnectTimeout

      public void setConnectTimeout(int timeout)
      Sets a specified timeout value, in milliseconds, to be used when opening a communications link to the resource referenced by this connection. If the timeout expires before the connection can be established, a SocketTimeoutException is raised. A timeout of zero is interpreted as an infinite timeout.
      Parameters:
      timeout - an int that specifies the connect timeout value in milliseconds
    • setReadTimeout

      public void setReadTimeout(int timeout)
      Sets the read timeout to a specified timeout, in milliseconds. A non-zero value specifies the timeout when reading from Input stream when a connection is established to a resource. If the timeout expires before there is data available for read, a java.net.SocketTimeoutException is raised. A timeout of zero is interpreted as an infinite timeout.
      Parameters:
      timeout - an int that specifies the timeout value to be used in milliseconds
    • makeRequest

      public void makeRequest(SimpleHttpRequest.HTTP_METHOD method)
      Execute an HTTP request to the URL supplied in the constructor, with the properties and (optionally) request body provided.
      Parameters:
      method - The HTTP method to execute (GET, PUT, POST or HEADER)
    • getResponseCode

      public int getResponseCode()
      Get the server's response code e.g. 200 (OK)
      Returns:
      The response code returned by the server
    • getResponseHeaderFields

      public Map<String,List<String>> getResponseHeaderFields()
      Get the header fields of the server's response
      Returns:
      The header fields returned by the server
    • getContentType

      public String getContentType()
      Get the content type received for the HTTP request
      Returns:
      The content-type string
    • getResponseData

      public byte[] getResponseData()
      Get the body of the server's response
      Returns:
      The complete response body returned by the server
    • getErrorMessage

      public String getErrorMessage()
      Get the error message of a failed request
      Returns:
      An error message describing the reason for a failed request (empty string if none)
    • setHttpConnection

      public void setHttpConnection(HttpURLConnection connection)