Class HttpClientSupport
- Since:
- 1.35
- Version:
- 1.2
- Author:
- matt
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringA HTTP Accept header value for a JSON type.static final StringA HTTP Accept header value for any text type.static final intThe default value for theconnectionTimeoutproperty.static final StringThe HTTP method GET.static final StringThe HTTP method POST.protected final org.slf4j.LoggerA class-level logger. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidappendXWWWFormURLEncodedValue(StringBuilder buf, String key, Object value) Append a URL-escaped key/value pair to a string buffer.intGet the connection timeout.protected InputStreamGet an InputStream from a URLConnection response, handling compression.Get the SSL service.protected ReaderGet a Reader for a Unicode encoded URL connection response.protected URLConnectiongetURLConnection(String url, String httpMethod) Get a URLConnection for a specific URL and HTTP method.protected URLConnectiongetURLConnection(String url, String httpMethod, String accept) Get a URLConnection for a specific URL and HTTP method.protected URLConnectionpostXWWWFormURLEncodedData(String url, String accept, Map<String, ?> data) HTTP POST data asapplication/x-www-form-urlencoded(e.g.protected StringpostXWWWFormURLEncodedDataForString(String url, Map<String, ?> data) HTTP POST data asapplication/x-www-form-urlencoded(e.g.voidsetConnectionTimeout(int connectionTimeout) Set the connection timeout.voidsetSslService(OptionalService<SSLService> sslService) Set the SSL service.protected StringxWWWFormURLEncoded(Map<String, ?> data) Encode a map of data into a string suitable for posting to a web server as the content typeapplication/x-www-form-urlencoded.
-
Field Details
-
ACCEPT_TEXT
A HTTP Accept header value for any text type.- See Also:
-
ACCEPT_JSON
A HTTP Accept header value for a JSON type.- See Also:
-
DEFAULT_CONNECTION_TIMEOUT
public static final int DEFAULT_CONNECTION_TIMEOUTThe default value for theconnectionTimeoutproperty.- See Also:
-
HTTP_METHOD_GET
The HTTP method GET.- See Also:
-
HTTP_METHOD_POST
The HTTP method POST.- See Also:
-
log
protected final org.slf4j.Logger logA class-level logger.
-
-
Constructor Details
-
HttpClientSupport
public HttpClientSupport()
-
-
Method Details
-
getInputStreamFromURLConnection
Get an InputStream from a URLConnection response, handling compression.This method handles decompressing the response if the encoding is set to
gzipordeflate.- Parameters:
conn- the URLConnection- Returns:
- the InputStream
- Throws:
IOException- if any IO error occurs
-
getUnicodeReaderFromURLConnection
Get a Reader for a Unicode encoded URL connection response.This calls
getInputStreamFromURLConnection(URLConnection)so compressed responses are handled appropriately.- Parameters:
conn- the URLConnection- Returns:
- the Reader
- Throws:
IOException- if an IO error occurs
-
getURLConnection
Get a URLConnection for a specific URL and HTTP method.This defaults to the
ACCEPT_TEXTaccept value.- Parameters:
url- the URL to connect tohttpMethod- the HTTP method- Returns:
- the URLConnection
- Throws:
IOException- if any IO error occurs- See Also:
-
getURLConnection
protected URLConnection getURLConnection(String url, String httpMethod, String accept) throws IOException Get a URLConnection for a specific URL and HTTP method.If the httpMethod equals
POSTthen the connection'sdoOutputproperty will be set to true, otherwise it will be set to false. ThedoInputproperty is always set to true.This method also sets up the request property
Accept-Encoding: gzip,deflateso the response can be compressed. ThegetInputStreamFromURLConnection(URLConnection)automatically handles compressed responses.If the
getSslService()property is configured and the URL represents an HTTPS connection, then that factory will be used to for the connection.- Parameters:
url- the URL to connect tohttpMethod- the HTTP methodaccept- the HTTP Accept header value- Returns:
- the URLConnection
- Throws:
IOException- if any IO error occurs
-
appendXWWWFormURLEncodedValue
Append a URL-escaped key/value pair to a string buffer.- Parameters:
buf- the bufferkey- the keyvalue- the value
-
xWWWFormURLEncoded
Encode a map of data into a string suitable for posting to a web server as the content typeapplication/x-www-form-urlencoded. Arrays and Collections of values are supported as well.- Parameters:
data- the map of data to encode- Returns:
- the encoded data, or an empty string if nothing to encode
-
postXWWWFormURLEncodedData
protected URLConnection postXWWWFormURLEncodedData(String url, String accept, Map<String, ?> data) throws IOExceptionHTTP POST data asapplication/x-www-form-urlencoded(e.g. a web form) to a URL.- Parameters:
url- the URL to post toaccept- the value to use for the Accept HTTP headerdata- the data to encode and send as the body of the HTTP POST- Returns:
- the URLConnection after the post data has been sent
- Throws:
IOException- if any IO error occursRuntimeException- if the HTTP response code is not within the 200 - 299 range
-
postXWWWFormURLEncodedDataForString
protected String postXWWWFormURLEncodedDataForString(String url, Map<String, ?> data) throws IOExceptionHTTP POST data asapplication/x-www-form-urlencoded(e.g. a web form) to a URL and return the response body as a string.This method accepts text and JSON responses.
- Parameters:
url- the URL to post todata- the data to encode and send as the body of the HTTP POST- Returns:
- the response body as a String
- Throws:
IOException- if any IO error occursRuntimeException- if the HTTP response code is not within the 200 - 299 range- See Also:
-
setConnectionTimeout
public void setConnectionTimeout(int connectionTimeout) Set the connection timeout.- Parameters:
connectionTimeout- the timeout to set, in milliseconds
-
getConnectionTimeout
public int getConnectionTimeout()Get the connection timeout.- Returns:
- the timeout, in milliseconds
-
getSslService
Get the SSL service.- Returns:
- the service
-
setSslService
Set the SSL service.- Parameters:
sslService- the service to set
-