Class HttpRequest
- java.lang.Object
-
- com.github.kevinsawicki.http.HttpRequest
-
public class HttpRequest extends java.lang.Object
A fluid interface for making HTTP requests using an underlyingHttpURLConnection
(or sub-class).Each instance supports making a single request and cannot be reused for further requests.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
HttpRequest.Base64
Encodes and decodes to and from Base64 notation.protected static class
HttpRequest.CloseOperation<V>
Class that ensures aCloseable
gets closed with proper exception handling.static interface
HttpRequest.ConnectionFactory
CreatesHTTP connections
forurls
.protected static class
HttpRequest.FlushOperation<V>
Class that and ensures aFlushable
gets flushed with proper exception handling.static class
HttpRequest.HttpRequestException
HTTP request exception whose cause is always anIOException
protected static class
HttpRequest.Operation<V>
Operation that handles executing a callback once complete and handling nested exceptionsstatic class
HttpRequest.RequestOutputStream
Request output streamstatic interface
HttpRequest.UploadProgress
Callback interface for reporting upload progress for a request.
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.String
BOUNDARY
private int
bufferSize
static java.lang.String
CHARSET_UTF8
'UTF-8' charset nameprivate java.net.HttpURLConnection
connection
private static HttpRequest.ConnectionFactory
CONNECTION_FACTORY
static java.lang.String
CONTENT_TYPE_FORM
'application/x-www-form-urlencoded' content type header valuestatic java.lang.String
CONTENT_TYPE_JSON
'application/json' content type header valueprivate static java.lang.String
CONTENT_TYPE_MULTIPART
private static java.lang.String
CRLF
private static java.lang.String[]
EMPTY_STRINGS
static java.lang.String
ENCODING_GZIP
'gzip' encoding header valueprivate boolean
form
static java.lang.String
HEADER_ACCEPT
'Accept' header namestatic java.lang.String
HEADER_ACCEPT_CHARSET
'Accept-Charset' header namestatic java.lang.String
HEADER_ACCEPT_ENCODING
'Accept-Encoding' header namestatic java.lang.String
HEADER_AUTHORIZATION
'Authorization' header namestatic java.lang.String
HEADER_CACHE_CONTROL
'Cache-Control' header namestatic java.lang.String
HEADER_CONTENT_ENCODING
'Content-Encoding' header namestatic java.lang.String
HEADER_CONTENT_LENGTH
'Content-Length' header namestatic java.lang.String
HEADER_CONTENT_TYPE
'Content-Type' header namestatic java.lang.String
HEADER_DATE
'Date' header namestatic java.lang.String
HEADER_ETAG
'ETag' header namestatic java.lang.String
HEADER_EXPIRES
'Expires' header namestatic java.lang.String
HEADER_IF_NONE_MATCH
'If-None-Match' header namestatic java.lang.String
HEADER_LAST_MODIFIED
'Last-Modified' header namestatic java.lang.String
HEADER_LOCATION
'Location' header namestatic java.lang.String
HEADER_PROXY_AUTHORIZATION
'Proxy-Authorization' header namestatic java.lang.String
HEADER_REFERER
'Referer' header namestatic java.lang.String
HEADER_SERVER
'Server' header namestatic java.lang.String
HEADER_USER_AGENT
'User-Agent' header nameprivate java.lang.String
httpProxyHost
private int
httpProxyPort
private boolean
ignoreCloseExceptions
static java.lang.String
METHOD_DELETE
'DELETE' request methodstatic java.lang.String
METHOD_GET
'GET' request methodstatic java.lang.String
METHOD_HEAD
'HEAD' request methodstatic java.lang.String
METHOD_OPTIONS
'OPTIONS' options methodstatic java.lang.String
METHOD_POST
'POST' request methodstatic java.lang.String
METHOD_PUT
'PUT' request methodstatic java.lang.String
METHOD_TRACE
'TRACE' request methodprivate boolean
multipart
private HttpRequest.RequestOutputStream
output
static java.lang.String
PARAM_CHARSET
'charset' header value parameterprivate HttpRequest.UploadProgress
progress
private java.lang.String
requestMethod
private long
totalSize
private long
totalWritten
private static javax.net.ssl.SSLSocketFactory
TRUSTED_FACTORY
private static javax.net.ssl.HostnameVerifier
TRUSTED_VERIFIER
private boolean
uncompress
private java.net.URL
url
-
Constructor Summary
Constructors Constructor Description HttpRequest(java.lang.CharSequence url, java.lang.String method)
Create HTTP connection wrapperHttpRequest(java.net.URL url, java.lang.String method)
Create HTTP connection wrapper
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description HttpRequest
accept(java.lang.String accept)
Set the 'Accept' header to given valueHttpRequest
acceptCharset(java.lang.String acceptCharset)
Set the 'Accept-Charset' header to given valueHttpRequest
acceptEncoding(java.lang.String acceptEncoding)
Set the 'Accept-Encoding' header to given valueHttpRequest
acceptGzipEncoding()
Set the 'Accept-Encoding' header to 'gzip'HttpRequest
acceptJson()
Set the 'Accept' header to 'application/json'private static java.lang.StringBuilder
addParam(java.lang.Object key, java.lang.Object value, java.lang.StringBuilder result)
private static java.lang.StringBuilder
addParamPrefix(java.lang.String baseUrl, java.lang.StringBuilder result)
private static java.lang.StringBuilder
addPathSeparator(java.lang.String baseUrl, java.lang.StringBuilder result)
static java.lang.String
append(java.lang.CharSequence url, java.lang.Object... params)
Append given name/value pairs as query parameters to the base URLstatic java.lang.String
append(java.lang.CharSequence url, java.util.Map<?,?> params)
Append given map as query parameters to the base URLprivate static java.util.List<java.lang.Object>
arrayToList(java.lang.Object array)
Represents array of any type as list of objects so we can easily iterate over itHttpRequest
authorization(java.lang.String authorization)
Set the 'Authorization' header to given valueboolean
badRequest()
Is the response code a 400 Bad Request?HttpRequest
basic(java.lang.String name, java.lang.String password)
Set the 'Authorization' header to given values in Basic authentication formatjava.lang.String
body()
Get response asString
using character set returned fromcharset()
java.lang.String
body(java.lang.String charset)
Get response asString
in given character setHttpRequest
body(java.util.concurrent.atomic.AtomicReference<java.lang.String> output)
Get the response body as aString
and set it as the value of the given reference.HttpRequest
body(java.util.concurrent.atomic.AtomicReference<java.lang.String> output, java.lang.String charset)
Get the response body as aString
and set it as the value of the given reference.java.io.BufferedInputStream
buffer()
Get response in a buffered streamjava.io.BufferedReader
bufferedReader()
Get buffered reader to response body using the character set returned fromcharset()
and the configured buffer sizejava.io.BufferedReader
bufferedReader(java.lang.String charset)
Get buffered reader to response body using the given character set r and the configured buffer sizeint
bufferSize()
Get the configured buffer sizeHttpRequest
bufferSize(int size)
Set the size used when buffering and copying between streamsbyte[]
bytes()
Get response as byte arrayprotected java.io.ByteArrayOutputStream
byteStream()
Create byte array output streamjava.lang.String
cacheControl()
Get the 'Cache-Control' header from the responsejava.lang.String
charset()
Get 'charset' parameter from 'Content-Type' response headerHttpRequest
chunk(int size)
Set chunked streaming mode to the given sizeprotected HttpRequest
closeOutput()
Close output streamprotected HttpRequest
closeOutputQuietly()
int
code()
Get the status code of the responseHttpRequest
code(java.util.concurrent.atomic.AtomicInteger output)
Set the value of the givenAtomicInteger
to the status code of the responseHttpRequest
connectTimeout(int timeout)
Set connect timeout on connection to given valuejava.lang.String
contentEncoding()
Get the 'Content-Encoding' header from the responseint
contentLength()
Get the 'Content-Length' header from the responseHttpRequest
contentLength(int contentLength)
Set the 'Content-Length' request header to the given valueHttpRequest
contentLength(java.lang.String contentLength)
Set the 'Content-Length' request header to the given valuejava.lang.String
contentType()
Get the 'Content-Type' header from the responseHttpRequest
contentType(java.lang.String contentType)
Set the 'Content-Type' request header to the given valueHttpRequest
contentType(java.lang.String contentType, java.lang.String charset)
Set the 'Content-Type' request header to the given value and charsetprotected HttpRequest
copy(java.io.InputStream input, java.io.OutputStream output)
Copy from input stream to output streamprotected HttpRequest
copy(java.io.Reader input, java.io.Writer output)
Copy from reader to writerprivate java.net.HttpURLConnection
createConnection()
boolean
created()
Is the response code a 201 Created?private java.net.Proxy
createProxy()
long
date()
Get the 'Date' header from the responselong
dateHeader(java.lang.String name)
Get a date header from the response falling back to returning -1 if the header is missing or parsing failslong
dateHeader(java.lang.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(java.lang.CharSequence url)
Start a 'DELETE' request to the given URLstatic HttpRequest
delete(java.lang.CharSequence baseUrl, boolean encode, java.lang.Object... params)
Start a 'DELETE' request to the given URL along with the query paramsstatic HttpRequest
delete(java.lang.CharSequence baseUrl, java.util.Map<?,?> params, boolean encode)
Start a 'DELETE' request to the given URL along with the query paramsstatic HttpRequest
delete(java.net.URL url)
Start a 'DELETE' request to the given URLHttpRequest
disconnect()
Disconnect the connectionstatic java.lang.String
encode(java.lang.CharSequence url)
Encode the given URL as an ASCIIString
java.lang.String
eTag()
Get the 'ETag' header from the responselong
expires()
Get the 'Expires' header from the responseHttpRequest
followRedirects(boolean followRedirects)
Set whether or not the underlying connection should follow redirects in the response.HttpRequest
form(java.lang.Object name, java.lang.Object value)
Write the name/value pair as form data to the request bodyHttpRequest
form(java.lang.Object name, java.lang.Object value, java.lang.String charset)
Write the name/value pair as form data to the request bodyHttpRequest
form(java.util.Map.Entry<?,?> entry)
Write the key and value in the entry as form data to the request bodyHttpRequest
form(java.util.Map.Entry<?,?> entry, java.lang.String charset)
Write the key and value in the entry as form data to the request bodyHttpRequest
form(java.util.Map<?,?> values)
Write the values in the map as form data to the request bodyHttpRequest
form(java.util.Map<?,?> values, java.lang.String charset)
Write the values in the map as encoded form data to the request bodystatic HttpRequest
get(java.lang.CharSequence url)
Start a 'GET' request to the given URLstatic HttpRequest
get(java.lang.CharSequence baseUrl, boolean encode, java.lang.Object... params)
Start a 'GET' request to the given URL along with the query paramsstatic HttpRequest
get(java.lang.CharSequence baseUrl, java.util.Map<?,?> params, boolean encode)
Start a 'GET' request to the given URL along with the query paramsstatic HttpRequest
get(java.net.URL url)
Start a 'GET' request to the given URLjava.net.HttpURLConnection
getConnection()
Get underlying connectionprotected java.lang.String
getParam(java.lang.String value, java.lang.String paramName)
Get parameter value from header valueprotected java.util.Map<java.lang.String,java.lang.String>
getParams(java.lang.String header)
Get parameter values from header valueprivate static javax.net.ssl.SSLSocketFactory
getTrustedFactory()
private static javax.net.ssl.HostnameVerifier
getTrustedVerifier()
private static java.lang.String
getValidCharset(java.lang.String charset)
static HttpRequest
head(java.lang.CharSequence url)
Start a 'HEAD' request to the given URLstatic HttpRequest
head(java.lang.CharSequence baseUrl, boolean encode, java.lang.Object... params)
Start a 'GET' request to the given URL along with the query paramsstatic HttpRequest
head(java.lang.CharSequence baseUrl, java.util.Map<?,?> params, boolean encode)
Start a 'HEAD' request to the given URL along with the query paramsstatic HttpRequest
head(java.net.URL url)
Start a 'HEAD' request to the given URLjava.lang.String
header(java.lang.String name)
Get a response headerHttpRequest
header(java.lang.String name, java.lang.Number value)
Set header name to given valueHttpRequest
header(java.lang.String name, java.lang.String value)
Set header name to given valueHttpRequest
header(java.util.Map.Entry<java.lang.String,java.lang.String> header)
Set header to have given entry's key as the name and value as the valuejava.util.Map<java.lang.String,java.util.List<java.lang.String>>
headers()
Get all the response headersjava.lang.String[]
headers(java.lang.String name)
Get all values of the given header from the responseHttpRequest
headers(java.util.Map<java.lang.String,java.lang.String> headers)
Set all headers found in given map where the keys are the header names and the values are the header valuesHttpRequest
ifModifiedSince(long ifModifiedSince)
Set the 'If-Modified-Since' request header to the given valueHttpRequest
ifNoneMatch(java.lang.String ifNoneMatch)
Set the 'If-None-Match' request header to the given valueboolean
ignoreCloseExceptions()
Get whether or not exceptions thrown byCloseable.close()
are ignoredHttpRequest
ignoreCloseExceptions(boolean ignore)
Set whether or not to ignore exceptions that occur from callingCloseable.close()
private HttpRequest
incrementTotalSize(long size)
int
intHeader(java.lang.String name)
Get an integer header from the response falling back to returning -1 if the header is missing or parsing failsint
intHeader(java.lang.String name, int defaultValue)
Get an integer header value from the response falling back to the given default value if the header is missing or if parsing failsboolean
isBodyEmpty()
Is the response body empty?static void
keepAlive(boolean keepAlive)
Set the 'http.keepAlive' property to the given value.long
lastModified()
Get the 'Last-Modified' header from the responsejava.lang.String
location()
Get the 'Location' header from the responsestatic void
maxConnections(int maxConnections)
Set the 'http.maxConnections' property to the given value.java.lang.String
message()
Get status message of the responsejava.lang.String
method()
Get the HTTP method of this requestboolean
noContent()
Is the response code a 204 No Content?static void
nonProxyHosts(java.lang.String... hosts)
Set the 'http.nonProxyHosts' property to the given host values.boolean
notFound()
Is the response code a 404 Not Found?boolean
notModified()
Is the response code a 304 Not Modified?boolean
ok()
Is the response code a 200 OK?protected HttpRequest
openOutput()
Open output streamstatic HttpRequest
options(java.lang.CharSequence url)
Start an 'OPTIONS' request to the given URLstatic HttpRequest
options(java.net.URL url)
Start an 'OPTIONS' request to the given URLjava.lang.String
parameter(java.lang.String headerName, java.lang.String paramName)
Get parameter with given name from header value in responsejava.util.Map<java.lang.String,java.lang.String>
parameters(java.lang.String headerName)
Get all parameters from header value in responseHttpRequest
part(java.lang.String name, java.io.File part)
Write part of a multipart request to the request bodyHttpRequest
part(java.lang.String name, java.io.InputStream part)
Write part of a multipart request to the request bodyHttpRequest
part(java.lang.String name, java.lang.Number part)
Write part of a multipart request to the request bodyHttpRequest
part(java.lang.String name, java.lang.String part)
Write part of a multipart request to the request bodyHttpRequest
part(java.lang.String name, java.lang.String filename, java.io.File part)
Write part of a multipart request to the request bodyHttpRequest
part(java.lang.String name, java.lang.String filename, java.lang.Number part)
Write part of a multipart request to the request bodyHttpRequest
part(java.lang.String name, java.lang.String filename, java.lang.String part)
Write part of a multipart request to the request bodyHttpRequest
part(java.lang.String name, java.lang.String filename, java.lang.String contentType, java.io.File part)
Write part of a multipart request to the request bodyHttpRequest
part(java.lang.String name, java.lang.String filename, java.lang.String contentType, java.io.InputStream part)
Write part of a multipart request to the request bodyHttpRequest
part(java.lang.String name, java.lang.String filename, java.lang.String contentType, java.lang.String part)
Write part of a multipart request to the request bodyHttpRequest
partHeader(java.lang.String name, java.lang.String value)
Write a multipart header to the response bodystatic HttpRequest
post(java.lang.CharSequence url)
Start a 'POST' request to the given URLstatic HttpRequest
post(java.lang.CharSequence baseUrl, boolean encode, java.lang.Object... params)
Start a 'POST' request to the given URL along with the query paramsstatic HttpRequest
post(java.lang.CharSequence baseUrl, java.util.Map<?,?> params, boolean encode)
Start a 'POST' request to the given URL along with the query paramsstatic HttpRequest
post(java.net.URL url)
Start a 'POST' request to the given URLHttpRequest
progress(HttpRequest.UploadProgress callback)
Set the UploadProgress callback for this requestHttpRequest
proxyAuthorization(java.lang.String proxyAuthorization)
Set the 'Proxy-Authorization' header to given valueHttpRequest
proxyBasic(java.lang.String name, java.lang.String password)
Set the 'Proxy-Authorization' header to given values in Basic authentication formatstatic void
proxyHost(java.lang.String host)
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(java.lang.CharSequence url)
Start a 'PUT' request to the given URLstatic HttpRequest
put(java.lang.CharSequence baseUrl, boolean encode, java.lang.Object... params)
Start a 'PUT' request to the given URL along with the query paramsstatic HttpRequest
put(java.lang.CharSequence baseUrl, java.util.Map<?,?> params, boolean encode)
Start a 'PUT' request to the given URL along with the query paramsstatic HttpRequest
put(java.net.URL url)
Start a 'PUT' request to the given URLjava.io.InputStreamReader
reader()
Get reader to response body using the character set returned fromcharset()
java.io.InputStreamReader
reader(java.lang.String charset)
Get reader to response body using given character set.HttpRequest
readTimeout(int timeout)
Set read timeout on connection to given valueHttpRequest
receive(java.io.File file)
Stream response body to fileHttpRequest
receive(java.io.OutputStream output)
Stream response to given output streamHttpRequest
receive(java.io.PrintStream output)
Stream response to given print streamHttpRequest
receive(java.io.Writer writer)
Receive response into the given writerHttpRequest
receive(java.lang.Appendable appendable)
Receive response into the given appendableHttpRequest
referer(java.lang.String referer)
Set the 'Referer' header to given valueHttpRequest
send(byte[] input)
Write byte array to request bodyHttpRequest
send(java.io.File input)
Write contents of file to request bodyHttpRequest
send(java.io.InputStream input)
Write stream to request bodyHttpRequest
send(java.io.Reader input)
Write reader to request bodyHttpRequest
send(java.lang.CharSequence value)
Write char sequence to request bodyjava.lang.String
server()
Get the 'Server' header from the responseboolean
serverError()
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 java.lang.String
setProperty(java.lang.String name, java.lang.String value)
Set property to given value.protected HttpRequest
startPart()
Start part of a multipartjava.io.InputStream
stream()
Get stream to response bodyjava.lang.String
toString()
static HttpRequest
trace(java.lang.CharSequence url)
Start a 'TRACE' request to the given URLstatic HttpRequest
trace(java.net.URL url)
Start a 'TRACE' request to the given URLHttpRequest
trustAllCerts()
Configure HTTPS connection to trust all certificatesHttpRequest
trustAllHosts()
Configure HTTPS connection to trust all hosts using a customHostnameVerifier
that always returnstrue
for each host verifiedHttpRequest
uncompress(boolean uncompress)
Set whether or not the response body should be automatically uncompressed when read from.java.net.URL
url()
Get theURL
of this request's connectionHttpRequest
useCaches(boolean useCaches)
Set value ofURLConnection.setUseCaches(boolean)
HttpRequest
useProxy(java.lang.String proxyHost, int proxyPort)
Configure an HTTP proxy on this connection.HttpRequest
userAgent(java.lang.String userAgent)
Set the 'User-Agent' header to given valueprotected HttpRequest
writePartHeader(java.lang.String name, java.lang.String filename)
Write part headerprotected HttpRequest
writePartHeader(java.lang.String name, java.lang.String filename, java.lang.String contentType)
Write part headerjava.io.OutputStreamWriter
writer()
Create writer to request output stream
-
-
-
Field Detail
-
CHARSET_UTF8
public static final java.lang.String CHARSET_UTF8
'UTF-8' charset name- See Also:
- Constant Field Values
-
CONTENT_TYPE_FORM
public static final java.lang.String CONTENT_TYPE_FORM
'application/x-www-form-urlencoded' content type header value- See Also:
- Constant Field Values
-
CONTENT_TYPE_JSON
public static final java.lang.String CONTENT_TYPE_JSON
'application/json' content type header value- See Also:
- Constant Field Values
-
ENCODING_GZIP
public static final java.lang.String ENCODING_GZIP
'gzip' encoding header value- See Also:
- Constant Field Values
-
HEADER_ACCEPT
public static final java.lang.String HEADER_ACCEPT
'Accept' header name- See Also:
- Constant Field Values
-
HEADER_ACCEPT_CHARSET
public static final java.lang.String HEADER_ACCEPT_CHARSET
'Accept-Charset' header name- See Also:
- Constant Field Values
-
HEADER_ACCEPT_ENCODING
public static final java.lang.String HEADER_ACCEPT_ENCODING
'Accept-Encoding' header name- See Also:
- Constant Field Values
-
HEADER_AUTHORIZATION
public static final java.lang.String HEADER_AUTHORIZATION
'Authorization' header name- See Also:
- Constant Field Values
-
HEADER_CACHE_CONTROL
public static final java.lang.String HEADER_CACHE_CONTROL
'Cache-Control' header name- See Also:
- Constant Field Values
-
HEADER_CONTENT_ENCODING
public static final java.lang.String HEADER_CONTENT_ENCODING
'Content-Encoding' header name- See Also:
- Constant Field Values
-
HEADER_CONTENT_LENGTH
public static final java.lang.String HEADER_CONTENT_LENGTH
'Content-Length' header name- See Also:
- Constant Field Values
-
HEADER_CONTENT_TYPE
public static final java.lang.String HEADER_CONTENT_TYPE
'Content-Type' header name- See Also:
- Constant Field Values
-
HEADER_DATE
public static final java.lang.String HEADER_DATE
'Date' header name- See Also:
- Constant Field Values
-
HEADER_ETAG
public static final java.lang.String HEADER_ETAG
'ETag' header name- See Also:
- Constant Field Values
-
HEADER_EXPIRES
public static final java.lang.String HEADER_EXPIRES
'Expires' header name- See Also:
- Constant Field Values
-
HEADER_IF_NONE_MATCH
public static final java.lang.String HEADER_IF_NONE_MATCH
'If-None-Match' header name- See Also:
- Constant Field Values
-
HEADER_LAST_MODIFIED
public static final java.lang.String HEADER_LAST_MODIFIED
'Last-Modified' header name- See Also:
- Constant Field Values
-
HEADER_LOCATION
public static final java.lang.String HEADER_LOCATION
'Location' header name- See Also:
- Constant Field Values
-
HEADER_PROXY_AUTHORIZATION
public static final java.lang.String HEADER_PROXY_AUTHORIZATION
'Proxy-Authorization' header name- See Also:
- Constant Field Values
-
HEADER_REFERER
public static final java.lang.String HEADER_REFERER
'Referer' header name- See Also:
- Constant Field Values
-
HEADER_SERVER
public static final java.lang.String HEADER_SERVER
'Server' header name- See Also:
- Constant Field Values
-
HEADER_USER_AGENT
public static final java.lang.String HEADER_USER_AGENT
'User-Agent' header name- See Also:
- Constant Field Values
-
METHOD_DELETE
public static final java.lang.String METHOD_DELETE
'DELETE' request method- See Also:
- Constant Field Values
-
METHOD_GET
public static final java.lang.String METHOD_GET
'GET' request method- See Also:
- Constant Field Values
-
METHOD_HEAD
public static final java.lang.String METHOD_HEAD
'HEAD' request method- See Also:
- Constant Field Values
-
METHOD_OPTIONS
public static final java.lang.String METHOD_OPTIONS
'OPTIONS' options method- See Also:
- Constant Field Values
-
METHOD_POST
public static final java.lang.String METHOD_POST
'POST' request method- See Also:
- Constant Field Values
-
METHOD_PUT
public static final java.lang.String METHOD_PUT
'PUT' request method- See Also:
- Constant Field Values
-
METHOD_TRACE
public static final java.lang.String METHOD_TRACE
'TRACE' request method- See Also:
- Constant Field Values
-
PARAM_CHARSET
public static final java.lang.String PARAM_CHARSET
'charset' header value parameter- See Also:
- Constant Field Values
-
BOUNDARY
private static final java.lang.String BOUNDARY
- See Also:
- Constant Field Values
-
CONTENT_TYPE_MULTIPART
private static final java.lang.String CONTENT_TYPE_MULTIPART
- See Also:
- Constant Field Values
-
CRLF
private static final java.lang.String CRLF
- See Also:
- Constant Field Values
-
EMPTY_STRINGS
private static final java.lang.String[] EMPTY_STRINGS
-
TRUSTED_FACTORY
private static javax.net.ssl.SSLSocketFactory TRUSTED_FACTORY
-
TRUSTED_VERIFIER
private static javax.net.ssl.HostnameVerifier TRUSTED_VERIFIER
-
CONNECTION_FACTORY
private static HttpRequest.ConnectionFactory CONNECTION_FACTORY
-
connection
private java.net.HttpURLConnection connection
-
url
private final java.net.URL url
-
requestMethod
private final java.lang.String requestMethod
-
output
private HttpRequest.RequestOutputStream 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
private java.lang.String httpProxyHost
-
httpProxyPort
private int httpProxyPort
-
progress
private HttpRequest.UploadProgress progress
-
-
Constructor Detail
-
HttpRequest
public HttpRequest(java.lang.CharSequence url, java.lang.String method) throws HttpRequest.HttpRequestException
Create HTTP connection wrapper- Parameters:
url
- Remote resource URL.method
- HTTP request method (e.g., "GET", "POST").- Throws:
HttpRequest.HttpRequestException
-
HttpRequest
public HttpRequest(java.net.URL url, java.lang.String method) throws HttpRequest.HttpRequestException
Create HTTP connection wrapper- Parameters:
url
- Remote resource URL.method
- HTTP request method (e.g., "GET", "POST").- Throws:
HttpRequest.HttpRequestException
-
-
Method Detail
-
getValidCharset
private static java.lang.String getValidCharset(java.lang.String charset)
-
getTrustedFactory
private static javax.net.ssl.SSLSocketFactory getTrustedFactory() throws HttpRequest.HttpRequestException
- Throws:
HttpRequest.HttpRequestException
-
getTrustedVerifier
private static javax.net.ssl.HostnameVerifier getTrustedVerifier()
-
addPathSeparator
private static java.lang.StringBuilder addPathSeparator(java.lang.String baseUrl, java.lang.StringBuilder result)
-
addParamPrefix
private static java.lang.StringBuilder addParamPrefix(java.lang.String baseUrl, java.lang.StringBuilder result)
-
addParam
private static java.lang.StringBuilder addParam(java.lang.Object key, java.lang.Object value, java.lang.StringBuilder result)
-
setConnectionFactory
public static void setConnectionFactory(HttpRequest.ConnectionFactory connectionFactory)
Specify theHttpRequest.ConnectionFactory
used to create new requests.
-
arrayToList
private static java.util.List<java.lang.Object> arrayToList(java.lang.Object array)
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
public static java.lang.String encode(java.lang.CharSequence url) throws HttpRequest.HttpRequestException
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
public static java.lang.String append(java.lang.CharSequence url, java.util.Map<?,?> params)
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
public static java.lang.String append(java.lang.CharSequence url, java.lang.Object... params)
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
public static HttpRequest get(java.lang.CharSequence url) throws HttpRequest.HttpRequestException
Start a 'GET' request to the given URL- Parameters:
url
-- Returns:
- request
- Throws:
HttpRequest.HttpRequestException
-
get
public static HttpRequest get(java.net.URL url) throws HttpRequest.HttpRequestException
Start a 'GET' request to the given URL- Parameters:
url
-- Returns:
- request
- Throws:
HttpRequest.HttpRequestException
-
get
public static HttpRequest get(java.lang.CharSequence baseUrl, java.util.Map<?,?> params, boolean encode)
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:
append(CharSequence, Map)
,encode(CharSequence)
-
get
public static HttpRequest get(java.lang.CharSequence baseUrl, boolean encode, java.lang.Object... params)
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:
append(CharSequence, Object...)
,encode(CharSequence)
-
post
public static HttpRequest post(java.lang.CharSequence url) throws HttpRequest.HttpRequestException
Start a 'POST' request to the given URL- Parameters:
url
-- Returns:
- request
- Throws:
HttpRequest.HttpRequestException
-
post
public static HttpRequest post(java.net.URL url) throws HttpRequest.HttpRequestException
Start a 'POST' request to the given URL- Parameters:
url
-- Returns:
- request
- Throws:
HttpRequest.HttpRequestException
-
post
public static HttpRequest post(java.lang.CharSequence baseUrl, java.util.Map<?,?> params, boolean encode)
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:
append(CharSequence, Map)
,encode(CharSequence)
-
post
public static HttpRequest post(java.lang.CharSequence baseUrl, boolean encode, java.lang.Object... params)
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:
append(CharSequence, Object...)
,encode(CharSequence)
-
put
public static HttpRequest put(java.lang.CharSequence url) throws HttpRequest.HttpRequestException
Start a 'PUT' request to the given URL- Parameters:
url
-- Returns:
- request
- Throws:
HttpRequest.HttpRequestException
-
put
public static HttpRequest put(java.net.URL url) throws HttpRequest.HttpRequestException
Start a 'PUT' request to the given URL- Parameters:
url
-- Returns:
- request
- Throws:
HttpRequest.HttpRequestException
-
put
public static HttpRequest put(java.lang.CharSequence baseUrl, java.util.Map<?,?> params, boolean encode)
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:
append(CharSequence, Map)
,encode(CharSequence)
-
put
public static HttpRequest put(java.lang.CharSequence baseUrl, boolean encode, java.lang.Object... params)
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:
append(CharSequence, Object...)
,encode(CharSequence)
-
delete
public static HttpRequest delete(java.lang.CharSequence url) throws HttpRequest.HttpRequestException
Start a 'DELETE' request to the given URL- Parameters:
url
-- Returns:
- request
- Throws:
HttpRequest.HttpRequestException
-
delete
public static HttpRequest delete(java.net.URL url) throws HttpRequest.HttpRequestException
Start a 'DELETE' request to the given URL- Parameters:
url
-- Returns:
- request
- Throws:
HttpRequest.HttpRequestException
-
delete
public static HttpRequest delete(java.lang.CharSequence baseUrl, java.util.Map<?,?> params, boolean encode)
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:
append(CharSequence, Map)
,encode(CharSequence)
-
delete
public static HttpRequest delete(java.lang.CharSequence baseUrl, boolean encode, java.lang.Object... params)
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:
append(CharSequence, Object...)
,encode(CharSequence)
-
head
public static HttpRequest head(java.lang.CharSequence url) throws HttpRequest.HttpRequestException
Start a 'HEAD' request to the given URL- Parameters:
url
-- Returns:
- request
- Throws:
HttpRequest.HttpRequestException
-
head
public static HttpRequest head(java.net.URL url) throws HttpRequest.HttpRequestException
Start a 'HEAD' request to the given URL- Parameters:
url
-- Returns:
- request
- Throws:
HttpRequest.HttpRequestException
-
head
public static HttpRequest head(java.lang.CharSequence baseUrl, java.util.Map<?,?> params, boolean encode)
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:
append(CharSequence, Map)
,encode(CharSequence)
-
head
public static HttpRequest head(java.lang.CharSequence baseUrl, boolean encode, java.lang.Object... params)
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:
append(CharSequence, Object...)
,encode(CharSequence)
-
options
public static HttpRequest options(java.lang.CharSequence url) throws HttpRequest.HttpRequestException
Start an 'OPTIONS' request to the given URL- Parameters:
url
-- Returns:
- request
- Throws:
HttpRequest.HttpRequestException
-
options
public static HttpRequest options(java.net.URL url) throws HttpRequest.HttpRequestException
Start an 'OPTIONS' request to the given URL- Parameters:
url
-- Returns:
- request
- Throws:
HttpRequest.HttpRequestException
-
trace
public static HttpRequest trace(java.lang.CharSequence url) throws HttpRequest.HttpRequestException
Start a 'TRACE' request to the given URL- Parameters:
url
-- Returns:
- request
- Throws:
HttpRequest.HttpRequestException
-
trace
public static HttpRequest trace(java.net.URL url) throws HttpRequest.HttpRequestException
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
public static void proxyHost(java.lang.String host)
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
public static void nonProxyHosts(java.lang.String... hosts)
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
private static java.lang.String setProperty(java.lang.String name, java.lang.String value)
Set property to given value.Specifying a null value will cause the property to be cleared
- Parameters:
name
-value
-- Returns:
- previous value
-
createProxy
private java.net.Proxy createProxy()
-
createConnection
private java.net.HttpURLConnection createConnection()
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
getConnection
public java.net.HttpURLConnection getConnection()
Get underlying connection- Returns:
- connection
-
ignoreCloseExceptions
public HttpRequest ignoreCloseExceptions(boolean ignore)
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
public int code() throws HttpRequest.HttpRequestException
Get the status code of the response- Returns:
- the response code
- Throws:
HttpRequest.HttpRequestException
-
code
public HttpRequest code(java.util.concurrent.atomic.AtomicInteger output) throws HttpRequest.HttpRequestException
Set the value of the givenAtomicInteger
to the status code of the response- Parameters:
output
-- Returns:
- this request
- Throws:
HttpRequest.HttpRequestException
-
ok
public boolean ok() throws HttpRequest.HttpRequestException
Is the response code a 200 OK?- Returns:
- true if 200, false otherwise
- Throws:
HttpRequest.HttpRequestException
-
created
public boolean created() throws HttpRequest.HttpRequestException
Is the response code a 201 Created?- Returns:
- true if 201, false otherwise
- Throws:
HttpRequest.HttpRequestException
-
noContent
public boolean noContent() throws HttpRequest.HttpRequestException
Is the response code a 204 No Content?- Returns:
- true if 204, false otherwise
- Throws:
HttpRequest.HttpRequestException
-
serverError
public boolean serverError() throws HttpRequest.HttpRequestException
Is the response code a 500 Internal Server Error?- Returns:
- true if 500, false otherwise
- Throws:
HttpRequest.HttpRequestException
-
badRequest
public boolean badRequest() throws HttpRequest.HttpRequestException
Is the response code a 400 Bad Request?- Returns:
- true if 400, false otherwise
- Throws:
HttpRequest.HttpRequestException
-
notFound
public boolean notFound() throws HttpRequest.HttpRequestException
Is the response code a 404 Not Found?- Returns:
- true if 404, false otherwise
- Throws:
HttpRequest.HttpRequestException
-
notModified
public boolean notModified() throws HttpRequest.HttpRequestException
Is the response code a 304 Not Modified?- Returns:
- true if 304, false otherwise
- Throws:
HttpRequest.HttpRequestException
-
message
public java.lang.String message() throws HttpRequest.HttpRequestException
Get status message of the response- Returns:
- message
- Throws:
HttpRequest.HttpRequestException
-
disconnect
public HttpRequest disconnect()
Disconnect the connection- Returns:
- this request
-
chunk
public HttpRequest chunk(int size)
Set chunked streaming mode to the given size- Parameters:
size
-- Returns:
- this request
-
bufferSize
public HttpRequest bufferSize(int size)
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
public HttpRequest uncompress(boolean 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
protected java.io.ByteArrayOutputStream byteStream()
Create byte array output stream- Returns:
- stream
-
body
public java.lang.String body(java.lang.String charset) throws HttpRequest.HttpRequestException
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
public java.lang.String body() throws HttpRequest.HttpRequestException
Get response asString
using character set returned fromcharset()
- Returns:
- string
- Throws:
HttpRequest.HttpRequestException
-
body
public HttpRequest body(java.util.concurrent.atomic.AtomicReference<java.lang.String> output) throws HttpRequest.HttpRequestException
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(java.util.concurrent.atomic.AtomicReference<java.lang.String> output, java.lang.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
public boolean isBodyEmpty() throws HttpRequest.HttpRequestException
Is the response body empty?- Returns:
- true if the Content-Length response header is 0, false otherwise
- Throws:
HttpRequest.HttpRequestException
-
bytes
public byte[] bytes() throws HttpRequest.HttpRequestException
Get response as byte array- Returns:
- byte array
- Throws:
HttpRequest.HttpRequestException
-
buffer
public java.io.BufferedInputStream buffer() throws HttpRequest.HttpRequestException
Get response in a buffered stream- Returns:
- stream
- Throws:
HttpRequest.HttpRequestException
- See Also:
bufferSize(int)
-
stream
public java.io.InputStream stream() throws HttpRequest.HttpRequestException
Get stream to response body- Returns:
- stream
- Throws:
HttpRequest.HttpRequestException
-
reader
public java.io.InputStreamReader reader(java.lang.String charset) throws HttpRequest.HttpRequestException
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
public java.io.InputStreamReader reader() throws HttpRequest.HttpRequestException
Get reader to response body using the character set returned fromcharset()
- Returns:
- reader
- Throws:
HttpRequest.HttpRequestException
-
bufferedReader
public java.io.BufferedReader bufferedReader(java.lang.String charset) throws HttpRequest.HttpRequestException
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:
bufferSize(int)
-
bufferedReader
public java.io.BufferedReader bufferedReader() throws HttpRequest.HttpRequestException
Get buffered reader to response body using the character set returned fromcharset()
and the configured buffer size- Returns:
- reader
- Throws:
HttpRequest.HttpRequestException
- See Also:
bufferSize(int)
-
receive
public HttpRequest receive(java.io.File file) throws HttpRequest.HttpRequestException
Stream response body to file- Parameters:
file
-- Returns:
- this request
- Throws:
HttpRequest.HttpRequestException
-
receive
public HttpRequest receive(java.io.OutputStream output) throws HttpRequest.HttpRequestException
Stream response to given output stream- Parameters:
output
-- Returns:
- this request
- Throws:
HttpRequest.HttpRequestException
-
receive
public HttpRequest receive(java.io.PrintStream output) throws HttpRequest.HttpRequestException
Stream response to given print stream- Parameters:
output
-- Returns:
- this request
- Throws:
HttpRequest.HttpRequestException
-
receive
public HttpRequest receive(java.lang.Appendable appendable) throws HttpRequest.HttpRequestException
Receive response into the given appendable- Parameters:
appendable
-- Returns:
- this request
- Throws:
HttpRequest.HttpRequestException
-
receive
public HttpRequest receive(java.io.Writer writer) throws HttpRequest.HttpRequestException
Receive response into the given writer- Parameters:
writer
-- Returns:
- this request
- Throws:
HttpRequest.HttpRequestException
-
readTimeout
public HttpRequest readTimeout(int timeout)
Set read timeout on connection to given value- Parameters:
timeout
-- Returns:
- this request
-
connectTimeout
public HttpRequest connectTimeout(int timeout)
Set connect timeout on connection to given value- Parameters:
timeout
-- Returns:
- this request
-
header
public HttpRequest header(java.lang.String name, java.lang.String value)
Set header name to given value- Parameters:
name
-value
-- Returns:
- this request
-
header
public HttpRequest header(java.lang.String name, java.lang.Number value)
Set header name to given value- Parameters:
name
-value
-- Returns:
- this request
-
headers
public HttpRequest headers(java.util.Map<java.lang.String,java.lang.String> 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
public HttpRequest header(java.util.Map.Entry<java.lang.String,java.lang.String> header)
Set header to have given entry's key as the name and value as the value- Parameters:
header
-- Returns:
- this request
-
header
public java.lang.String header(java.lang.String name) throws HttpRequest.HttpRequestException
Get a response header- Parameters:
name
-- Returns:
- response header
- Throws:
HttpRequest.HttpRequestException
-
headers
public java.util.Map<java.lang.String,java.util.List<java.lang.String>> headers() throws HttpRequest.HttpRequestException
Get all the response headers- Returns:
- map of response header names to their value(s)
- Throws:
HttpRequest.HttpRequestException
-
dateHeader
public long dateHeader(java.lang.String name) throws HttpRequest.HttpRequestException
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
public long dateHeader(java.lang.String name, long defaultValue) throws HttpRequest.HttpRequestException
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
public int intHeader(java.lang.String name) throws HttpRequest.HttpRequestException
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
public int intHeader(java.lang.String name, int defaultValue) throws HttpRequest.HttpRequestException
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
public java.lang.String[] headers(java.lang.String name)
Get all values of the given header from the response- Parameters:
name
-- Returns:
- non-null but possibly empty array of
String
header values
-
parameter
public java.lang.String parameter(java.lang.String headerName, java.lang.String paramName)
Get parameter with given name from header value in response- Parameters:
headerName
-paramName
-- Returns:
- parameter value or null if missing
-
parameters
public java.util.Map<java.lang.String,java.lang.String> parameters(java.lang.String headerName)
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
protected java.util.Map<java.lang.String,java.lang.String> getParams(java.lang.String header)
Get parameter values from header value- Parameters:
header
-- Returns:
- parameter value or null if none
-
getParam
protected java.lang.String getParam(java.lang.String value, java.lang.String paramName)
Get parameter value from header value- Parameters:
value
-paramName
-- Returns:
- parameter value or null if none
-
charset
public java.lang.String charset()
Get 'charset' parameter from 'Content-Type' response header- Returns:
- charset or null if none
-
userAgent
public HttpRequest userAgent(java.lang.String userAgent)
Set the 'User-Agent' header to given value- Parameters:
userAgent
-- Returns:
- this request
-
referer
public HttpRequest referer(java.lang.String referer)
Set the 'Referer' header to given value- Parameters:
referer
-- Returns:
- this request
-
useCaches
public HttpRequest useCaches(boolean useCaches)
Set value ofURLConnection.setUseCaches(boolean)
- Parameters:
useCaches
-- Returns:
- this request
-
acceptEncoding
public HttpRequest acceptEncoding(java.lang.String acceptEncoding)
Set the 'Accept-Encoding' header to given value- Parameters:
acceptEncoding
-- Returns:
- this request
-
acceptGzipEncoding
public HttpRequest acceptGzipEncoding()
Set the 'Accept-Encoding' header to 'gzip'- Returns:
- this request
- See Also:
uncompress(boolean)
-
acceptCharset
public HttpRequest acceptCharset(java.lang.String acceptCharset)
Set the 'Accept-Charset' header to given value- Parameters:
acceptCharset
-- Returns:
- this request
-
contentEncoding
public java.lang.String contentEncoding()
Get the 'Content-Encoding' header from the response- Returns:
- this request
-
server
public java.lang.String 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
public java.lang.String cacheControl()
Get the 'Cache-Control' header from the response- Returns:
- cache control
-
eTag
public java.lang.String 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
public java.lang.String location()
Get the 'Location' header from the response- Returns:
- location
-
authorization
public HttpRequest authorization(java.lang.String authorization)
Set the 'Authorization' header to given value- Parameters:
authorization
-- Returns:
- this request
-
proxyAuthorization
public HttpRequest proxyAuthorization(java.lang.String proxyAuthorization)
Set the 'Proxy-Authorization' header to given value- Parameters:
proxyAuthorization
-- Returns:
- this request
-
basic
public HttpRequest basic(java.lang.String name, java.lang.String password)
Set the 'Authorization' header to given values in Basic authentication format- Parameters:
name
-password
-- Returns:
- this request
-
proxyBasic
public HttpRequest proxyBasic(java.lang.String name, java.lang.String password)
Set the 'Proxy-Authorization' header to given values in Basic authentication format- Parameters:
name
-password
-- Returns:
- this request
-
ifModifiedSince
public HttpRequest ifModifiedSince(long ifModifiedSince)
Set the 'If-Modified-Since' request header to the given value- Parameters:
ifModifiedSince
-- Returns:
- this request
-
ifNoneMatch
public HttpRequest ifNoneMatch(java.lang.String ifNoneMatch)
Set the 'If-None-Match' request header to the given value- Parameters:
ifNoneMatch
-- Returns:
- this request
-
contentType
public HttpRequest contentType(java.lang.String contentType)
Set the 'Content-Type' request header to the given value- Parameters:
contentType
-- Returns:
- this request
-
contentType
public HttpRequest contentType(java.lang.String contentType, java.lang.String charset)
Set the 'Content-Type' request header to the given value and charset- Parameters:
contentType
-charset
-- Returns:
- this request
-
contentType
public java.lang.String 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
public HttpRequest contentLength(java.lang.String contentLength)
Set the 'Content-Length' request header to the given value- Parameters:
contentLength
-- Returns:
- this request
-
contentLength
public HttpRequest contentLength(int contentLength)
Set the 'Content-Length' request header to the given value- Parameters:
contentLength
-- Returns:
- this request
-
accept
public HttpRequest accept(java.lang.String accept)
Set the 'Accept' header to given value- Parameters:
accept
-- Returns:
- this request
-
acceptJson
public HttpRequest acceptJson()
Set the 'Accept' header to 'application/json'- Returns:
- this request
-
copy
protected HttpRequest copy(java.io.InputStream input, java.io.OutputStream output) throws java.io.IOException
Copy from input stream to output stream- Parameters:
input
-output
-- Returns:
- this request
- Throws:
java.io.IOException
-
copy
protected HttpRequest copy(java.io.Reader input, java.io.Writer output) throws java.io.IOException
Copy from reader to writer- Parameters:
input
-output
-- Returns:
- this request
- Throws:
java.io.IOException
-
progress
public HttpRequest progress(HttpRequest.UploadProgress callback)
Set the UploadProgress callback for this request- Parameters:
callback
-- Returns:
- this request
-
incrementTotalSize
private HttpRequest incrementTotalSize(long size)
-
closeOutput
protected HttpRequest closeOutput() throws java.io.IOException
Close output stream- Returns:
- this request
- Throws:
HttpRequest.HttpRequestException
java.io.IOException
-
closeOutputQuietly
protected HttpRequest closeOutputQuietly() throws HttpRequest.HttpRequestException
- Returns:
- this request
- Throws:
HttpRequest.HttpRequestException
-
openOutput
protected HttpRequest openOutput() throws java.io.IOException
Open output stream- Returns:
- this request
- Throws:
java.io.IOException
-
startPart
protected HttpRequest startPart() throws java.io.IOException
Start part of a multipart- Returns:
- this request
- Throws:
java.io.IOException
-
writePartHeader
protected HttpRequest writePartHeader(java.lang.String name, java.lang.String filename) throws java.io.IOException
Write part header- Parameters:
name
-filename
-- Returns:
- this request
- Throws:
java.io.IOException
-
writePartHeader
protected HttpRequest writePartHeader(java.lang.String name, java.lang.String filename, java.lang.String contentType) throws java.io.IOException
Write part header- Parameters:
name
-filename
-contentType
-- Returns:
- this request
- Throws:
java.io.IOException
-
part
public HttpRequest part(java.lang.String name, java.lang.String part)
Write part of a multipart request to the request body- Parameters:
name
-part
-- Returns:
- this request
-
part
public HttpRequest part(java.lang.String name, java.lang.String filename, java.lang.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(java.lang.String name, java.lang.String filename, java.lang.String contentType, java.lang.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
public HttpRequest part(java.lang.String name, java.lang.Number part) throws HttpRequest.HttpRequestException
Write part of a multipart request to the request body- Parameters:
name
-part
-- Returns:
- this request
- Throws:
HttpRequest.HttpRequestException
-
part
public HttpRequest part(java.lang.String name, java.lang.String filename, java.lang.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
public HttpRequest part(java.lang.String name, java.io.File part) throws HttpRequest.HttpRequestException
Write part of a multipart request to the request body- Parameters:
name
-part
-- Returns:
- this request
- Throws:
HttpRequest.HttpRequestException
-
part
public HttpRequest part(java.lang.String name, java.lang.String filename, java.io.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(java.lang.String name, java.lang.String filename, java.lang.String contentType, java.io.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
public HttpRequest part(java.lang.String name, java.io.InputStream part) throws HttpRequest.HttpRequestException
Write part of a multipart request to the request body- Parameters:
name
-part
-- Returns:
- this request
- Throws:
HttpRequest.HttpRequestException
-
part
public HttpRequest part(java.lang.String name, java.lang.String filename, java.lang.String contentType, java.io.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
public HttpRequest partHeader(java.lang.String name, java.lang.String value) throws HttpRequest.HttpRequestException
Write a multipart header to the response body- Parameters:
name
-value
-- Returns:
- this request
- Throws:
HttpRequest.HttpRequestException
-
send
public HttpRequest send(java.io.File input) throws HttpRequest.HttpRequestException
Write contents of file to request body- Parameters:
input
-- Returns:
- this request
- Throws:
HttpRequest.HttpRequestException
-
send
public HttpRequest send(byte[] input) throws HttpRequest.HttpRequestException
Write byte array to request body- Parameters:
input
-- Returns:
- this request
- Throws:
HttpRequest.HttpRequestException
-
send
public HttpRequest send(java.io.InputStream input) throws HttpRequest.HttpRequestException
Write stream to request bodyThe given stream will be closed once sending completes
- Parameters:
input
-- Returns:
- this request
- Throws:
HttpRequest.HttpRequestException
-
send
public HttpRequest send(java.io.Reader input) throws HttpRequest.HttpRequestException
Write reader to request bodyThe given reader will be closed once sending completes
- Parameters:
input
-- Returns:
- this request
- Throws:
HttpRequest.HttpRequestException
-
send
public HttpRequest send(java.lang.CharSequence value) throws HttpRequest.HttpRequestException
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
public java.io.OutputStreamWriter writer() throws HttpRequest.HttpRequestException
Create writer to request output stream- Returns:
- writer
- Throws:
HttpRequest.HttpRequestException
-
form
public HttpRequest form(java.util.Map<?,?> values) throws HttpRequest.HttpRequestException
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
public HttpRequest form(java.util.Map.Entry<?,?> entry) throws HttpRequest.HttpRequestException
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(java.util.Map.Entry<?,?> entry, java.lang.String charset) throws HttpRequest.HttpRequestException
Write 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
public HttpRequest form(java.lang.Object name, java.lang.Object value) throws HttpRequest.HttpRequestException
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(java.lang.Object name, java.lang.Object value, java.lang.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
public HttpRequest form(java.util.Map<?,?> values, java.lang.String charset) throws HttpRequest.HttpRequestException
Write the values in the map as encoded form data to the request body- Parameters:
values
-charset
-- Returns:
- this request
- Throws:
HttpRequest.HttpRequestException
-
trustAllCerts
public HttpRequest trustAllCerts() throws HttpRequest.HttpRequestException
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
public HttpRequest 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
public java.net.URL url()
Get theURL
of this request's connection- Returns:
- request URL
-
method
public java.lang.String method()
Get the HTTP method of this request- Returns:
- method
-
useProxy
public HttpRequest useProxy(java.lang.String proxyHost, int proxyPort)
Configure an HTTP proxy on this connection. Use {proxyBasic(String, String)
if this proxy requires basic authentication.- Parameters:
proxyHost
-proxyPort
-- Returns:
- this request
-
followRedirects
public HttpRequest followRedirects(boolean 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
-
-