Class HttpRequest
HttpURLConnection
(or sub-class).
Each instance supports making a single request and cannot be reused for further requests.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Encodes and decodes to and from Base64 notation.protected static class
Class that ensures aCloseable
gets closed with proper exception handling.static interface
CreatesHTTP connections
forurls
.protected static class
Class that and ensures aFlushable
gets flushed with proper exception handling.static class
HTTP request exception whose cause is always anIOException
protected static class
Operation that handles executing a callback once complete and handling nested exceptionsstatic class
Request output streamstatic interface
Callback interface for reporting upload progress for a request. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final String
private int
static final String
'UTF-8' charset nameprivate HttpURLConnection
private static HttpRequest.ConnectionFactory
static final String
'application/x-www-form-urlencoded' content type header valuestatic final String
'application/json' content type header valueprivate static final String
private static final String
private static final String[]
static final String
'gzip' encoding header valueprivate boolean
static final String
'Accept' header namestatic final String
'Accept-Charset' header namestatic final String
'Accept-Encoding' header namestatic final String
'Authorization' header namestatic final String
'Cache-Control' header namestatic final String
'Content-Encoding' header namestatic final String
'Content-Length' header namestatic final String
'Content-Type' header namestatic final String
'Date' header namestatic final String
'ETag' header namestatic final String
'Expires' header namestatic final String
'If-None-Match' header namestatic final String
'Last-Modified' header namestatic final String
'Location' header namestatic final String
'Proxy-Authorization' header namestatic final String
'Referer' header namestatic final String
'Server' header namestatic final String
'User-Agent' header nameprivate String
private int
private boolean
static final String
'DELETE' request methodstatic final String
'GET' request methodstatic final String
'HEAD' request methodstatic final String
'OPTIONS' options methodstatic final String
'POST' request methodstatic final String
'PUT' request methodstatic final String
'TRACE' request methodprivate boolean
private HttpRequest.RequestOutputStream
static final String
'charset' header value parameterprivate HttpRequest.UploadProgress
private final String
private long
private long
private static SSLSocketFactory
private static HostnameVerifier
private boolean
private final URL
-
Constructor Summary
ConstructorsConstructorDescriptionHttpRequest
(CharSequence url, String method) Create HTTP connection wrapperHttpRequest
(URL url, String method) Create HTTP connection wrapper -
Method Summary
Modifier and TypeMethodDescriptionSet the 'Accept' header to given valueacceptCharset
(String acceptCharset) Set the 'Accept-Charset' header to given valueacceptEncoding
(String acceptEncoding) Set the 'Accept-Encoding' header to given valueSet the 'Accept-Encoding' header to 'gzip'Set the 'Accept' header to 'application/json'private static StringBuilder
addParam
(Object key, Object value, StringBuilder result) private static StringBuilder
addParamPrefix
(String baseUrl, StringBuilder result) private static StringBuilder
addPathSeparator
(String baseUrl, StringBuilder result) static String
append
(CharSequence url, Object... params) Append given name/value pairs as query parameters to the base URLstatic String
append
(CharSequence url, Map<?, ?> params) Append given map as query parameters to the base URLarrayToList
(Object array) Represents array of any type as list of objects so we can easily iterate over itauthorization
(String authorization) Set the 'Authorization' header to given valueboolean
Is the response code a 400 Bad Request?Set the 'Authorization' header to given values in Basic authentication formatbody()
Get response asString
in given character setbody
(AtomicReference<String> output) Get the response body as aString
and set it as the value of the given reference.body
(AtomicReference<String> output, String charset) Get the response body as aString
and set it as the value of the given reference.buffer()
Get response in a buffered streamGet buffered reader to response body using the character set returned fromcharset()
and the configured buffer sizebufferedReader
(String charset) Get buffered reader to response body using the given character set r and the configured buffer sizeint
Get the configured buffer sizebufferSize
(int size) Set the size used when buffering and copying between streamsbyte[]
bytes()
Get response as byte arrayprotected ByteArrayOutputStream
Create byte array output streamGet the 'Cache-Control' header from the responsecharset()
Get 'charset' parameter from 'Content-Type' response headerchunk
(int size) Set chunked streaming mode to the given sizeprotected HttpRequest
Close output streamprotected HttpRequest
int
code()
Get the status code of the responsecode
(AtomicInteger output) Set the value of the givenAtomicInteger
to the status code of the responseconnectTimeout
(int timeout) Set connect timeout on connection to given valueGet the 'Content-Encoding' header from the responseint
Get the 'Content-Length' header from the responsecontentLength
(int contentLength) Set the 'Content-Length' request header to the given valuecontentLength
(String contentLength) Set the 'Content-Length' request header to the given valueGet the 'Content-Type' header from the responsecontentType
(String contentType) Set the 'Content-Type' request header to the given valuecontentType
(String contentType, String charset) Set the 'Content-Type' request header to the given value and charsetprotected HttpRequest
copy
(InputStream input, OutputStream output) Copy from input stream to output streamprotected HttpRequest
Copy from reader to writerprivate HttpURLConnection
boolean
created()
Is the response code a 201 Created?private Proxy
long
date()
Get the 'Date' header from the responselong
dateHeader
(String name) Get a date header from the response falling back to returning -1 if the header is missing or parsing failslong
dateHeader
(String name, long defaultValue) Get a date header from the response falling back to returning the given default value if the header is missing or parsing failsstatic HttpRequest
delete
(CharSequence url) Start a 'DELETE' request to the given URLstatic HttpRequest
delete
(CharSequence baseUrl, boolean encode, Object... params) Start a 'DELETE' request to the given URL along with the query paramsstatic HttpRequest
delete
(CharSequence baseUrl, Map<?, ?> params, boolean encode) Start a 'DELETE' request to the given URL along with the query paramsstatic HttpRequest
Start a 'DELETE' request to the given URLDisconnect the connectionstatic String
encode
(CharSequence url) Encode the given URL as an ASCIIString
eTag()
Get the 'ETag' header from the responselong
expires()
Get the 'Expires' header from the responsefollowRedirects
(boolean followRedirects) Set whether or not the underlying connection should follow redirects in the response.Write the name/value pair as form data to the request bodyWrite the name/value pair as form data to the request bodyWrite the key and value in the entry as form data to the request bodyWrite the key and value in the entry as form data to the request bodyWrite the values in the map as form data to the request bodyWrite the values in the map as encoded form data to the request bodystatic HttpRequest
get
(CharSequence url) Start a 'GET' request to the given URLstatic HttpRequest
get
(CharSequence baseUrl, boolean encode, Object... params) Start a 'GET' request to the given URL along with the query paramsstatic HttpRequest
get
(CharSequence baseUrl, Map<?, ?> params, boolean encode) Start a 'GET' request to the given URL along with the query paramsstatic HttpRequest
Start a 'GET' request to the given URLGet underlying connectionprotected String
Get parameter value from header valueGet parameter values from header valueprivate static SSLSocketFactory
private static HostnameVerifier
private static String
getValidCharset
(String charset) static HttpRequest
head
(CharSequence url) Start a 'HEAD' request to the given URLstatic HttpRequest
head
(CharSequence baseUrl, boolean encode, Object... params) Start a 'GET' request to the given URL along with the query paramsstatic HttpRequest
head
(CharSequence baseUrl, Map<?, ?> params, boolean encode) Start a 'HEAD' request to the given URL along with the query paramsstatic HttpRequest
Start a 'HEAD' request to the given URLGet a response headerSet header name to given valueSet header name to given valueSet header to have given entry's key as the name and value as the valueheaders()
Get all the response headersString[]
Get all values of the given header from the responseSet all headers found in given map where the keys are the header names and the values are the header valuesifModifiedSince
(long ifModifiedSince) Set the 'If-Modified-Since' request header to the given valueifNoneMatch
(String ifNoneMatch) Set the 'If-None-Match' request header to the given valueboolean
Get whether or not exceptions thrown byCloseable.close()
are ignoredignoreCloseExceptions
(boolean ignore) Set whether or not to ignore exceptions that occur from callingCloseable.close()
private HttpRequest
incrementTotalSize
(long size) int
Get an integer header from the response falling back to returning -1 if the header is missing or parsing failsint
Get an integer header value from the response falling back to the given default value if the header is missing or if parsing failsboolean
Is the response body empty?static void
keepAlive
(boolean keepAlive) Set the 'http.keepAlive' property to the given value.long
Get the 'Last-Modified' header from the responselocation()
Get the 'Location' header from the responsestatic void
maxConnections
(int maxConnections) Set the 'http.maxConnections' property to the given value.message()
Get status message of the responsemethod()
Get the HTTP method of this requestboolean
Is the response code a 204 No Content?static void
nonProxyHosts
(String... hosts) Set the 'http.nonProxyHosts' property to the given host values.boolean
notFound()
Is the response code a 404 Not Found?boolean
Is the response code a 304 Not Modified?boolean
ok()
Is the response code a 200 OK?protected HttpRequest
Open output streamstatic HttpRequest
options
(CharSequence url) Start an 'OPTIONS' request to the given URLstatic HttpRequest
Start an 'OPTIONS' request to the given URLGet parameter with given name from header value in responseparameters
(String headerName) Get all parameters from header value in responseWrite part of a multipart request to the request bodypart
(String name, InputStream part) Write part of a multipart request to the request bodyWrite part of a multipart request to the request bodyWrite part of a multipart request to the request bodyWrite part of a multipart request to the request bodyWrite part of a multipart request to the request bodyWrite part of a multipart request to the request bodyWrite part of a multipart request to the request bodypart
(String name, String filename, String contentType, InputStream part) Write part of a multipart request to the request bodyWrite part of a multipart request to the request bodypartHeader
(String name, String value) Write a multipart header to the response bodystatic HttpRequest
post
(CharSequence url) Start a 'POST' request to the given URLstatic HttpRequest
post
(CharSequence baseUrl, boolean encode, Object... params) Start a 'POST' request to the given URL along with the query paramsstatic HttpRequest
post
(CharSequence baseUrl, Map<?, ?> params, boolean encode) Start a 'POST' request to the given URL along with the query paramsstatic HttpRequest
Start a 'POST' request to the given URLprogress
(HttpRequest.UploadProgress callback) Set the UploadProgress callback for this requestproxyAuthorization
(String proxyAuthorization) Set the 'Proxy-Authorization' header to given valueproxyBasic
(String name, String password) Set the 'Proxy-Authorization' header to given values in Basic authentication formatstatic void
Set the 'http.proxyHost' and 'https.proxyHost' properties to the given host value.static void
proxyPort
(int port) Set the 'http.proxyPort' and 'https.proxyPort' properties to the given port number.static HttpRequest
put
(CharSequence url) Start a 'PUT' request to the given URLstatic HttpRequest
put
(CharSequence baseUrl, boolean encode, Object... params) Start a 'PUT' request to the given URL along with the query paramsstatic HttpRequest
put
(CharSequence baseUrl, Map<?, ?> params, boolean encode) Start a 'PUT' request to the given URL along with the query paramsstatic HttpRequest
Start a 'PUT' request to the given URLreader()
Get reader to response body using the character set returned fromcharset()
Get reader to response body using given character set.readTimeout
(int timeout) Set read timeout on connection to given valueStream response body to filereceive
(OutputStream output) Stream response to given output streamreceive
(PrintStream output) Stream response to given print streamReceive response into the given writerreceive
(Appendable appendable) Receive response into the given appendableSet the 'Referer' header to given valuesend
(byte[] input) Write byte array to request bodyWrite contents of file to request bodysend
(InputStream input) Write stream to request bodyWrite reader to request bodysend
(CharSequence value) Write char sequence to request bodyserver()
Get the 'Server' header from the responseboolean
Is the response code a 500 Internal Server Error?static void
setConnectionFactory
(HttpRequest.ConnectionFactory connectionFactory) Specify theHttpRequest.ConnectionFactory
used to create new requests.private static String
setProperty
(String name, String value) Set property to given value.protected HttpRequest
Start part of a multipartstream()
Get stream to response bodytoString()
static HttpRequest
trace
(CharSequence url) Start a 'TRACE' request to the given URLstatic HttpRequest
Start a 'TRACE' request to the given URLConfigure HTTPS connection to trust all certificatesConfigure HTTPS connection to trust all hosts using a customHostnameVerifier
that always returnstrue
for each host verifieduncompress
(boolean uncompress) Set whether or not the response body should be automatically uncompressed when read from.url()
Get theURL
of this request's connectionuseCaches
(boolean useCaches) Set value ofURLConnection.setUseCaches(boolean)
Configure an HTTP proxy on this connection.Set the 'User-Agent' header to given valueprotected HttpRequest
writePartHeader
(String name, String filename) Write part headerprotected HttpRequest
writePartHeader
(String name, String filename, String contentType) Write part headerwriter()
Create writer to request output stream
-
Field Details
-
CHARSET_UTF8
'UTF-8' charset name- See Also:
-
CONTENT_TYPE_FORM
'application/x-www-form-urlencoded' content type header value- See Also:
-
CONTENT_TYPE_JSON
'application/json' content type header value- See Also:
-
ENCODING_GZIP
'gzip' encoding header value- See Also:
-
HEADER_ACCEPT
'Accept' header name- See Also:
-
HEADER_ACCEPT_CHARSET
'Accept-Charset' header name- See Also:
-
HEADER_ACCEPT_ENCODING
'Accept-Encoding' header name- See Also:
-
HEADER_AUTHORIZATION
'Authorization' header name- See Also:
-
HEADER_CACHE_CONTROL
'Cache-Control' header name- See Also:
-
HEADER_CONTENT_ENCODING
'Content-Encoding' header name- See Also:
-
HEADER_CONTENT_LENGTH
'Content-Length' header name- See Also:
-
HEADER_CONTENT_TYPE
'Content-Type' header name- See Also:
-
HEADER_DATE
'Date' header name- See Also:
-
HEADER_ETAG
'ETag' header name- See Also:
-
HEADER_EXPIRES
'Expires' header name- See Also:
-
HEADER_IF_NONE_MATCH
'If-None-Match' header name- See Also:
-
HEADER_LAST_MODIFIED
'Last-Modified' header name- See Also:
-
HEADER_LOCATION
'Location' header name- See Also:
-
HEADER_PROXY_AUTHORIZATION
'Proxy-Authorization' header name- See Also:
-
HEADER_REFERER
'Referer' header name- See Also:
-
HEADER_SERVER
'Server' header name- See Also:
-
HEADER_USER_AGENT
'User-Agent' header name- See Also:
-
METHOD_DELETE
'DELETE' request method- See Also:
-
METHOD_GET
'GET' request method- See Also:
-
METHOD_HEAD
'HEAD' request method- See Also:
-
METHOD_OPTIONS
'OPTIONS' options method- See Also:
-
METHOD_POST
'POST' request method- See Also:
-
METHOD_PUT
'PUT' request method- See Also:
-
METHOD_TRACE
'TRACE' request method- See Also:
-
PARAM_CHARSET
'charset' header value parameter- See Also:
-
BOUNDARY
- See Also:
-
CONTENT_TYPE_MULTIPART
- See Also:
-
CRLF
- See Also:
-
EMPTY_STRINGS
-
TRUSTED_FACTORY
-
TRUSTED_VERIFIER
-
CONNECTION_FACTORY
-
connection
-
url
-
requestMethod
-
output
-
multipart
private boolean multipart -
form
private boolean form -
ignoreCloseExceptions
private boolean ignoreCloseExceptions -
uncompress
private boolean uncompress -
bufferSize
private int bufferSize -
totalSize
private long totalSize -
totalWritten
private long totalWritten -
httpProxyHost
-
httpProxyPort
private int httpProxyPort -
progress
-
-
Constructor Details
-
HttpRequest
Create HTTP connection wrapper- Parameters:
url
- Remote resource URL.method
- HTTP request method (e.g., "GET", "POST").- Throws:
HttpRequest.HttpRequestException
-
HttpRequest
Create HTTP connection wrapper- Parameters:
url
- Remote resource URL.method
- HTTP request method (e.g., "GET", "POST").- Throws:
HttpRequest.HttpRequestException
-
-
Method Details
-
getValidCharset
-
getTrustedFactory
- Throws:
HttpRequest.HttpRequestException
-
getTrustedVerifier
-
addPathSeparator
-
addParamPrefix
-
addParam
-
setConnectionFactory
Specify theHttpRequest.ConnectionFactory
used to create new requests. -
arrayToList
Represents array of any type as list of objects so we can easily iterate over it- Parameters:
array
- of elements- Returns:
- list with the same elements
-
encode
Encode the given URL as an ASCIIString
This method ensures the path and query segments of the URL are properly encoded such as ' ' characters being encoded to '%20' or any UTF-8 characters that are non-ASCII. No encoding of URLs is done by default by the
HttpRequest
constructors and so if URL encoding is needed this method should be called before calling theHttpRequest
constructor.- Parameters:
url
-- Returns:
- encoded URL
- Throws:
HttpRequest.HttpRequestException
-
append
Append given map as query parameters to the base URLEach map entry's key will be a parameter name and the value's
Object.toString()
will be the parameter value.- Parameters:
url
-params
-- Returns:
- URL with appended query params
-
append
Append given name/value pairs as query parameters to the base URLThe params argument is interpreted as a sequence of name/value pairs so the given number of params must be divisible by 2.
- Parameters:
url
-params
- name/value pairs- Returns:
- URL with appended query params
-
get
Start a 'GET' request to the given URL- Parameters:
url
-- Returns:
- request
- Throws:
HttpRequest.HttpRequestException
-
get
Start a 'GET' request to the given URL- Parameters:
url
-- Returns:
- request
- Throws:
HttpRequest.HttpRequestException
-
get
Start a 'GET' request to the given URL along with the query params- Parameters:
baseUrl
-params
- The query parameters to include as part of the baseUrlencode
- true to encode the full URL- Returns:
- request
- See Also:
-
get
Start a 'GET' request to the given URL along with the query params- Parameters:
baseUrl
-encode
- true to encode the full URLparams
- the name/value query parameter pairs to include as part of the baseUrl- Returns:
- request
- See Also:
-
post
Start a 'POST' request to the given URL- Parameters:
url
-- Returns:
- request
- Throws:
HttpRequest.HttpRequestException
-
post
Start a 'POST' request to the given URL- Parameters:
url
-- Returns:
- request
- Throws:
HttpRequest.HttpRequestException
-
post
Start a 'POST' request to the given URL along with the query params- Parameters:
baseUrl
-params
- the query parameters to include as part of the baseUrlencode
- true to encode the full URL- Returns:
- request
- See Also:
-
post
Start a 'POST' request to the given URL along with the query params- Parameters:
baseUrl
-encode
- true to encode the full URLparams
- the name/value query parameter pairs to include as part of the baseUrl- Returns:
- request
- See Also:
-
put
Start a 'PUT' request to the given URL- Parameters:
url
-- Returns:
- request
- Throws:
HttpRequest.HttpRequestException
-
put
Start a 'PUT' request to the given URL- Parameters:
url
-- Returns:
- request
- Throws:
HttpRequest.HttpRequestException
-
put
Start a 'PUT' request to the given URL along with the query params- Parameters:
baseUrl
-params
- the query parameters to include as part of the baseUrlencode
- true to encode the full URL- Returns:
- request
- See Also:
-
put
Start a 'PUT' request to the given URL along with the query params- Parameters:
baseUrl
-encode
- true to encode the full URLparams
- the name/value query parameter pairs to include as part of the baseUrl- Returns:
- request
- See Also:
-
delete
Start a 'DELETE' request to the given URL- Parameters:
url
-- Returns:
- request
- Throws:
HttpRequest.HttpRequestException
-
delete
Start a 'DELETE' request to the given URL- Parameters:
url
-- Returns:
- request
- Throws:
HttpRequest.HttpRequestException
-
delete
Start a 'DELETE' request to the given URL along with the query params- Parameters:
baseUrl
-params
- The query parameters to include as part of the baseUrlencode
- true to encode the full URL- Returns:
- request
- See Also:
-
delete
Start a 'DELETE' request to the given URL along with the query params- Parameters:
baseUrl
-encode
- true to encode the full URLparams
- the name/value query parameter pairs to include as part of the baseUrl- Returns:
- request
- See Also:
-
head
Start a 'HEAD' request to the given URL- Parameters:
url
-- Returns:
- request
- Throws:
HttpRequest.HttpRequestException
-
head
Start a 'HEAD' request to the given URL- Parameters:
url
-- Returns:
- request
- Throws:
HttpRequest.HttpRequestException
-
head
Start a 'HEAD' request to the given URL along with the query params- Parameters:
baseUrl
-params
- The query parameters to include as part of the baseUrlencode
- true to encode the full URL- Returns:
- request
- See Also:
-
head
Start a 'GET' request to the given URL along with the query params- Parameters:
baseUrl
-encode
- true to encode the full URLparams
- the name/value query parameter pairs to include as part of the baseUrl- Returns:
- request
- See Also:
-
options
Start an 'OPTIONS' request to the given URL- Parameters:
url
-- Returns:
- request
- Throws:
HttpRequest.HttpRequestException
-
options
Start an 'OPTIONS' request to the given URL- Parameters:
url
-- Returns:
- request
- Throws:
HttpRequest.HttpRequestException
-
trace
Start a 'TRACE' request to the given URL- Parameters:
url
-- Returns:
- request
- Throws:
HttpRequest.HttpRequestException
-
trace
Start a 'TRACE' request to the given URL- Parameters:
url
-- Returns:
- request
- Throws:
HttpRequest.HttpRequestException
-
keepAlive
public static void keepAlive(boolean keepAlive) Set the 'http.keepAlive' property to the given value.This setting will apply to all requests.
- Parameters:
keepAlive
-
-
maxConnections
public static void maxConnections(int maxConnections) Set the 'http.maxConnections' property to the given value.This setting will apply to all requests.
- Parameters:
maxConnections
-
-
proxyHost
Set the 'http.proxyHost' and 'https.proxyHost' properties to the given host value.This setting will apply to all requests.
- Parameters:
host
-
-
proxyPort
public static void proxyPort(int port) Set the 'http.proxyPort' and 'https.proxyPort' properties to the given port number.This setting will apply to all requests.
- Parameters:
port
-
-
nonProxyHosts
Set the 'http.nonProxyHosts' property to the given host values.Hosts will be separated by a '|' character.
This setting will apply to all requests.
- Parameters:
hosts
-
-
setProperty
Set property to given value.Specifying a null value will cause the property to be cleared
- Parameters:
name
-value
-- Returns:
- previous value
-
createProxy
-
createConnection
-
toString
-
getConnection
Get underlying connection- Returns:
- connection
-
ignoreCloseExceptions
Set whether or not to ignore exceptions that occur from callingCloseable.close()
The default value of this setting is
true
- Parameters:
ignore
-- Returns:
- this request
-
ignoreCloseExceptions
public boolean ignoreCloseExceptions()Get whether or not exceptions thrown byCloseable.close()
are ignored- Returns:
- true if ignoring, false if throwing
-
code
Get the status code of the response- Returns:
- the response code
- Throws:
HttpRequest.HttpRequestException
-
code
Set the value of the givenAtomicInteger
to the status code of the response- Parameters:
output
-- Returns:
- this request
- Throws:
HttpRequest.HttpRequestException
-
ok
Is the response code a 200 OK?- Returns:
- true if 200, false otherwise
- Throws:
HttpRequest.HttpRequestException
-
created
Is the response code a 201 Created?- Returns:
- true if 201, false otherwise
- Throws:
HttpRequest.HttpRequestException
-
noContent
Is the response code a 204 No Content?- Returns:
- true if 204, false otherwise
- Throws:
HttpRequest.HttpRequestException
-
serverError
Is the response code a 500 Internal Server Error?- Returns:
- true if 500, false otherwise
- Throws:
HttpRequest.HttpRequestException
-
badRequest
Is the response code a 400 Bad Request?- Returns:
- true if 400, false otherwise
- Throws:
HttpRequest.HttpRequestException
-
notFound
Is the response code a 404 Not Found?- Returns:
- true if 404, false otherwise
- Throws:
HttpRequest.HttpRequestException
-
notModified
Is the response code a 304 Not Modified?- Returns:
- true if 304, false otherwise
- Throws:
HttpRequest.HttpRequestException
-
message
Get status message of the response- Returns:
- message
- Throws:
HttpRequest.HttpRequestException
-
disconnect
Disconnect the connection- Returns:
- this request
-
chunk
Set chunked streaming mode to the given size- Parameters:
size
-- Returns:
- this request
-
bufferSize
Set the size used when buffering and copying between streamsThis size is also used for send and receive buffers created for both char and byte arrays
The default buffer size is 8,192 bytes
- Parameters:
size
-- Returns:
- this request
-
bufferSize
public int bufferSize()Get the configured buffer sizeThe default buffer size is 8,192 bytes
- Returns:
- buffer size
-
uncompress
Set whether or not the response body should be automatically uncompressed when read from.This will only affect requests that have the 'Content-Encoding' response header set to 'gzip'.
This causes all receive methods to use a
GZIPInputStream
when applicable so that higher level streams and readers can read the data uncompressed.Setting this option does not cause any request headers to be set automatically so
acceptGzipEncoding()
should be used in conjunction with this setting to tell the server to gzip the response.- Parameters:
uncompress
-- Returns:
- this request
-
byteStream
Create byte array output stream- Returns:
- stream
-
body
Get response asString
in given character setThis will fall back to using the UTF-8 character set if the given charset is null
- Parameters:
charset
-- Returns:
- string
- Throws:
HttpRequest.HttpRequestException
-
body
- Returns:
- string
- Throws:
HttpRequest.HttpRequestException
-
body
Get the response body as aString
and set it as the value of the given reference.- Parameters:
output
-- Returns:
- this request
- Throws:
HttpRequest.HttpRequestException
-
body
public HttpRequest body(AtomicReference<String> output, String charset) throws HttpRequest.HttpRequestException Get the response body as aString
and set it as the value of the given reference.- Parameters:
output
-charset
-- Returns:
- this request
- Throws:
HttpRequest.HttpRequestException
-
isBodyEmpty
Is the response body empty?- Returns:
- true if the Content-Length response header is 0, false otherwise
- Throws:
HttpRequest.HttpRequestException
-
bytes
Get response as byte array- Returns:
- byte array
- Throws:
HttpRequest.HttpRequestException
-
buffer
Get response in a buffered stream- Returns:
- stream
- Throws:
HttpRequest.HttpRequestException
- See Also:
-
stream
Get stream to response body- Returns:
- stream
- Throws:
HttpRequest.HttpRequestException
-
reader
Get reader to response body using given character set.This will fall back to using the UTF-8 character set if the given charset is null
- Parameters:
charset
-- Returns:
- reader
- Throws:
HttpRequest.HttpRequestException
-
reader
Get reader to response body using the character set returned fromcharset()
- Returns:
- reader
- Throws:
HttpRequest.HttpRequestException
-
bufferedReader
Get buffered reader to response body using the given character set r and the configured buffer size- Parameters:
charset
-- Returns:
- reader
- Throws:
HttpRequest.HttpRequestException
- See Also:
-
bufferedReader
Get buffered reader to response body using the character set returned fromcharset()
and the configured buffer size- Returns:
- reader
- Throws:
HttpRequest.HttpRequestException
- See Also:
-
receive
Stream response body to file- Parameters:
file
-- Returns:
- this request
- Throws:
HttpRequest.HttpRequestException
-
receive
Stream response to given output stream- Parameters:
output
-- Returns:
- this request
- Throws:
HttpRequest.HttpRequestException
-
receive
Stream response to given print stream- Parameters:
output
-- Returns:
- this request
- Throws:
HttpRequest.HttpRequestException
-
receive
Receive response into the given appendable- Parameters:
appendable
-- Returns:
- this request
- Throws:
HttpRequest.HttpRequestException
-
receive
Receive response into the given writer- Parameters:
writer
-- Returns:
- this request
- Throws:
HttpRequest.HttpRequestException
-
readTimeout
Set read timeout on connection to given value- Parameters:
timeout
-- Returns:
- this request
-
connectTimeout
Set connect timeout on connection to given value- Parameters:
timeout
-- Returns:
- this request
-
header
Set header name to given value- Parameters:
name
-value
-- Returns:
- this request
-
header
Set header name to given value- Parameters:
name
-value
-- Returns:
- this request
-
headers
Set all headers found in given map where the keys are the header names and the values are the header values- Parameters:
headers
-- Returns:
- this request
-
header
Set header to have given entry's key as the name and value as the value- Parameters:
header
-- Returns:
- this request
-
header
Get a response header- Parameters:
name
-- Returns:
- response header
- Throws:
HttpRequest.HttpRequestException
-
headers
Get all the response headers- Returns:
- map of response header names to their value(s)
- Throws:
HttpRequest.HttpRequestException
-
dateHeader
Get a date header from the response falling back to returning -1 if the header is missing or parsing fails- Parameters:
name
-- Returns:
- date, -1 on failures
- Throws:
HttpRequest.HttpRequestException
-
dateHeader
Get a date header from the response falling back to returning the given default value if the header is missing or parsing fails- Parameters:
name
-defaultValue
-- Returns:
- date, default value on failures
- Throws:
HttpRequest.HttpRequestException
-
intHeader
Get an integer header from the response falling back to returning -1 if the header is missing or parsing fails- Parameters:
name
-- Returns:
- header value as an integer, -1 when missing or parsing fails
- Throws:
HttpRequest.HttpRequestException
-
intHeader
Get an integer header value from the response falling back to the given default value if the header is missing or if parsing fails- Parameters:
name
-defaultValue
-- Returns:
- header value as an integer, default value when missing or parsing fails
- Throws:
HttpRequest.HttpRequestException
-
headers
Get all values of the given header from the response- Parameters:
name
-- Returns:
- non-null but possibly empty array of
String
header values
-
parameter
Get parameter with given name from header value in response- Parameters:
headerName
-paramName
-- Returns:
- parameter value or null if missing
-
parameters
Get all parameters from header value in responseThis will be all key=value pairs after the first ';' that are separated by a ';'
- Parameters:
headerName
-- Returns:
- non-null but possibly empty map of parameter headers
-
getParams
Get parameter values from header value- Parameters:
header
-- Returns:
- parameter value or null if none
-
getParam
Get parameter value from header value- Parameters:
value
-paramName
-- Returns:
- parameter value or null if none
-
charset
Get 'charset' parameter from 'Content-Type' response header- Returns:
- charset or null if none
-
userAgent
Set the 'User-Agent' header to given value- Parameters:
userAgent
-- Returns:
- this request
-
referer
Set the 'Referer' header to given value- Parameters:
referer
-- Returns:
- this request
-
useCaches
Set value ofURLConnection.setUseCaches(boolean)
- Parameters:
useCaches
-- Returns:
- this request
-
acceptEncoding
Set the 'Accept-Encoding' header to given value- Parameters:
acceptEncoding
-- Returns:
- this request
-
acceptGzipEncoding
Set the 'Accept-Encoding' header to 'gzip'- Returns:
- this request
- See Also:
-
acceptCharset
Set the 'Accept-Charset' header to given value- Parameters:
acceptCharset
-- Returns:
- this request
-
contentEncoding
Get the 'Content-Encoding' header from the response- Returns:
- this request
-
server
Get the 'Server' header from the response- Returns:
- server
-
date
public long date()Get the 'Date' header from the response- Returns:
- date value, -1 on failures
-
cacheControl
Get the 'Cache-Control' header from the response- Returns:
- cache control
-
eTag
Get the 'ETag' header from the response- Returns:
- entity tag
-
expires
public long expires()Get the 'Expires' header from the response- Returns:
- expires value, -1 on failures
-
lastModified
public long lastModified()Get the 'Last-Modified' header from the response- Returns:
- last modified value, -1 on failures
-
location
Get the 'Location' header from the response- Returns:
- location
-
authorization
Set the 'Authorization' header to given value- Parameters:
authorization
-- Returns:
- this request
-
proxyAuthorization
Set the 'Proxy-Authorization' header to given value- Parameters:
proxyAuthorization
-- Returns:
- this request
-
basic
Set the 'Authorization' header to given values in Basic authentication format- Parameters:
name
-password
-- Returns:
- this request
-
proxyBasic
Set the 'Proxy-Authorization' header to given values in Basic authentication format- Parameters:
name
-password
-- Returns:
- this request
-
ifModifiedSince
Set the 'If-Modified-Since' request header to the given value- Parameters:
ifModifiedSince
-- Returns:
- this request
-
ifNoneMatch
Set the 'If-None-Match' request header to the given value- Parameters:
ifNoneMatch
-- Returns:
- this request
-
contentType
Set the 'Content-Type' request header to the given value- Parameters:
contentType
-- Returns:
- this request
-
contentType
Set the 'Content-Type' request header to the given value and charset- Parameters:
contentType
-charset
-- Returns:
- this request
-
contentType
Get the 'Content-Type' header from the response- Returns:
- response header value
-
contentLength
public int contentLength()Get the 'Content-Length' header from the response- Returns:
- response header value
-
contentLength
Set the 'Content-Length' request header to the given value- Parameters:
contentLength
-- Returns:
- this request
-
contentLength
Set the 'Content-Length' request header to the given value- Parameters:
contentLength
-- Returns:
- this request
-
accept
Set the 'Accept' header to given value- Parameters:
accept
-- Returns:
- this request
-
acceptJson
Set the 'Accept' header to 'application/json'- Returns:
- this request
-
copy
Copy from input stream to output stream- Parameters:
input
-output
-- Returns:
- this request
- Throws:
IOException
-
copy
Copy from reader to writer- Parameters:
input
-output
-- Returns:
- this request
- Throws:
IOException
-
progress
Set the UploadProgress callback for this request- Parameters:
callback
-- Returns:
- this request
-
incrementTotalSize
-
closeOutput
Close output stream- Returns:
- this request
- Throws:
HttpRequest.HttpRequestException
IOException
-
closeOutputQuietly
- Returns:
- this request
- Throws:
HttpRequest.HttpRequestException
-
openOutput
Open output stream- Returns:
- this request
- Throws:
IOException
-
startPart
Start part of a multipart- Returns:
- this request
- Throws:
IOException
-
writePartHeader
Write part header- Parameters:
name
-filename
-- Returns:
- this request
- Throws:
IOException
-
writePartHeader
protected HttpRequest writePartHeader(String name, String filename, String contentType) throws IOException Write part header- Parameters:
name
-filename
-contentType
-- Returns:
- this request
- Throws:
IOException
-
part
Write part of a multipart request to the request body- Parameters:
name
-part
-- Returns:
- this request
-
part
public HttpRequest part(String name, String filename, String part) throws HttpRequest.HttpRequestException Write part of a multipart request to the request body- Parameters:
name
-filename
-part
-- Returns:
- this request
- Throws:
HttpRequest.HttpRequestException
-
part
public HttpRequest part(String name, String filename, String contentType, String part) throws HttpRequest.HttpRequestException Write part of a multipart request to the request body- Parameters:
name
-filename
-contentType
- value of the Content-Type part headerpart
-- Returns:
- this request
- Throws:
HttpRequest.HttpRequestException
-
part
Write part of a multipart request to the request body- Parameters:
name
-part
-- Returns:
- this request
- Throws:
HttpRequest.HttpRequestException
-
part
public HttpRequest part(String name, String filename, Number part) throws HttpRequest.HttpRequestException Write part of a multipart request to the request body- Parameters:
name
-filename
-part
-- Returns:
- this request
- Throws:
HttpRequest.HttpRequestException
-
part
Write part of a multipart request to the request body- Parameters:
name
-part
-- Returns:
- this request
- Throws:
HttpRequest.HttpRequestException
-
part
public HttpRequest part(String name, String filename, File part) throws HttpRequest.HttpRequestException Write part of a multipart request to the request body- Parameters:
name
-filename
-part
-- Returns:
- this request
- Throws:
HttpRequest.HttpRequestException
-
part
public HttpRequest part(String name, String filename, String contentType, File part) throws HttpRequest.HttpRequestException Write part of a multipart request to the request body- Parameters:
name
-filename
-contentType
- value of the Content-Type part headerpart
-- Returns:
- this request
- Throws:
HttpRequest.HttpRequestException
-
part
Write part of a multipart request to the request body- Parameters:
name
-part
-- Returns:
- this request
- Throws:
HttpRequest.HttpRequestException
-
part
public HttpRequest part(String name, String filename, String contentType, InputStream part) throws HttpRequest.HttpRequestException Write part of a multipart request to the request body- Parameters:
name
-filename
-contentType
- value of the Content-Type part headerpart
-- Returns:
- this request
- Throws:
HttpRequest.HttpRequestException
-
partHeader
Write a multipart header to the response body- Parameters:
name
-value
-- Returns:
- this request
- Throws:
HttpRequest.HttpRequestException
-
send
Write contents of file to request body- Parameters:
input
-- Returns:
- this request
- Throws:
HttpRequest.HttpRequestException
-
send
Write byte array to request body- Parameters:
input
-- Returns:
- this request
- Throws:
HttpRequest.HttpRequestException
-
send
Write stream to request bodyThe given stream will be closed once sending completes
- Parameters:
input
-- Returns:
- this request
- Throws:
HttpRequest.HttpRequestException
-
send
Write reader to request bodyThe given reader will be closed once sending completes
- Parameters:
input
-- Returns:
- this request
- Throws:
HttpRequest.HttpRequestException
-
send
Write char sequence to request bodyThe charset configured via
contentType(String)
will be used and UTF-8 will be used if it is unset.- Parameters:
value
-- Returns:
- this request
- Throws:
HttpRequest.HttpRequestException
-
writer
Create writer to request output stream- Returns:
- writer
- Throws:
HttpRequest.HttpRequestException
-
form
Write the values in the map as form data to the request bodyThe pairs specified will be URL-encoded in UTF-8 and sent with the 'application/x-www-form-urlencoded' content-type
- Parameters:
values
-- Returns:
- this request
- Throws:
HttpRequest.HttpRequestException
-
form
Write the key and value in the entry as form data to the request bodyThe pair specified will be URL-encoded in UTF-8 and sent with the 'application/x-www-form-urlencoded' content-type
- Parameters:
entry
-- Returns:
- this request
- Throws:
HttpRequest.HttpRequestException
-
form
public HttpRequest form(Map.Entry<?, ?> entry, String charset) throws HttpRequest.HttpRequestExceptionWrite the key and value in the entry as form data to the request bodyThe pair specified will be URL-encoded and sent with the 'application/x-www-form-urlencoded' content-type
- Parameters:
entry
-charset
-- Returns:
- this request
- Throws:
HttpRequest.HttpRequestException
-
form
Write the name/value pair as form data to the request bodyThe pair specified will be URL-encoded in UTF-8 and sent with the 'application/x-www-form-urlencoded' content-type
- Parameters:
name
-value
-- Returns:
- this request
- Throws:
HttpRequest.HttpRequestException
-
form
public HttpRequest form(Object name, Object value, String charset) throws HttpRequest.HttpRequestException Write the name/value pair as form data to the request bodyThe values specified will be URL-encoded and sent with the 'application/x-www-form-urlencoded' content-type
- Parameters:
name
-value
-charset
-- Returns:
- this request
- Throws:
HttpRequest.HttpRequestException
-
form
Write the values in the map as encoded form data to the request body- Parameters:
values
-charset
-- Returns:
- this request
- Throws:
HttpRequest.HttpRequestException
-
trustAllCerts
Configure HTTPS connection to trust all certificatesThis method does nothing if the current request is not a HTTPS request
- Returns:
- this request
- Throws:
HttpRequest.HttpRequestException
-
trustAllHosts
Configure HTTPS connection to trust all hosts using a customHostnameVerifier
that always returnstrue
for each host verifiedThis method does nothing if the current request is not a HTTPS request
- Returns:
- this request
-
url
Get theURL
of this request's connection- Returns:
- request URL
-
method
Get the HTTP method of this request- Returns:
- method
-
useProxy
Configure an HTTP proxy on this connection. Use {proxyBasic(String, String)
if this proxy requires basic authentication.- Parameters:
proxyHost
-proxyPort
-- Returns:
- this request
-
followRedirects
Set whether or not the underlying connection should follow redirects in the response.- Parameters:
followRedirects
- - true fo follow redirects, false to not.- Returns:
- this request
-