Class UrlUtils
- Since:
- 1.54
- Version:
- 2.0
- Author:
- matt
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringA HTTP Accept header value for any type.static final StringA HTTP Accept header value for a JSON type.static final StringA HTTP Accept header value for any text type.static final StringA HTTP Accept header value for any text and JSON type.static final StringA HTTP Accept header value for any text and XML type.static final StringA HTTP Accept header value for any text and XML type.static final StringA HTTP Accept header value for an XML type.static final StringThe HTTP method GET.static final StringThe HTTP method PATCH.static final StringThe HTTP method POST.static final StringThe HTTP method PUT.static final StringThe UTF-8 character set name. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends Appendable & CharSequence>
voidappendURLEncodedValue(T buf, String key, Object value) Append a URL-escaped key/value pair to a character buffer.static InputStreamGet an InputStream from a URLConnection response, handling compression.static ReaderGet a Reader for a Unicode encoded URL connection response.static URLConnectiongetURL(String url, String accept, Map<String, ?> queryParameters, int timeout, SSLService sslService) HTTP GET a URL.static URLConnectiongetURLConnection(String url, String httpMethod, int timeout, SSLService sslService) Get a URLConnection for a specific URL and HTTP method.static URLConnectiongetURLConnection(String url, String httpMethod, String accept, int timeout, SSLService sslService) Get aURLConnectionfor a URL and HTTP method.static StringgetURLForString(String url, String accept, Map<String, ?> queryParameters, int timeout, SSLService sslService) HTTP GET a URL and return the response as a string.static URLConnectionpostXWWWFormURLEncodedData(String url, String accept, Map<String, ?> data, int timeout, SSLService sslService) HTTP POST data asapplication/x-www-form-urlencoded(e.g.static StringpostXWWWFormURLEncodedDataForString(String url, String accept, Map<String, ?> data, int timeout, SSLService sslService) HTTP POST data asapplication/x-www-form-urlencoded(e.g.static StringurlEncoded(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
-
UTF8_CHARSET
The UTF-8 character set name.- See Also:
-
ACCEPT_ANYTHING
A HTTP Accept header value for any type.- See Also:
-
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:
-
ACCEPT_TEXT_AND_JSON
A HTTP Accept header value for any text and JSON type.- See Also:
-
ACCEPT_XML
A HTTP Accept header value for an XML type.- See Also:
-
ACCEPT_TEXT_AND_XML
A HTTP Accept header value for any text and XML type.- See Also:
-
ACCEPT_TEXT_AND_JSON_AND_XML
A HTTP Accept header value for any text and XML type.- See Also:
-
HTTP_METHOD_GET
The HTTP method GET.- See Also:
-
HTTP_METHOD_PATCH
The HTTP method PATCH.- See Also:
-
HTTP_METHOD_POST
The HTTP method POST.- See Also:
-
HTTP_METHOD_PUT
The HTTP method PUT.- See Also:
-
-
Constructor Details
-
UrlUtils
public UrlUtils()
-
-
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
public static URLConnection getURLConnection(String url, String httpMethod, int timeout, SSLService sslService) throws IOException 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 methodtimeout- if greater than 0 then set as both the connection timeout and read timeout, in millisecondssslService- if provided and the URL represents an HTTPS URL, thenSSLService.getSSLSocketFactory()will be used as the connection'sSSLSocketFactory- Returns:
- the URLConnection
- Throws:
IOException- if any IO error occurs- See Also:
-
getURLConnection
public static URLConnection getURLConnection(String url, String httpMethod, String accept, int timeout, SSLService sslService) throws IOException Get aURLConnectionfor a URL and HTTP method.This method is geared towards HTTP (and HTTPS) connections, but can be used for arbitrary URLs by passing null for the HTTP-specific arguments.
If
httpMethodequals PATCH, POST, or PUT then 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.- Parameters:
url- the URL to connect tohttpMethod- if provided and the URL is an HTTP URL, the HTTP request method to setaccept- if provided, an HTTP Accept header value to settimeout- if greater than 0 then set as both the connection timeout and read timeout, in millisecondssslService- if provided and the URL represents an HTTPS URL, thenSSLService.getSSLSocketFactory()will be used as the connection'sSSLSocketFactory- Returns:
- the URLConnection
- Throws:
IOException- if any IO error occurs
-
appendURLEncodedValue
public static <T extends Appendable & CharSequence> void appendURLEncodedValue(T buf, String key, Object value) Append a URL-escaped key/value pair to a character buffer.- Type Parameters:
T- the buffer type- Parameters:
buf- the buffer to append tokey- the key to appendvalue- the value
-
urlEncoded
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
public static URLConnection postXWWWFormURLEncodedData(String url, String accept, Map<String, ?> data, int timeout, SSLService sslService) 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 POSTtimeout- if greater than 0 then set as both the connection timeout and read timeout, in millisecondssslService- if provided and the URL represents an HTTPS URL, thenSSLService.getSSLSocketFactory()will be used as the connection'sSSLSocketFactory- Returns:
- the URLConnection after the post data has been sent
- Throws:
IOException- if any IO error occursResultStatusException- if the URL is the HTTP scheme and the HTTP response code is not within the 200 - 299 range
-
postXWWWFormURLEncodedDataForString
public static String postXWWWFormURLEncodedDataForString(String url, String accept, Map<String, ?> data, int timeout, SSLService sslService) 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.- 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 POSTtimeout- if greater than 0 then set as both the connection timeout and read timeout, in millisecondssslService- if provided and the URL represents an HTTPS URL, thenSSLService.getSSLSocketFactory()will be used as the connection'sSSLSocketFactory- Returns:
- the response body as a String
- Throws:
IOException- if any IO error occursResultStatusException- if the URL is the HTTP scheme and the HTTP response code is not within the 200 - 299 range- See Also:
-
getURL
public static URLConnection getURL(String url, String accept, Map<String, ?> queryParameters, int timeout, SSLService sslService) throws IOExceptionHTTP GET a URL.- Parameters:
url- the URL to post toaccept- the value to use for the Accept HTTP headerqueryParameters- the data to encode and send as URL query parameterstimeout- if greater than 0 then set as both the connection timeout and read timeout, in millisecondssslService- if provided and the URL represents an HTTPS URL, thenSSLService.getSSLSocketFactory()will be used as the connection'sSSLSocketFactory- Returns:
- the URLConnection after the post data has been sent
- Throws:
IOException- if any IO error occursResultStatusException- if the URL is the HTTP scheme and the HTTP response code is not within the 200 - 299 range
-
getURLForString
public static String getURLForString(String url, String accept, Map<String, ?> queryParameters, int timeout, SSLService sslService) throws IOExceptionHTTP GET a URL and return the response as a string.- Parameters:
url- the URL to post toaccept- the value to use for the Accept HTTP headerqueryParameters- the data to encode and send as URL query parameterstimeout- if greater than 0 then set as both the connection timeout and read timeout, in millisecondssslService- if provided and the URL represents an HTTPS URL, thenSSLService.getSSLSocketFactory()will be used as the connection'sSSLSocketFactory- Returns:
- the URLConnection after the post data has been sent
- Throws:
IOException- if any IO error occursResultStatusException- if the URL is the HTTP scheme and the HTTP response code is not within the 200 - 299 range
-