Class HttpClientUtils
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic intDefault timeout value (in milliseconds).
Used for socket timeout, connect timeout, and connection request timeout.static intstatic String -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringexecuteMethodAndGetAsStringOrAsFile(org.apache.http.impl.client.CloseableHttpClient client, org.apache.http.client.methods.HttpRequestBase request, File file) static StringexecuteMethodAndGetContentAsStringOrAsFile(org.apache.commons.httpclient.HttpClient client, org.apache.commons.httpclient.HttpMethodBase method, File file) static StringRetrieves the content of the specified url as a string.static StringRetrieves the content of the specified url.static voidSave the content of the specified url in the given file.static voidSave the content of the specified url in the given file.static voidSave the content of the specified url in the given file.static voidSave the content of the specified url in the given file.static voidSave the content of the specified url in the given file.static StringRetrieves the content of the specified url.static StringRetrieves the content of the specified url.static StringgetContent(String url) Deprecated.static StringgetContent(String url, int timeout, String userAgent) Deprecated.static voidgetContent(String url, File file) Deprecated.static voidgetContent(String url, File file, int timeout) Deprecated.static voidgetContent(String url, File file, int timeout, String userAgent) Deprecated.static StringgetContent(String url, String userAgent) Deprecated.static org.apache.commons.httpclient.HttpClientgetNewHttpClient(String url, int timeout) Deprecated.this method use legacy HttpClient 3.x, usenewHttpClient()static org.apache.http.impl.client.CloseableHttpClientRetrieve a new HttpClient instance preconfigured with JCMS settings.static org.apache.http.impl.client.CloseableHttpClientnewHttpClient(int timeout) Retrieve a new HttpClient instance preconfigured with JCMS settings.static Stringpost(String url, int timeout, String userAgent, List<? extends org.apache.http.NameValuePair> parameters) Post the specified data to the specified url and retrieve the content as a string.static Stringpost(String url, int timeout, String userAgent, List<? extends org.apache.http.NameValuePair> parameters, String charset) Post the specified data to the specified url and retrieve the content as a string.static Stringpost(String url, int timeout, String userAgent, List<? extends org.apache.http.NameValuePair> parameters, String charset, String accept) Post the specified data to the specified url and retrieve the content as a string.static Stringpost(String url, int timeout, String userAgent, List<? extends org.apache.http.NameValuePair> parameters, Map<String, String> headers, String charset) Post the specified data to the specified url and retrieve the content as a string.static Stringpost(String url, int timeout, String userAgent, List<? extends org.apache.http.NameValuePair> parameters, Map<String, String> headers, String charset, String accept) Post the specified data to the specified url and retrieve the content as a string.static StringpostAndGetContent(String url, int timeout, String userAgent, org.apache.commons.httpclient.NameValuePair[] data) Post the specified data to the specified url and retrieve the content as a string.static StringpostAndGetContent(String url, int timeout, String userAgent, org.apache.commons.httpclient.NameValuePair[] data, String charset) Post the specified data to the specified url and retrieve the content as a string.static StringpostAndGetContentAsStringOrAsFile(String url, File file, int timeout, String userAgent, org.apache.commons.httpclient.NameValuePair[] data, String charset) static voidsetProxyParamsForUrl(org.apache.commons.httpclient.HttpClient client, String remoteUrl) Deprecated.this method use legacy HttpClient 3.x, usenewHttpClient()static voidsetProxyParamsForUrl(org.apache.commons.httpclient.HttpClient client, URL remoteURL) Deprecated.this method use legacy HttpClient 3.x, usenewHttpClient()
-
Field Details
-
HTTP_TIMEOUT
public static int HTTP_TIMEOUT -
DEFAULT_TIMEOUT
public static int DEFAULT_TIMEOUTDefault timeout value (in milliseconds).
Used for socket timeout, connect timeout, and connection request timeout.- Since:
- 10.0.1 / JCMS-6434
-
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, 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:
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, 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:
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, 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
@Deprecated public static String getContent(String url) throws org.apache.commons.httpclient.HttpException, 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.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 accessuserAgent- 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 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.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 accessfile- 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 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.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 Deprecated.Save the content of the specified url in the given file.- Parameters:
url- the url to accessfile- the output filetimeout- the connection timeoutuserAgent- 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 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:
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 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:
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.HttpExceptionIOException
-
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.HttpExceptionIOException
-
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_TIMEOUTis 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
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
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:
IOException- if request failed- Since:
- jcms-10.0.1 / JCMS-6434
-
get
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-Agentaccept- 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
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:
IOException- if request failed- Since:
- jcms-10.0.1 / JCMS-6434
-
get
Save the content of the specified url in the given file.- Parameters:
url- the url to accessfile- the output file- Throws:
IOException- if request failed or if output file could not be written- Since:
- jcms-10.0.1 / JCMS-6434
-
get
Save the content of the specified url in the given file.- Parameters:
url- the url to accessfile- the output fileaccept- 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
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:
IOException- if request failed or if output file could not be written- Since:
- jcms-10.0.1 / JCMS-6434
-
get
Save the content of the specified url in the given file.- Parameters:
url- the url to accessfile- the output filetimeout- the connection timeoutuserAgent- 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 accessfile- the output filetimeout- the connection timeoutuserAgent- the User-Agentaccept- 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 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:
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 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:
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 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-1accept- 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 IOExceptionPost 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 urlheaders- a map of optional headers of the requestcharset- 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 IOExceptionPost 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 urlheaders- a map of optional headers of the requestcharset- the charset encoding to use. If null, uses the default one in HTTP : ISO-8859-1accept- 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
-
get(String)