Class URLLoader
Depending on the platform, this class will automatically choose between using
HttpClient
and HttpURLConnection
. The
former was introduced in Java 11 and supports HTTP/2. However, it is not yet
fully supported across all platforms and environments. On such platforms, the
legacy HTTP client is used instead.
The auto-detect mechanism can be overruled using the
FORCE_LEGACY_HTTP_CLIENT_SYSTEM_PROPERTY
system property. This is a
global setting that affects all URLLoader
instances. It is not
possible to override the auto-detect mechanism on a per-request basis, since
this would be akin to just using the preferred HTTP client directly.
This class only supports URL requests for the HTTP and HTTPS protocols. Other
protocols, such as file://
URLs, are not supported.
Though the purpose of this class is to provide a consistent API on top of the two HTTP clients, the name is based on the Flash ActionScript class of the same name.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
Deprecated.Applications should prefer automatic detection, to avoid ending up with a HTTP client that is not actually supported on the current platform. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionAllows response error codes (HTTP status 4xx and 5xx).static URLLoader
Factory method that creates a DELETE request to the specified URL.Disables SSL certificate validation performed for HTTPS connections, ignoring any errors or warnings that may occur while checking the certificate.static URLLoader
Factory method that creates a GET request to the specified URL.static URLLoader
Factory method that creates a POST request to the specified URL.static URLLoader
Factory method that creates a PUT request to the specified URL.send()
Sends the HTTP request and returns the response.Asynchronous version ofsend()
that sends the request from a separate thread.Starts a background thread that will send this request.toString()
withBasicAuth
(String user, String password) Convenience method to add the HTTP basic authentication header to the request.Uses the specified request body and sets the Content-Type accordingly.Uses the specified form data as the request body, and changes the Content-Type header to "application/x-www-form-urlencoded".withHeader
(String name, String value) Adds the specified header to the end of the request.withHeader
(String name, String value, boolean replace) Adds the specified request header.withHeaders
(Map<String, String> headers) Adds the specified request headers.withHeaders
(Headers additional) Adds the specified request headers.Uses the specified JSON request body, and changes the Content-Type header to "application/json".withPathComponent
(String pathComponent) Appends the specified path component to the end of the URL.withQueryParam
(String name, String value) withTimeout
(int timeout) Uses the specified XML request body, and changes the Content-Type header to "text/xml".
-
Field Details
-
FORCE_LEGACY_HTTP_CLIENT_SYSTEM_PROPERTY
Deprecated.Applications should prefer automatic detection, to avoid ending up with a HTTP client that is not actually supported on the current platform.System property to override the automatication HTTP client detection, and forces the usage of the classicHttpURLConnection
when set to true.- See Also:
-
-
Constructor Details
-
URLLoader
Starts building a URL request. In addition to this constructor, this class also provides a number of factory methods.- Throws:
IllegalArgumentException
- if the URL cannot be parsed into a valid HTTP or HTTPS URL.
-
URLLoader
Starts building a URL request. In addition to this constructor, this class also provides a number of factory methods.- Throws:
IllegalArgumentException
- if the URL cannot be parsed into a valid HTTP or HTTPS URL.
-
-
Method Details
-
withPathComponent
Appends the specified path component to the end of the URL. The path component is allowed to include a leading or trailing slash. Any other characters, including slashes, will be encoded.- Throws:
IllegalArgumentException
- if the combination of the existing URL plus the new path component leads to an invalid URL.
-
withHeader
Adds the specified request header. If thereplace
option is true, this will replace any headers with the same name. If the option is false, it will simply add the new header, leaving any existing headers with the same name in place. -
withHeader
Adds the specified header to the end of the request. Using this method is the equivalent of usingwithHeader(name, value, false
. -
withHeaders
Adds the specified request headers. This method is the equivalent of usingwithHeader(name, value, false
on all headers in the map. -
withHeaders
Adds the specified request headers. This method is the equivalent of usingwithHeader(name, value, false
on all headers. -
withBody
Uses the specified request body and sets the Content-Type accordingly. See thewithBody(PostData)
,withJSON(String)
, andwithXML(String)
convenience methods for common request body types. -
withBody
Uses the specified form data as the request body, and changes the Content-Type header to "application/x-www-form-urlencoded". -
withXML
Uses the specified XML request body, and changes the Content-Type header to "text/xml". -
withJSON
Uses the specified JSON request body, and changes the Content-Type header to "application/json". -
withQueryParam
-
withBasicAuth
Convenience method to add the HTTP basic authentication header to the request.Security note: Only use this method when sending requests to HTTP URLs. Do not use it for requests over plain HTTP, as the header can potentially be intercepted by anyone with network access.
-
withTimeout
-
allowErrorStatus
Allows response error codes (HTTP status 4xx and 5xx). This allows theURLLoader
to process the response for such a request, instead of throwing an exception. -
disableCertificateValidation
Disables SSL certificate validation performed for HTTPS connections, ignoring any errors or warnings that may occur while checking the certificate.Security note: Ignoring these errors and warnings means you can no longer assume communication to be secure. It is strongly recommended to correct the SSL certificate itself, rather than using HTTPS with an insecure certificate.
-
send
Sends the HTTP request and returns the response. This method will automatically select a suitable HTTP client for the current platform, as described in the class documentation.- Throws:
IOException
- if the request failes, of if the request produces HTTP status 4xx (client error) or 5xx (server error). However, ifallowErrorStatus()
was used, no such exception will be thrown.
-
sendAsync
Asynchronous version ofsend()
that sends the request from a separate thread. -
sendBackground
Starts a background thread that will send this request. Returns aSubscribable
that can be used to subscribe to the response and/or errors. -
toString
-
get
Factory method that creates a GET request to the specified URL. The request will use the UTF-8 character encoding. -
post
Factory method that creates a POST request to the specified URL. The request will use the UTF-8 character encoding. -
put
Factory method that creates a PUT request to the specified URL. The request will use the UTF-8 character encoding. -
delete
Factory method that creates a DELETE request to the specified URL. The request will use the UTF-8 character encoding.
-