public final class HttpRequests
extends java.lang.Object
Handy class for reading data from URL connections with built-in support for HTTP redirects and gzipped content and automatic cleanup.
Reading the whole response into a string:
HttpRequests.request("https://example.com").readString(progressIndicator)
Downloading a file:
HttpRequests.request("https://example.com/file.zip").saveToFile(new File(downloadDir, "temp.zip"), progressIndicator)
Tuning a connection:
HttpRequests.request(url).userAgent("IntelliJ").readString()
HttpRequests.request(url).tuner(connection -> connection.setRequestProperty("X-Custom", value)).readString()
Using the input stream to implement custom reading logic:
int firstByte = HttpRequests.request("file:///dev/random").connect(request -> request.getInputStream().read())
String firstLine = HttpRequests.request("https://example.com").connect(request -> new BufferedReader(request.getReader()).readLine())
Modifier and Type | Class and Description |
---|---|
static interface |
HttpRequests.ConnectionTuner |
static class |
HttpRequests.HttpStatusException |
static interface |
HttpRequests.Request |
static interface |
HttpRequests.RequestProcessor<T> |
Modifier and Type | Field and Description |
---|---|
static int |
CONNECTION_TIMEOUT |
static java.lang.String |
JSON_CONTENT_TYPE |
static int |
READ_TIMEOUT |
static int |
REDIRECT_LIMIT |
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
createErrorMessage(java.io.IOException e,
HttpRequests.Request request,
boolean includeHeaders) |
static RequestBuilder |
delete(java.lang.String url) |
static RequestBuilder |
delete(java.lang.String url,
java.lang.String contentType) |
static RequestBuilder |
head(java.lang.String url) |
static RequestBuilder |
patch(java.lang.String url,
java.lang.String contentType)
Java does not support "newer" HTTP methods, so we have to rely on server-side support of `X-HTTP-Method-Override` header to invoke PATCH
For reasoning see
HttpURLConnection.setRequestMethod(String) |
static RequestBuilder |
post(java.lang.String url,
java.lang.String contentType) |
static RequestBuilder |
put(java.lang.String url,
java.lang.String contentType) |
static RequestBuilder |
request(java.lang.String url) |
static RequestBuilder |
request(Url url) |
public static final java.lang.String JSON_CONTENT_TYPE
public static final int CONNECTION_TIMEOUT
public static final int READ_TIMEOUT
public static final int REDIRECT_LIMIT
public static RequestBuilder request(Url url)
public static RequestBuilder request(java.lang.String url)
public static RequestBuilder head(java.lang.String url)
public static RequestBuilder delete(java.lang.String url)
public static RequestBuilder delete(java.lang.String url, java.lang.String contentType)
public static RequestBuilder post(java.lang.String url, java.lang.String contentType)
public static RequestBuilder put(java.lang.String url, java.lang.String contentType)
public static RequestBuilder patch(java.lang.String url, java.lang.String contentType)
HttpURLConnection.setRequestMethod(String)
TODO: either fiddle with reflection or patch JDK to avoid server reliance
public static java.lang.String createErrorMessage(java.io.IOException e, HttpRequests.Request request, boolean includeHeaders)