Java Reference
In-Depth Information
Building and Invoking Requests
Once you have a
WebTarget
that represents the exact URI you want to invoke on, you can
begin building and invoking HTTP requests through one of its
request()
methods:
public
public interface
interface
WebTarget
WebTarget
extends
extends
Configurable
<
WebTarget
> {
...
public
public
Invocation
.
Builder
request
();
public
public
Invocation
.
Builder
request
(
String
...
acceptedResponseTypes
);
public
public
Invocation
.
Builder
request
(
MediaType
...
acceptedResponseTypes
);
}
The
Invocation.Builder
interface hierarchy is a bit convoluted, so I'll explain how to
build requests using examples and code fragments:
package
package
javax
.
ws
.
rs
.
client
;
public
public interface
interface
Invocation
Invocation
{
...
public
public interface
interface
Builder
Builder
extends
extends
SyncInvoker
,
Configurable
<
Builder
> {
...
public
public
Builder
accept
(
String
...
types
);
public
public
Builder
accept
(
MediaType
...
types
public
public
Builder
acceptLanguage
(
Locale
...
locales
);
public
public
Builder
acceptLanguage
(
String
...
locales
);
public
public
Builder
acceptEncoding
(
String
...
encodings
);
public
public
Builder
cookie
(
Cookie cookie
);
public
public
Builder
cookie
(
String name
,
String value
);
public
public
Builder
cacheControl
(
CacheControl cacheControl
);
public
public
Builder
header
(
String name
,
Object value
);
public
public
Builder
headers
(
MultivaluedMap
<
String
,
Object
>
headers
);
}
}
Invocation.Builder
has a bunch of methods that allow you to set different types of request
cookie()
methods allow you to set HTTP cookies you want to return to the server. And then
there are the more generic
header()
and
headers()
methods that cover the more esoteric
HTTP headers and any custom ones your application might have.
After setting the headers the request requires, you can then invoke a specific HTTP method
to get back a response from the server. GET requests have two flavors: