Class HttpClientUtils

java.lang.Object
com.jalios.util.HttpClientUtils

public class HttpClientUtils extends Object
Utility class for the jakarta common HttpClient
  • Field Details

    • HTTP_TIMEOUT

      public static int HTTP_TIMEOUT
    • DEFAULT_TIMEOUT

      public static int DEFAULT_TIMEOUT
      Default timeout value (in milliseconds).
      Used for socket timeout, connect timeout, and connection request timeout.
      Since:
      10.0.1 / JCMS-6434
    • HTTP_USER_AGENT

      public static String HTTP_USER_AGENT
  • Constructor Details

    • HttpClientUtils

      public HttpClientUtils()
  • Method Details

    • getNewHttpClient

      @Deprecated public static org.apache.commons.httpclient.HttpClient getNewHttpClient(String url, int timeout) throws MalformedURLException
      Deprecated.
      this method use legacy HttpClient 3.x, use newHttpClient()
      Retrieve a new configured HttpClient instance. The following configuration are performed : - socket and http timeout value are set - proxy param will be configured if required for the specified URL.
      Parameters:
      url - the url that will be accessed with this client
      timeout - the timeout value to use
      Returns:
      a new HttpClient instance
      Throws:
      MalformedURLException - if an error occurred.
    • setProxyParamsForUrl

      @Deprecated public static void setProxyParamsForUrl(org.apache.commons.httpclient.HttpClient client, String remoteUrl) throws MalformedURLException
      Deprecated.
      this method use legacy HttpClient 3.x, use newHttpClient()
      Set HttpClient proxy parameters based on Java Properties.
      Parameters:
      client - the HttpClient instance of which proxy parameters
      remoteUrl - the remote url for which the HttpClient is being used. It is required in order to be able to apply the rules specified in the system property "http.nonProxyHosts"
      Throws:
      MalformedURLException - if an error occurred.
    • setProxyParamsForUrl

      @Deprecated public static void setProxyParamsForUrl(org.apache.commons.httpclient.HttpClient client, URL remoteURL)
      Deprecated.
      this method use legacy HttpClient 3.x, use newHttpClient()
      Set HttpClient proxy parameters based on Java Properties.
      Parameters:
      client - the HttpClient instance of which proxy parameters
      remoteURL - the remote URL for which the HttpClient is being used. It is required in order to be able to apply the rules specified in the system property "http.nonProxyHosts"
    • getContent

      @Deprecated public static String getContent(String url) throws org.apache.commons.httpclient.HttpException, IOException
      Deprecated.
      Retrieves the content of the specified url as a string.

      Warning: The size of the response body is not controlled and may consume a lot of memory.

      Parameters:
      url - the url to access
      Returns:
      the content retrieved as a string
      Throws:
      org.apache.commons.httpclient.HttpException - if an error occurred.
      IOException - if an error occurred.
      Since:
      jcms-5.7.2
    • getContent

      @Deprecated public static String getContent(String url, String userAgent) throws org.apache.commons.httpclient.HttpException, IOException
      Deprecated.
      Retrieves the content of the specified url.

      Warning: The size of the response body is not controlled and may consume a lot of memory.

      Parameters:
      url - the url to access
      userAgent - the User-Agent
      Returns:
      the content retrieved as a string
      Throws:
      org.apache.commons.httpclient.HttpException - if an error occurred.
      IOException - if an error occurred.
      Since:
      jcms-5.7.2
    • getContent

      @Deprecated public static String getContent(String url, int timeout, String userAgent) throws org.apache.commons.httpclient.HttpException, IOException
      Deprecated.
      Retrieves the content of the specified url.

      Warning: The size of the response body is not controlled and may consume a lot of memory.

      Parameters:
      url - the url to access
      timeout - the time (in milliseconds) to wait for a connection to become available. Default value is 3000 (3 seconds). Default value will be used if zero or a negative value is specified.
      userAgent - the User-Agent
      Returns:
      the content retrieved as a string
      Throws:
      org.apache.commons.httpclient.HttpException - if an error occurred.
      IOException - if an error occurred.
      Since:
      jcms-5.7.2
    • getContent

      @Deprecated public static void getContent(String url, File file) throws org.apache.commons.httpclient.HttpException, IOException
      Deprecated.
      Save the content of the specified url in the given file.
      Parameters:
      url - the url to access
      file - the output file
      Throws:
      org.apache.commons.httpclient.HttpException - if an error occurred.
      IOException - if an error occurred.
      Since:
      jcms-5.7.2
    • getContent

      @Deprecated public static void getContent(String url, File file, int timeout) throws org.apache.commons.httpclient.HttpException, IOException
      Deprecated.
      Save the content of the specified url in the given file.
      Parameters:
      url - the url to access
      file - the output file
      timeout - the time (in milliseconds) to wait for a connection to become available. Default value is 3000 (3 seconds). Default value will be used if zero or a negative value is specified.
      Throws:
      org.apache.commons.httpclient.HttpException - if an error occurred.
      IOException - if an error occurred.
      Since:
      jcms-5.7.2
    • getContent

      @Deprecated public static void getContent(String url, File file, int timeout, String userAgent) throws org.apache.commons.httpclient.HttpException, IOException
      Save the content of the specified url in the given file.
      Parameters:
      url - the url to access
      file - the output file
      timeout - the connection timeout
      userAgent - the User-Agent
      Throws:
      org.apache.commons.httpclient.HttpException - if an error occurred.
      IOException - if an error occurred.
      Since:
      jcms-5.7.2
    • postAndGetContent

      public static String postAndGetContent(String url, int timeout, String userAgent, org.apache.commons.httpclient.NameValuePair[] data) throws IOException
      Post the specified data to the specified url and retrieve the content as a string.

      Warning: The size of the response body is not controlled and may consume a lot of memory. Charset encoding used is the default one in HTTP : ISO-8859-1

      Parameters:
      url - the url to access
      timeout - the time (in milliseconds) to wait for a connection to become available. Default value is 3000 (3 seconds). Default value will be used if zero or a negative value is specified.
      userAgent - the User-Agent
      data - the parameters to be posted to the remote url
      Returns:
      the content retrieved as a string
      Throws:
      IOException - if an error occurred.
      Since:
      jcms-5.7.5
    • postAndGetContent

      public static String postAndGetContent(String url, int timeout, String userAgent, org.apache.commons.httpclient.NameValuePair[] data, String charset) throws IOException
      Post the specified data to the specified url and retrieve the content as a string.

      Warning: The size of the response body is not controlled and may consume a lot of memory.

      Parameters:
      url - the url to access
      timeout - the time (in milliseconds) to wait for a connection to become available. Default value is 3000 (3 seconds). Default value will be used if zero or a negative value is specified.
      userAgent - the User-Agent
      data - the parameters to be posted to the remote url
      charset - the charset encoding to use. If null, uses the default one in HTTP : ISO-8859-1
      Returns:
      the content retrieved as a string or an empty String if the StatusCode is lesser than 200 or greater or equal to 300
      Throws:
      IOException - if an error occurred.
      Since:
      jcms-5.7.5
    • postAndGetContentAsStringOrAsFile

      public static String postAndGetContentAsStringOrAsFile(String url, File file, int timeout, String userAgent, org.apache.commons.httpclient.NameValuePair[] data, String charset) throws org.apache.commons.httpclient.HttpException, IOException
      Throws:
      org.apache.commons.httpclient.HttpException
      IOException
    • executeMethodAndGetContentAsStringOrAsFile

      public static String executeMethodAndGetContentAsStringOrAsFile(org.apache.commons.httpclient.HttpClient client, org.apache.commons.httpclient.HttpMethodBase method, File file) throws org.apache.commons.httpclient.HttpException, IOException
      Throws:
      org.apache.commons.httpclient.HttpException
      IOException
    • newHttpClient

      public static org.apache.http.impl.client.CloseableHttpClient newHttpClient()
      Retrieve a new HttpClient instance preconfigured with JCMS settings.
      Returns:
      a new HttpClient instance, never returns null
      Since:
      jcms-10.0.1 / JCMS-6434
    • newHttpClient

      public static org.apache.http.impl.client.CloseableHttpClient newHttpClient(int timeout)
      Retrieve a new HttpClient instance preconfigured with JCMS settings.

      The following configuration are performed :

      • system properties are used to create the HttpClient, including proxy settings
      • system properties are used to read login/password of proxy if any
      • socket, connect and connexion request timeout are configured from the specified timeout value if positive, otherwise DEFAULT_TIMEOUT is used.
      Parameters:
      timeout - the timeout value to use, in milliseconds (for socket, connect and connexion request timeout),
      Returns:
      a new HttpClient instance, never returns null
      Since:
      jcms-10.0.1 / JCMS-6434
    • get

      public static String get(String url) throws IOException
      Retrieves the content of the specified url as a string.

      Warning: The size of the response body is not controlled and may consume a lot of memory.

      Parameters:
      url - the url to access
      Returns:
      the content retrieved as a string
      Throws:
      IOException - if request failed
      Since:
      jcms-10.0.1 / JCMS-6434
    • get

      public static String get(String url, String userAgent) throws IOException
      Retrieves the content of the specified url.

      Warning: The size of the response body is not controlled and may consume a lot of memory.

      Parameters:
      url - the url to access
      userAgent - the User-Agent
      Returns:
      the content retrieved as a string
      Throws:
      IOException - if request failed
      Since:
      jcms-10.0.1 / JCMS-6434
    • get

      public static String get(String url, String userAgent, String accept) throws IOException
      Retrieves the content of the specified url.

      Warning: The size of the response body is not controlled and may consume a lot of memory.

      Parameters:
      url - the url to access
      userAgent - the User-Agent
      accept - the value of accept header
      Returns:
      the content retrieved as a string
      Throws:
      IOException - if request failed
      Since:
      jcms-10.0.8 / JCMS-10847
    • get

      public static String get(String url, int timeout, String userAgent) throws IOException
      Retrieves the content of the specified url.

      Warning: The size of the response body is not controlled and may consume a lot of memory.

      Parameters:
      url - the url to access
      timeout - the time (in milliseconds) to wait for a connection to become available. Default value is 3000 (3 seconds). Default value will be used if zero or a negative value is specified.
      userAgent - the User-Agent
      Returns:
      the content retrieved as a string
      Throws:
      IOException - if request failed
      Since:
      jcms-10.0.1 / JCMS-6434
    • get

      public static void get(String url, File file) throws IOException
      Save the content of the specified url in the given file.
      Parameters:
      url - the url to access
      file - the output file
      Throws:
      IOException - if request failed or if output file could not be written
      Since:
      jcms-10.0.1 / JCMS-6434
    • get

      public static void get(String url, File file, String accept) throws IOException
      Save the content of the specified url in the given file.
      Parameters:
      url - the url to access
      file - the output file
      accept - the value of accept header
      Throws:
      IOException - if request failed or if output file could not be written
      Since:
      jcms-10.0.8 / JCMS-10847
    • get

      public static void get(String url, File file, int timeout) throws IOException
      Save the content of the specified url in the given file.
      Parameters:
      url - the url to access
      file - the output file
      timeout - the time (in milliseconds) to wait for a connection to become available. Default value is 3000 (3 seconds). Default value will be used if zero or a negative value is specified.
      Throws:
      IOException - if request failed or if output file could not be written
      Since:
      jcms-10.0.1 / JCMS-6434
    • get

      public static void get(String url, File file, int timeout, String userAgent) throws IOException
      Save the content of the specified url in the given file.
      Parameters:
      url - the url to access
      file - the output file
      timeout - the connection timeout
      userAgent - the User-Agent
      Throws:
      IOException - if request failed or if output file could not be written
      Since:
      jcms-10.0.1 / JCMS-6434
    • get

      public static void get(String url, File file, int timeout, String userAgent, String accept) throws IOException
      Save the content of the specified url in the given file.
      Parameters:
      url - the url to access
      file - the output file
      timeout - the connection timeout
      userAgent - the User-Agent
      accept - the value of accept header
      Throws:
      IOException - if request failed or if output file could not be written
      Since:
      jcms-10.0.8 / JCMS-10847
    • post

      public static String post(String url, int timeout, String userAgent, List<? extends org.apache.http.NameValuePair> parameters) throws IOException
      Post the specified data to the specified url and retrieve the content as a string.

      Warning: The size of the response body is not controlled and may consume a lot of memory. Charset encoding used is the default one in HTTP : ISO-8859-1

      Parameters:
      url - the url to access
      timeout - the time (in milliseconds) to wait for a connection to become available. Default value is 3000 (3 seconds). Default value will be used if zero or a negative value is specified.
      userAgent - the User-Agent
      parameters - the parameters to be posted to the remote url
      Returns:
      the content retrieved as a string
      Throws:
      IOException - if request failed
      Since:
      jcms-10.0.1 / JCMS-6434
    • post

      public static String post(String url, int timeout, String userAgent, List<? extends org.apache.http.NameValuePair> parameters, String charset) throws IOException
      Post the specified data to the specified url and retrieve the content as a string.

      Warning: The size of the response body is not controlled and may consume a lot of memory.

      Parameters:
      url - the url to access
      timeout - the time (in milliseconds) to wait for a connection to become available. Default value is 3000 (3 seconds). Default value will be used if zero or a negative value is specified.
      userAgent - the User-Agent
      parameters - the parameters to be posted to the remote url
      charset - the charset encoding to use. If null, uses the default one in HTTP : ISO-8859-1
      Returns:
      the content retrieved as a string or an empty String if the StatusCode is lesser than 200 or greater or equal to 300
      Throws:
      IOException - if request failed
      Since:
      jcms-10.0.1 / JCMS-6434
    • post

      public static String post(String url, int timeout, String userAgent, List<? extends org.apache.http.NameValuePair> parameters, String charset, String accept) throws IOException
      Post the specified data to the specified url and retrieve the content as a string.

      Warning: The size of the response body is not controlled and may consume a lot of memory.

      Parameters:
      url - the url to access
      timeout - the time (in milliseconds) to wait for a connection to become available. Default value is 3000 (3 seconds). Default value will be used if zero or a negative value is specified.
      userAgent - the User-Agent
      parameters - the parameters to be posted to the remote url
      charset - the charset encoding to use. If null, uses the default one in HTTP : ISO-8859-1
      accept - the value of accept header
      Returns:
      the content retrieved as a string or an empty String if the StatusCode is lesser than 200 or greater or equal to 300
      Throws:
      IOException - if request failed
      Since:
      jcms-10.0.8 / JCMS-10847
    • post

      public static String post(String url, int timeout, String userAgent, List<? extends org.apache.http.NameValuePair> parameters, Map<String,String> headers, String charset) throws IOException
      Post the specified data to the specified url and retrieve the content as a string.

      Warning: The size of the response body is not controlled and may consume a lot of memory.

      Parameters:
      url - the url to access
      timeout - the time (in milliseconds) to wait for a connection to become available. Default value is 3000 (3 seconds). Default value will be used if zero or a negative value is specified.
      userAgent - the User-Agent
      parameters - the parameters to be posted to the remote url
      headers - a map of optional headers of the request
      charset - the charset encoding to use. If null, uses the default one in HTTP : ISO-8859-1
      Returns:
      the content retrieved as a string or an empty String if the StatusCode is lesser than 200 or greater or equal to 300
      Throws:
      IOException - if request failed
      Since:
      jcms-10.0.1 / JCMS-6434
    • post

      public static String post(String url, int timeout, String userAgent, List<? extends org.apache.http.NameValuePair> parameters, Map<String,String> headers, String charset, String accept) throws IOException
      Post the specified data to the specified url and retrieve the content as a string.

      Warning: The size of the response body is not controlled and may consume a lot of memory.

      Parameters:
      url - the url to access
      timeout - the time (in milliseconds) to wait for a connection to become available. Default value is 3000 (3 seconds). Default value will be used if zero or a negative value is specified.
      userAgent - the User-Agent
      parameters - the parameters to be posted to the remote url
      headers - a map of optional headers of the request
      charset - the charset encoding to use. If null, uses the default one in HTTP : ISO-8859-1
      accept - the value of accept header
      Returns:
      the content retrieved as a string or an empty String if the StatusCode is lesser than 200 or greater or equal to 300
      Throws:
      IOException - if request failed
      Since:
      jcms-10.0.8 / JCMS-10847
    • executeMethodAndGetAsStringOrAsFile

      public static String executeMethodAndGetAsStringOrAsFile(org.apache.http.impl.client.CloseableHttpClient client, org.apache.http.client.methods.HttpRequestBase request, File file) throws IOException
      Throws:
      IOException