Class Client

java.lang.Object
io.github.parthappm.http.client.Client
Direct Known Subclasses:
HttpClient, HttpsClient

public class Client extends Object
A client class to implement the HTTP client functionality. The object of this class cannot be created directly, either create an object of HttpClient of HttpsClient class.
  • Method Details

    • setSocket

      protected void setSocket(Socket socket)
      Setter method for socket object.
      Parameters:
      socket - The socket that will be used for HTTP protocol
    • suppressException

      public Client suppressException(boolean suppressException)
      Setter method for flag to suppress any kind of exception.
      Parameters:
      suppressException - The flag to suppress exceptions
      Returns:
      The reference of the current object for chaining
    • keepConnectionOpen

      public Client keepConnectionOpen(boolean keepConnectionOpen)
      Setter method for flag to keep the connection open for the socket.
      Parameters:
      keepConnectionOpen - The flag whether to keep the connection open or close the socket
      Returns:
      The reference of the current object for chaining
    • followRedirect

      public Client followRedirect(boolean followRedirect)
      Setter method for flag to whether follow redirect HTTP response.
      Parameters:
      followRedirect - The flag whether to make the next request depending on HTTP response
      Returns:
      The reference of the current object for chaining
    • setMethod

      public Client setMethod(String method)
      Setter method to set the HTTP Method.
      Parameters:
      method - The HTTP method to be used
      Returns:
      The reference of the current object for chaining
    • setPath

      public Client setPath(String path)
      Setter method to set the path to be used after the host in url.
      Parameters:
      path - The url path
      Returns:
      The reference of the current object for chaining
    • addParameter

      public Client addParameter(String key, String value)
      Setter method to add or modify a single parameter to the existing list of parameters. To delete a parameter, set the value to null.
      Parameters:
      key - The parameter key
      value - The parameter value
      Returns:
      The reference of the current object for chaining
    • setParameters

      public Client setParameters(Map<String,String> parameters)
      Setter method to add or modify multiple parameters to the existing list of parameters. To delete a parameter, set the value to null.
      Parameters:
      parameters - The map of new or extra parameters that are to be added to the existing list of parameters
      Returns:
      The reference of the current object for chaining
    • addHeader

      public Client addHeader(String key, String value)
      Setter method to add or modify a single header to the existing list of headers. To delete a header, set the value to null.
      Parameters:
      key - The header name
      value - The header value
      Returns:
      The reference of the current object for chaining
    • setHeader

      public Client setHeader(Map<String,String> headers)
      Setter method add or modify multiple headers to the existing list of parameters. To delete a parameter, set the value to null.
      Parameters:
      headers - The map of new or extra headers that are to be added to the existing list of headers
      Returns:
      The reference of the current object for chaining
    • setBody

      public Client setBody(byte[] body)
      Setter method to set the request body
      Parameters:
      body - Bytes array representing the request body
      Returns:
      The reference of the current object for chaining
    • request

      public Response request() throws IOException
      This method has to be called to make the HTTP request to the server after setting the required parameters
      Returns:
      Response object that contains all the details of the HTTP response for this particular request
      Throws:
      IOException - If any IOException occurs in the socket connection
    • close

      public void close() throws IOException
      Closes the socket connection to the server
      Throws:
      IOException - If any IOException occurs in the process of closing the connection