Package org.metricshub.http
Class HttpClient
java.lang.Object
org.metricshub.http.HttpClient
Simple HTTP Client implementation for Java's HttpURLConnection.
It has no external dependencies and facilitates the execution of HTTP requests.
It has no external dependencies and facilitates the execution of HTTP requests.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
The default User-Agent to use if theuserAgent
value is not provided to the HttpClient. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic HttpResponse
sendRequest
(String url, String method, String[] specifiedSslProtocolArray, String username, char[] password, String proxyServer, int proxyPort, String proxyUsername, char[] proxyPassword, String userAgent, Map<String, String> addHeaderMap, String body, int timeout, String downloadToPath)
-
Field Details
-
DEFAULT_USER_AGENT
The default User-Agent to use if theuserAgent
value is not provided to the HttpClient.- See Also:
-
-
Constructor Details
-
HttpClient
public HttpClient()
-
-
Method Details
-
sendRequest
public static HttpResponse sendRequest(String url, String method, String[] specifiedSslProtocolArray, String username, char[] password, String proxyServer, int proxyPort, String proxyUsername, char[] proxyPassword, String userAgent, Map<String, String> addHeaderMap, String body, int timeout, String downloadToPath) throws IOException- Parameters:
url
- The URL to be requested (e.g. https://w3.test.org/site/list.jsp)method
- GET|POST|PUT|DELETE or whatever HTTP verb is supportedspecifiedSslProtocolArray
- Array of string of the SSL protocols to use (e.g.: "SSLv3", "TLSv1", etc.)username
- Username to access the specified URLpassword
- Password associated to usernameproxyServer
- Host name of IP address of the proxy. Leave empty or null if no proxy is required.proxyPort
- Port of the proxy (e.g. 3128)proxyUsername
- Username to connect to the proxy (if any)proxyPassword
- Password associated to the proxy usernameuserAgent
- String of the user agent to specify in the request (if null, will use a default one)addHeaderMap
- Additional headers to be added to the HTTP request (pairs of key and value)body
- Body of the HTTP request to be senttimeout
- Timeout in seconds before the operation is canceleddownloadToPath
- A path where to download the content of the HTTP response to- Returns:
- an HttpResponse, which itself contains the HTTP status code, the headers and the body of the response
- Throws:
MalformedURLException
- when the specified URL is invalidIOException
- when anything wrong happens during the connection and while downloading information from the Web serverFileNotFoundException
- when the specified downloadToPath is not correct (not a file, not accessible, etc.)
-