Class HttpClientUtils
- java.lang.Object
-
- com.jalios.util.HttpClientUtils
-
public class HttpClientUtils extends java.lang.Object
Utility class for the jakarta common HttpClient- Version:
- $Revision: 113047 $
-
-
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
static java.lang.String
REVISION
-
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
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 java.lang.String
get(java.lang.String url, java.lang.String userAgent)
Retrieves the content of the specified url.static java.lang.String
getContent(java.lang.String url)
Deprecated.useget(String)
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)
Deprecated.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, usenewHttpClient()
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
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, usenewHttpClient()
static void
setProxyParamsForUrl(org.apache.commons.httpclient.HttpClient client, java.net.URL remoteURL)
Deprecated.this method use legacy HttpClient 3.x, usenewHttpClient()
-
-
-
Field Detail
-
REVISION
public static final java.lang.String REVISION
- See Also:
- Constant Field Values
-
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
-
-
Method Detail
-
getNewHttpClient
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, usenewHttpClient()
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 clienttimeout
- the timeout value to use- Returns:
- a new HttpClient instance
- Throws:
java.net.MalformedURLException
- if an error occurred.
-
setProxyParamsForUrl
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, usenewHttpClient()
Set HttpClient proxy parameters based on Java Properties.- Parameters:
client
- the HttpClient instance of which proxy parametersremoteUrl
- 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
public static void setProxyParamsForUrl(org.apache.commons.httpclient.HttpClient client, java.net.URL remoteURL)
Deprecated.this method use legacy HttpClient 3.x, usenewHttpClient()
Set HttpClient proxy parameters based on Java Properties.- Parameters:
client
- the HttpClient instance of which proxy parametersremoteURL
- 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
public static java.lang.String getContent(java.lang.String url) throws org.apache.commons.httpclient.HttpException, java.io.IOException
Deprecated.useget(String)
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
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 accessuserAgent
- 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
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 accesstimeout
- 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
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 accessfile
- 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
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 accessfile
- the output filetimeout
- 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
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
Deprecated.Save the content of the specified url in the given file.- Parameters:
url
- the url to accessuserAgent
- the User-Agentfile
- the output filetimeout
- 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 org.apache.commons.httpclient.HttpException, 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 accesstimeout
- 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-Agentdata
- the parameters to be posted to the remote url- 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.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 org.apache.commons.httpclient.HttpException, 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 accesstimeout
- 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-Agentdata
- the parameters to be posted to the remote urlcharset
- 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:
org.apache.commons.httpclient.HttpException
- if an error occurred.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 accessuserAgent
- 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, 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 accesstimeout
- 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 accessfile
- 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, int timeout) throws java.io.IOException
Save the content of the specified url in the given file.- Parameters:
url
- the url to accessfile
- the output filetimeout
- 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 accessuserAgent
- the User-Agentfile
- the output filetimeout
- 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
-
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 accesstimeout
- 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-Agentparameters
- 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 accesstimeout
- 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-Agentparameters
- the parameters to be posted to the remote urlcharset
- 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
-
-