HttpRequest
, WebSocketUpgradeRequest
public interface Request
Request
represents a HTTP request, and offers a fluent interface to customize
various attributes such as the path, the headers, the content, etc.
You can create Request
objects via HttpClient.newRequest(String)
and
you can send them using either send()
for a blocking semantic, or
send(Response.CompleteListener)
for an asynchronous semantic.
Response
Modifier and Type | Interface | Description |
---|---|---|
static interface |
Request.BeginListener |
Listener for the request begin event.
|
static interface |
Request.CommitListener |
Listener for the request committed event.
|
static interface |
Request.ContentListener |
Listener for the request content event.
|
static interface |
Request.FailureListener |
Listener for the request failed event.
|
static interface |
Request.HeadersListener |
Listener for the request headers event.
|
static interface |
Request.Listener |
Listener for all request events.
|
static interface |
Request.QueuedListener |
Listener for the request queued event.
|
static interface |
Request.RequestListener |
Common, empty, super-interface for request listeners.
|
static interface |
Request.SuccessListener |
Listener for the request succeeded event.
|
Modifier and Type | Method | Description |
---|---|---|
boolean |
abort(java.lang.Throwable cause) |
Attempts to abort the send of this request.
|
Request |
accept(java.lang.String... accepts) |
|
Request |
agent(java.lang.String agent) |
|
Request |
attribute(java.lang.String name,
java.lang.Object value) |
|
Request |
content(ContentProvider content) |
|
Request |
content(ContentProvider content,
java.lang.String contentType) |
|
Request |
cookie(java.net.HttpCookie cookie) |
|
Request |
file(java.nio.file.Path file) |
Shortcut method to specify a file as a content for this request, with the default content type of
"application/octect-stream".
|
Request |
file(java.nio.file.Path file,
java.lang.String contentType) |
Shortcut method to specify a file as a content for this request, with the given content type.
|
Request |
followRedirects(boolean follow) |
|
java.lang.Throwable |
getAbortCause() |
|
java.lang.String |
getAgent() |
|
java.util.Map<java.lang.String,java.lang.Object> |
getAttributes() |
|
ContentProvider |
getContent() |
|
java.util.List<java.net.HttpCookie> |
getCookies() |
|
HttpFields |
getHeaders() |
|
java.lang.String |
getHost() |
|
long |
getIdleTimeout() |
|
java.lang.String |
getMethod() |
|
Fields |
getParams() |
|
java.lang.String |
getPath() |
|
int |
getPort() |
|
java.lang.String |
getQuery() |
|
<T extends Request.RequestListener> |
getRequestListeners(java.lang.Class<T> listenerClass) |
|
java.lang.String |
getScheme() |
|
long |
getTimeout() |
|
java.net.URI |
getURI() |
|
HttpVersion |
getVersion() |
|
Request |
header(java.lang.String name,
java.lang.String value) |
|
Request |
header(HttpHeader header,
java.lang.String value) |
Adds the given
value to the specified header . |
Request |
idleTimeout(long timeout,
java.util.concurrent.TimeUnit unit) |
|
boolean |
isFollowRedirects() |
|
Request |
listener(Request.Listener listener) |
|
Request |
method(java.lang.String method) |
|
Request |
method(HttpMethod method) |
|
Request |
onComplete(Response.CompleteListener listener) |
|
Request |
onRequestBegin(Request.BeginListener listener) |
|
Request |
onRequestCommit(Request.CommitListener listener) |
|
Request |
onRequestContent(Request.ContentListener listener) |
|
Request |
onRequestFailure(Request.FailureListener listener) |
|
Request |
onRequestHeaders(Request.HeadersListener listener) |
|
Request |
onRequestQueued(Request.QueuedListener listener) |
|
Request |
onRequestSuccess(Request.SuccessListener listener) |
|
Request |
onResponseBegin(Response.BeginListener listener) |
|
Request |
onResponseContent(Response.ContentListener listener) |
|
Request |
onResponseContentAsync(Response.AsyncContentListener listener) |
|
Request |
onResponseFailure(Response.FailureListener listener) |
|
Request |
onResponseHeader(Response.HeaderListener listener) |
|
Request |
onResponseHeaders(Response.HeadersListener listener) |
|
Request |
onResponseSuccess(Response.SuccessListener listener) |
|
Request |
param(java.lang.String name,
java.lang.String value) |
Adds a query parameter with the given name and value.
|
Request |
path(java.lang.String path) |
Specifies the path - and possibly the query - of this request.
|
Request |
scheme(java.lang.String scheme) |
|
ContentResponse |
send() |
Sends this request and returns the response.
|
void |
send(Response.CompleteListener listener) |
Sends this request and asynchronously notifies the given listener for response events.
|
Request |
timeout(long timeout,
java.util.concurrent.TimeUnit unit) |
|
Request |
version(HttpVersion version) |
java.lang.String getScheme()
Request scheme(java.lang.String scheme)
scheme
- the scheme of this request, such as "http" or "https"java.lang.String getHost()
int getPort()
java.lang.String getMethod()
Request method(HttpMethod method)
method
- the method of this request, such as GET or POSTRequest method(java.lang.String method)
method
- the method of this request, such as GET or POSTjava.lang.String getPath()
getQuery()
Request path(java.lang.String path)
UTF-8 URL encoded
.
For example, if the value for parameter "currency" is the euro symbol € then the
query string for this parameter must be "currency=%E2%82%AC".
For transparent encoding of parameter values, use param(String, String)
.path
- the path of this request, such as "/" or "/path?param=1"java.lang.String getQuery()
getPath()
,
getParams()
java.net.URI getURI()
HttpVersion getVersion()
Request version(HttpVersion version)
version
- the HTTP version of this request, such as "HTTP/1.1"Fields getParams()
Request param(java.lang.String name, java.lang.String value)
UTF-8 URL encoded
.name
- the name of the query parametervalue
- the value of the query parameterHttpFields getHeaders()
Request header(java.lang.String name, java.lang.String value)
name
- the name of the headervalue
- the value of the headerheader(HttpHeader, String)
Request header(HttpHeader header, java.lang.String value)
Adds the given value
to the specified header
.
Multiple calls with the same parameters will add multiple values;
use the value null
to remove the header completely.
header
- the header namevalue
- the value of the headerjava.util.List<java.net.HttpCookie> getCookies()
Request cookie(java.net.HttpCookie cookie)
cookie
- a cookie for this requestRequest attribute(java.lang.String name, java.lang.Object value)
name
- the name of the attributevalue
- the value of the attributejava.util.Map<java.lang.String,java.lang.Object> getAttributes()
ContentProvider getContent()
Request content(ContentProvider content)
content
- the content provider of this requestRequest content(ContentProvider content, java.lang.String contentType)
content
- the content provider of this requestcontentType
- the content typeRequest file(java.nio.file.Path file) throws java.io.IOException
file
- the file to uploadjava.io.IOException
- if the file does not exist or cannot be readRequest file(java.nio.file.Path file, java.lang.String contentType) throws java.io.IOException
file
- the file to uploadcontentType
- the content type of the filejava.io.IOException
- if the file does not exist or cannot be readjava.lang.String getAgent()
Request agent(java.lang.String agent)
agent
- the user agent for this request (corresponds to the User-Agent
header)Request accept(java.lang.String... accepts)
accepts
- the media types that are acceptable in the response, such as
"text/plain;q=0.5" or "text/html" (corresponds to the Accept
header)long getIdleTimeout()
Request idleTimeout(long timeout, java.util.concurrent.TimeUnit unit)
timeout
- the idle timeout for this requestunit
- the idle timeout unitlong getTimeout()
Request timeout(long timeout, java.util.concurrent.TimeUnit unit)
timeout
- the total timeout for the request/response conversationunit
- the timeout unitboolean isFollowRedirects()
Request followRedirects(boolean follow)
follow
- whether this request follows redirects<T extends Request.RequestListener> java.util.List<T> getRequestListeners(java.lang.Class<T> listenerClass)
T
- the type of listener classlistenerClass
- the class of the listener, or null for all listeners classesRequest listener(Request.Listener listener)
listener
- a listener for request eventsRequest onRequestQueued(Request.QueuedListener listener)
listener
- a listener for request queued eventRequest onRequestBegin(Request.BeginListener listener)
listener
- a listener for request begin eventRequest onRequestHeaders(Request.HeadersListener listener)
listener
- a listener for request headers eventRequest onRequestCommit(Request.CommitListener listener)
listener
- a listener for request commit eventRequest onRequestContent(Request.ContentListener listener)
listener
- a listener for request content eventsRequest onRequestSuccess(Request.SuccessListener listener)
listener
- a listener for request success eventRequest onRequestFailure(Request.FailureListener listener)
listener
- a listener for request failure eventRequest onResponseBegin(Response.BeginListener listener)
listener
- a listener for response begin eventRequest onResponseHeader(Response.HeaderListener listener)
listener
- a listener for response header eventRequest onResponseHeaders(Response.HeadersListener listener)
listener
- a listener for response headers eventRequest onResponseContent(Response.ContentListener listener)
listener
- a consuming listener for response content eventsRequest onResponseContentAsync(Response.AsyncContentListener listener)
listener
- an asynchronous listener for response content eventsRequest onResponseSuccess(Response.SuccessListener listener)
listener
- a listener for response success eventRequest onResponseFailure(Response.FailureListener listener)
listener
- a listener for response failure eventRequest onComplete(Response.CompleteListener listener)
listener
- a listener for complete eventContentResponse send() throws java.lang.InterruptedException, java.util.concurrent.TimeoutException, java.util.concurrent.ExecutionException
This method should be used when a simple blocking semantic is needed, and when it is known that the response content can be buffered without exceeding memory constraints.
For example, this method is not appropriate to download big files from a server; consider using
send(Response.CompleteListener)
instead, passing your own Response.Listener
or a utility
listener such as InputStreamResponseListener
.
The method returns when the complete event
is fired.
ContentResponse
for this requestjava.lang.InterruptedException
- if send thread is interruptedjava.util.concurrent.TimeoutException
- if send times outjava.util.concurrent.ExecutionException
- if execution failsResponse.CompleteListener.onComplete(Result)
void send(Response.CompleteListener listener)
Sends this request and asynchronously notifies the given listener for response events.
This method should be used when the application needs to be notified of the various response events as they happen, or when the application needs to efficiently manage the response content.
The listener passed to this method may implement not only Response.CompleteListener
but also other response listener interfaces, and all the events implemented will be notified.
This allows application code to write a single listener class to handle all relevant events.
listener
- the listener that receives response eventsboolean abort(java.lang.Throwable cause)
cause
- the abort cause, must not be nulljava.lang.Throwable getAbortCause()
abort(Throwable)
,
or null if this request has not been abortedCopyright © 1995–2018 Webtide. All rights reserved.