public abstract static class HttpRequest.Builder extends Object
HttpRequest
s. HttpRequest.Builder
s are
created by calling HttpRequest.newBuilder(URI)
or HttpRequest.newBuilder()
.
Each of the setter methods in this class modifies the state of the builder and returns this (ie. the same instance). The methods are not synchronized and should not be called from multiple threads without external synchronization.
Note, that not all request headers may be set by user code. Some are restricted for security reasons and others such as the headers relating to authentication, redirection and cookie management are managed by specific APIs rather than through directly user set headers.
The build method returns a new
HttpRequest
each time it is invoked.
Modifier | Constructor | Description |
---|---|---|
protected |
Builder() |
Creates a Builder.
|
Modifier and Type | Method | Description |
---|---|---|
abstract HttpRequest |
build() |
Builds and returns a
HttpRequest . |
abstract HttpRequest.Builder |
copy() |
Returns an exact duplicate copy of this
Builder based on current
state. |
abstract HttpRequest.Builder |
DELETE(HttpRequest.BodyProcessor body) |
Sets the request method of this builder to DELETE and sets its
request body processor to the given value.
|
abstract HttpRequest.Builder |
expectContinue(boolean enable) |
Request server to acknowledge request before sending request
body.
|
abstract HttpRequest.Builder |
GET() |
Sets the request method of this builder to GET.
|
abstract HttpRequest.Builder |
header(String name,
String value) |
Adds the given name value pair to the set of headers for this request.
|
abstract HttpRequest.Builder |
headers(String... headers) |
Adds the given name value pairs to the set of headers for this
request.
|
abstract HttpRequest.Builder |
method(String method,
HttpRequest.BodyProcessor body) |
Sets the request method and request body of this builder to the
given values.
|
abstract HttpRequest.Builder |
POST(HttpRequest.BodyProcessor body) |
Sets the request method of this builder to POST and sets its
request body processor to the given value.
|
abstract HttpRequest.Builder |
PUT(HttpRequest.BodyProcessor body) |
Sets the request method of this builder to PUT and sets its
request body processor to the given value.
|
abstract HttpRequest.Builder |
setHeader(String name,
String value) |
Sets the given name value pair to the set of headers for this
request.
|
abstract HttpRequest.Builder |
timeout(Duration duration) |
Sets a timeout for this request.
|
abstract HttpRequest.Builder |
uri(URI uri) |
Sets this
HttpRequest 's request URI . |
abstract HttpRequest.Builder |
version(HttpClient.Version version) |
Overrides the
HttpClient.version() setting for this
request. |
public abstract HttpRequest.Builder uri(URI uri)
HttpRequest
's request URI
.uri
- the request URIIllegalArgumentException
- if the URI
scheme is not
supported.public abstract HttpRequest.Builder expectContinue(boolean enable)
100 Continue
response before the client
sends the request body. This means the request processor for the
request will not be invoked until this interim response is received.enable
- true
if Expect continue to be sentpublic abstract HttpRequest.Builder version(HttpClient.Version version)
HttpClient.version()
setting for this
request. This sets the version requested. The corresponding
HttpResponse
should be checked for the version that was
used.version
- the HTTP protocol version requestedpublic abstract HttpRequest.Builder header(String name, String value)
name
- the header namevalue
- the header valuepublic abstract HttpRequest.Builder headers(String... headers)
String
s must alternate as names and values.headers
- the list of String name value pairsIllegalArgumentException
- if there is an odd number of
parameterspublic abstract HttpRequest.Builder timeout(Duration duration)
HttpTimeoutException
is
thrown from HttpClient::send
or
HttpClient::sendAsync
completes exceptionally with a HttpTimeoutException
. The effect
of not setting a timeout is the same as setting an infinite Duration, ie.
block forever.duration
- public abstract HttpRequest.Builder setHeader(String name, String value)
name
- the header namevalue
- the header valuepublic abstract HttpRequest.Builder GET()
HttpRequest
public abstract HttpRequest.Builder POST(HttpRequest.BodyProcessor body)
body
- the body processorHttpRequest
public abstract HttpRequest.Builder PUT(HttpRequest.BodyProcessor body)
body
- the body processorHttpRequest
public abstract HttpRequest.Builder DELETE(HttpRequest.BodyProcessor body)
body
- the body processorHttpRequest
public abstract HttpRequest.Builder method(String method, HttpRequest.BodyProcessor body)
body
- the body processormethod
- the method to useHttpRequest
IllegalArgumentException
- if an unrecognized method is usedpublic abstract HttpRequest build()
HttpRequest
.public abstract HttpRequest.Builder copy()
Builder
based on current
state. The new builder can then be modified independently of this
builder. Submit a bug or feature
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 2015, 2017, Oracle and/or its affiliates. 500 Oracle Parkway
Redwood Shores, CA 94065 USA. All rights reserved.
DRAFT 9-Ubuntu+0-9b151-2