Class HttpClientUtils


  • public class HttpClientUtils
    extends java.lang.Object
    Utility class for the jakarta common HttpClient
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int DEFAULT_TIMEOUT
      Default timeout value (in milliseconds).
      Used for socket timeout, connect timeout, and connection request timeout.
      static int HTTP_TIMEOUT  
      static java.lang.String HTTP_USER_AGENT  
    • Constructor Summary

      Constructors 
      Constructor Description
      HttpClientUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static java.lang.String executeMethodAndGetAsStringOrAsFile​(org.apache.http.impl.client.CloseableHttpClient client, org.apache.http.client.methods.HttpRequestBase request, java.io.File file)  
      static java.lang.String executeMethodAndGetContentAsStringOrAsFile​(org.apache.commons.httpclient.HttpClient client, org.apache.commons.httpclient.HttpMethodBase method, java.io.File file)  
      static java.lang.String get​(java.lang.String url)
      Retrieves the content of the specified url as a string.
      static java.lang.String get​(java.lang.String url, int timeout, java.lang.String userAgent)
      Retrieves the content of the specified url.
      static void get​(java.lang.String url, java.io.File file)
      Save the content of the specified url in the given file.
      static void get​(java.lang.String url, java.io.File file, int timeout)
      Save the content of the specified url in the given file.
      static void get​(java.lang.String url, java.io.File file, int timeout, java.lang.String userAgent)
      Save the content of the specified url in the given file.
      static void get​(java.lang.String url, java.io.File file, int timeout, java.lang.String userAgent, java.lang.String accept)
      Save the content of the specified url in the given file.
      static void get​(java.lang.String url, java.io.File file, java.lang.String accept)
      Save the content of the specified url in the given file.
      static java.lang.String get​(java.lang.String url, java.lang.String userAgent)
      Retrieves the content of the specified url.
      static java.lang.String get​(java.lang.String url, java.lang.String userAgent, java.lang.String accept)
      Retrieves the content of the specified url.
      static java.lang.String getContent​(java.lang.String url)
      Deprecated.
      static java.lang.String getContent​(java.lang.String url, int timeout, java.lang.String userAgent)
      Deprecated.
      static void getContent​(java.lang.String url, java.io.File file)
      Deprecated.
      static void getContent​(java.lang.String url, java.io.File file, int timeout)
      Deprecated.
      static void getContent​(java.lang.String url, java.io.File file, int timeout, java.lang.String userAgent)
      static java.lang.String getContent​(java.lang.String url, java.lang.String userAgent)
      Deprecated.
      static org.apache.commons.httpclient.HttpClient getNewHttpClient​(java.lang.String url, int timeout)
      Deprecated.
      this method use legacy HttpClient 3.x, use newHttpClient()
      static org.apache.http.impl.client.CloseableHttpClient newHttpClient()
      Retrieve a new HttpClient instance preconfigured with JCMS settings.
      static org.apache.http.impl.client.CloseableHttpClient newHttpClient​(int timeout)
      Retrieve a new HttpClient instance preconfigured with JCMS settings.
      static java.lang.String post​(java.lang.String url, int timeout, java.lang.String userAgent, java.util.List<? extends org.apache.http.NameValuePair> parameters)
      Post the specified data to the specified url and retrieve the content as a string.
      static java.lang.String post​(java.lang.String url, int timeout, java.lang.String userAgent, java.util.List<? extends org.apache.http.NameValuePair> parameters, java.lang.String charset)
      Post the specified data to the specified url and retrieve the content as a string.
      static java.lang.String post​(java.lang.String url, int timeout, java.lang.String userAgent, java.util.List<? extends org.apache.http.NameValuePair> parameters, java.lang.String charset, java.lang.String accept)
      Post the specified data to the specified url and retrieve the content as a string.
      static java.lang.String post​(java.lang.String url, int timeout, java.lang.String userAgent, java.util.List<? extends org.apache.http.NameValuePair> parameters, java.util.Map<java.lang.String,​java.lang.String> headers, java.lang.String charset)
      Post the specified data to the specified url and retrieve the content as a string.
      static java.lang.String post​(java.lang.String url, int timeout, java.lang.String userAgent, java.util.List<? extends org.apache.http.NameValuePair> parameters, java.util.Map<java.lang.String,​java.lang.String> headers, java.lang.String charset, java.lang.String accept)
      Post the specified data to the specified url and retrieve the content as a string.
      static java.lang.String postAndGetContent​(java.lang.String url, int timeout, java.lang.String userAgent, org.apache.commons.httpclient.NameValuePair[] data)
      Post the specified data to the specified url and retrieve the content as a string.
      static java.lang.String postAndGetContent​(java.lang.String url, int timeout, java.lang.String userAgent, org.apache.commons.httpclient.NameValuePair[] data, java.lang.String charset)
      Post the specified data to the specified url and retrieve the content as a string.
      static java.lang.String postAndGetContentAsStringOrAsFile​(java.lang.String url, java.io.File file, int timeout, java.lang.String userAgent, org.apache.commons.httpclient.NameValuePair[] data, java.lang.String charset)  
      static void setProxyParamsForUrl​(org.apache.commons.httpclient.HttpClient client, java.lang.String remoteUrl)
      Deprecated.
      this method use legacy HttpClient 3.x, use newHttpClient()
      static void setProxyParamsForUrl​(org.apache.commons.httpclient.HttpClient client, java.net.URL remoteURL)
      Deprecated.
      this method use legacy HttpClient 3.x, use newHttpClient()
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • 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 java.lang.String HTTP_USER_AGENT
    • Constructor Detail

      • HttpClientUtils

        public HttpClientUtils()
    • Method Detail

      • getNewHttpClient

        @Deprecated
        public static org.apache.commons.httpclient.HttpClient getNewHttpClient​(java.lang.String url,
                                                                                int timeout)
                                                                         throws java.net.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:
        java.net.MalformedURLException - if an error occurred.
      • setProxyParamsForUrl

        @Deprecated
        public static void setProxyParamsForUrl​(org.apache.commons.httpclient.HttpClient client,
                                                java.lang.String remoteUrl)
                                         throws java.net.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:
        java.net.MalformedURLException - if an error occurred.
      • setProxyParamsForUrl

        @Deprecated
        public static void setProxyParamsForUrl​(org.apache.commons.httpclient.HttpClient client,
                                                java.net.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 java.lang.String getContent​(java.lang.String url)
                                           throws org.apache.commons.httpclient.HttpException,
                                                  java.io.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.
        java.io.IOException - if an error occurred.
        Since:
        jcms-5.7.2
      • getContent

        @Deprecated
        public static java.lang.String getContent​(java.lang.String url,
                                                  java.lang.String userAgent)
                                           throws org.apache.commons.httpclient.HttpException,
                                                  java.io.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.
        java.io.IOException - if an error occurred.
        Since:
        jcms-5.7.2
      • getContent

        @Deprecated
        public static java.lang.String getContent​(java.lang.String url,
                                                  int timeout,
                                                  java.lang.String userAgent)
                                           throws org.apache.commons.httpclient.HttpException,
                                                  java.io.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.
        java.io.IOException - if an error occurred.
        Since:
        jcms-5.7.2
      • getContent

        @Deprecated
        public static void getContent​(java.lang.String url,
                                      java.io.File file)
                               throws org.apache.commons.httpclient.HttpException,
                                      java.io.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.
        java.io.IOException - if an error occurred.
        Since:
        jcms-5.7.2
      • getContent

        @Deprecated
        public static void getContent​(java.lang.String url,
                                      java.io.File file,
                                      int timeout)
                               throws org.apache.commons.httpclient.HttpException,
                                      java.io.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.
        java.io.IOException - if an error occurred.
        Since:
        jcms-5.7.2
      • getContent

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

        public static java.lang.String postAndGetContent​(java.lang.String url,
                                                         int timeout,
                                                         java.lang.String userAgent,
                                                         org.apache.commons.httpclient.NameValuePair[] data)
                                                  throws java.io.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:
        java.io.IOException - if an error occurred.
        Since:
        jcms-5.7.5
      • postAndGetContent

        public static java.lang.String postAndGetContent​(java.lang.String url,
                                                         int timeout,
                                                         java.lang.String userAgent,
                                                         org.apache.commons.httpclient.NameValuePair[] data,
                                                         java.lang.String charset)
                                                  throws java.io.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:
        java.io.IOException - if an error occurred.
        Since:
        jcms-5.7.5
      • postAndGetContentAsStringOrAsFile

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

        public static java.lang.String executeMethodAndGetContentAsStringOrAsFile​(org.apache.commons.httpclient.HttpClient client,
                                                                                  org.apache.commons.httpclient.HttpMethodBase method,
                                                                                  java.io.File file)
                                                                           throws org.apache.commons.httpclient.HttpException,
                                                                                  java.io.IOException
        Throws:
        org.apache.commons.httpclient.HttpException
        java.io.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 java.lang.String get​(java.lang.String url)
                                    throws java.io.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:
        java.io.IOException - if request failed
        Since:
        jcms-10.0.1 / JCMS-6434
      • get

        public static java.lang.String get​(java.lang.String url,
                                           java.lang.String userAgent)
                                    throws java.io.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:
        java.io.IOException - if request failed
        Since:
        jcms-10.0.1 / JCMS-6434
      • get

        public static java.lang.String get​(java.lang.String url,
                                           java.lang.String userAgent,
                                           java.lang.String accept)
                                    throws java.io.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:
        java.io.IOException - if request failed
        Since:
        jcms-10.0.8 / JCMS-10847
      • get

        public static java.lang.String get​(java.lang.String url,
                                           int timeout,
                                           java.lang.String userAgent)
                                    throws java.io.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:
        java.io.IOException - if request failed
        Since:
        jcms-10.0.1 / JCMS-6434
      • get

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

        public static void get​(java.lang.String url,
                               java.io.File file,
                               java.lang.String accept)
                        throws java.io.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:
        java.io.IOException - if request failed or if output file could not be written
        Since:
        jcms-10.0.8 / JCMS-10847
      • get

        public static void get​(java.lang.String url,
                               java.io.File file,
                               int timeout)
                        throws java.io.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:
        java.io.IOException - if request failed or if output file could not be written
        Since:
        jcms-10.0.1 / JCMS-6434
      • get

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

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

        public static java.lang.String post​(java.lang.String url,
                                            int timeout,
                                            java.lang.String userAgent,
                                            java.util.List<? extends org.apache.http.NameValuePair> parameters)
                                     throws java.io.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:
        java.io.IOException - if request failed
        Since:
        jcms-10.0.1 / JCMS-6434
      • post

        public static java.lang.String post​(java.lang.String url,
                                            int timeout,
                                            java.lang.String userAgent,
                                            java.util.List<? extends org.apache.http.NameValuePair> parameters,
                                            java.lang.String charset)
                                     throws java.io.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:
        java.io.IOException - if request failed
        Since:
        jcms-10.0.1 / JCMS-6434
      • post

        public static java.lang.String post​(java.lang.String url,
                                            int timeout,
                                            java.lang.String userAgent,
                                            java.util.List<? extends org.apache.http.NameValuePair> parameters,
                                            java.lang.String charset,
                                            java.lang.String accept)
                                     throws java.io.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:
        java.io.IOException - if request failed
        Since:
        jcms-10.0.8 / JCMS-10847
      • post

        public static java.lang.String post​(java.lang.String url,
                                            int timeout,
                                            java.lang.String userAgent,
                                            java.util.List<? extends org.apache.http.NameValuePair> parameters,
                                            java.util.Map<java.lang.String,​java.lang.String> headers,
                                            java.lang.String charset)
                                     throws java.io.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:
        java.io.IOException - if request failed
        Since:
        jcms-10.0.1 / JCMS-6434
      • post

        public static java.lang.String post​(java.lang.String url,
                                            int timeout,
                                            java.lang.String userAgent,
                                            java.util.List<? extends org.apache.http.NameValuePair> parameters,
                                            java.util.Map<java.lang.String,​java.lang.String> headers,
                                            java.lang.String charset,
                                            java.lang.String accept)
                                     throws java.io.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:
        java.io.IOException - if request failed
        Since:
        jcms-10.0.8 / JCMS-10847
      • executeMethodAndGetAsStringOrAsFile

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