Class CurlRequest

java.lang.Object
org.codelibs.curl.CurlRequest

public class CurlRequest extends Object
The CurlRequest class represents an HTTP request that can be configured and executed. It supports various HTTP methods, request parameters, headers, body content, and more.
  • Field Details

    • GZIP

      protected static final String GZIP
      The GZIP compression type.
      See Also:
    • logger

      protected static final Logger logger
      Logger for logging request details.
    • url

      protected String url
      The URL for the HTTP request.
    • proxy

      protected Proxy proxy
      The proxy to be used for the HTTP request.
    • encoding

      protected String encoding
      The character encoding for the request.
    • threshold

      protected int threshold
      The threshold size for the request body.
    • method

      protected Curl.Method method
      The HTTP method for the request.
    • paramList

      protected List<String> paramList
      The list of request parameters.
    • headerList

      protected List<String[]> headerList
      The list of request headers.
    • body

      protected String body
      The body content of the request.
    • bodyStream

      protected InputStream bodyStream
      The input stream for the request body.
    • compression

      protected String compression
      The compression type for the request.
    • sslSocketFactory

      protected SSLSocketFactory sslSocketFactory
      The SSL socket factory for secure connections.
    • threadPool

      protected ForkJoinPool threadPool
      The thread pool for executing the request.
  • Constructor Details

    • CurlRequest

      public CurlRequest(Curl.Method method, String url)
      Constructs a new CurlRequest with the specified HTTP method and URL.
      Parameters:
      method - the HTTP method
      url - the URL
  • Method Details

    • proxy

      public Proxy proxy()
      Returns the proxy for the request.
      Returns:
      the proxy
    • encoding

      public String encoding()
      Returns the character encoding for the request.
      Returns:
      the encoding
    • threshold

      public int threshold()
      Returns the threshold size for the request body.
      Returns:
      the threshold
    • method

      public Curl.Method method()
      Returns the HTTP method for the request.
      Returns:
      the method
    • body

      public String body()
      Returns the body content of the request.
      Returns:
      the body
    • proxy

      public CurlRequest proxy(Proxy proxy)
      Sets the proxy for the request.
      Parameters:
      proxy - the proxy
      Returns:
      this CurlRequest instance
    • encoding

      public CurlRequest encoding(String encoding)
      Sets the character encoding for the request.
      Parameters:
      encoding - the encoding
      Returns:
      this CurlRequest instance
      Throws:
      CurlException - if the method is called after the param method
    • threshold

      public CurlRequest threshold(int threshold)
      Sets the threshold size for the request body.
      Parameters:
      threshold - the threshold
      Returns:
      this CurlRequest instance
    • gzip

      public CurlRequest gzip()
      Enables GZIP compression for the request.
      Returns:
      this CurlRequest instance
    • compression

      public CurlRequest compression(String compression)
      Sets the compression type for the request.
      Parameters:
      compression - the compression type
      Returns:
      this CurlRequest instance
    • sslSocketFactory

      public CurlRequest sslSocketFactory(SSLSocketFactory sslSocketFactory)
      Sets the SSL socket factory for secure connections.
      Parameters:
      sslSocketFactory - the SSL socket factory
      Returns:
      this CurlRequest instance
    • body

      public CurlRequest body(String body)
      Sets the body content for the request.
      Parameters:
      body - the body content
      Returns:
      this CurlRequest instance
      Throws:
      CurlException - if the body method is already called
    • body

      public CurlRequest body(InputStream stream)
      Sets the input stream for the request body.
      Parameters:
      stream - the input stream
      Returns:
      this CurlRequest instance
      Throws:
      CurlException - if the body method is already called
    • onConnect

      public CurlRequest onConnect(BiConsumer<CurlRequest,HttpURLConnection> connectionBuilder)
      Sets the connection builder for customizing the connection.
      Parameters:
      connectionBuilder - the connection builder
      Returns:
      this CurlRequest instance
    • param

      public CurlRequest param(String key, String value)
      Adds a request parameter.
      Parameters:
      key - the parameter key
      value - the parameter value
      Returns:
      this CurlRequest instance
    • header

      public CurlRequest header(String key, String value)
      Adds a request header.
      Parameters:
      key - the header key
      value - the header value
      Returns:
      this CurlRequest instance
    • connect

      public void connect(Consumer<HttpURLConnection> actionListener, Consumer<Exception> exceptionListener)
      Connects to the URL and executes the request.
      Parameters:
      actionListener - the action listener for handling the response
      exceptionListener - the exception listener for handling exceptions
    • open

      protected HttpURLConnection open(URL u) throws IOException
      Opens a connection to the specified URL.
      Parameters:
      u - the URL
      Returns:
      the HttpURLConnection
      Throws:
      IOException - if an I/O exception occurs
    • execute

      public void execute(Consumer<CurlResponse> actionListener, Consumer<Exception> exceptionListener)
      Executes the request and processes the response.
      Parameters:
      actionListener - the action listener for handling the response
      exceptionListener - the exception listener for handling exceptions
    • execute

      public CurlResponse execute()
      Executes the request and returns the response.
      Returns:
      the CurlResponse
    • encode

      protected String encode(String value)
      Encodes the specified value using the character encoding.
      Parameters:
      value - the value to encode
      Returns:
      the encoded value
      Throws:
      CurlException - if the encoding is unsupported
    • threadPool

      public CurlRequest threadPool(ForkJoinPool threadPool)
      Sets the thread pool for executing the request.
      Parameters:
      threadPool - the thread pool
      Returns:
      this CurlRequest instance