- java.lang.Object
-
- java.net.http.HttpRequest
-
public abstract class HttpRequest extends Object
An HTTP request.An
HttpRequestinstance is built through anHttpRequestbuilder. AnHttpRequestbuilder is obtained from one of thenewBuildermethods. A request'sURI, headers, and body can be set. Request bodies are provided through aBodyPublishersupplied to one of thePOST,PUTormethodmethods. Once all required parameters have been set in the builder,buildwill return theHttpRequest. Builders can be copied and modified many times in order to build multiple related requests that differ in some parameters.The following is an example of a GET request that prints the response body as a String:
HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder() .uri(URI.create("http://foo.com/")) .build(); client.sendAsync(request, BodyHandlers.ofString()) .thenApply(HttpResponse::body) .thenAccept(System.out::println) .join();The class
BodyPublishersprovides implementations of many common publishers. Alternatively, a customBodyPublisherimplementation can be used.- Since:
- 11
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceHttpRequest.BodyPublisherABodyPublisherconverts high-level Java objects into a flow of byte buffers suitable for sending as a request body.static classHttpRequest.BodyPublishersImplementations ofBodyPublisherthat implement various useful publishers, such as publishing the request body from a String, or from a file.static interfaceHttpRequest.BuilderA builder of HTTP requests.
-
Constructor Summary
Constructors Modifier Constructor Description protectedHttpRequest()Creates an HttpRequest.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract Optional<HttpRequest.BodyPublisher>bodyPublisher()Returns anOptionalcontaining theHttpRequest.BodyPublisherset on this request.booleanequals(Object obj)Tests this HTTP request instance for equality with the given object.abstract booleanexpectContinue()Returns this request's expect continue setting.inthashCode()Computes a hash code for this HTTP request instance.abstract HttpHeadersheaders()The (user-accessible) request headers that this request was (or will be) sent with.abstract Stringmethod()Returns the request method for this request.static HttpRequest.BuildernewBuilder()Creates anHttpRequestbuilder.static HttpRequest.BuildernewBuilder(URI uri)Creates anHttpRequestbuilder with the given URI.abstract Optional<Duration>timeout()Returns anOptionalcontaining this request's timeout duration.abstract URIuri()Returns this request'sURI.abstract Optional<HttpClient.Version>version()Returns anOptionalcontaining the HTTP protocol version that will be requested for thisHttpRequest.
-
-
-
Method Detail
-
newBuilder
public static HttpRequest.Builder newBuilder(URI uri)
Creates anHttpRequestbuilder with the given URI.- Parameters:
uri- the request URI- Returns:
- a new request builder
- Throws:
IllegalArgumentException- if the URI scheme is not supported.
-
newBuilder
public static HttpRequest.Builder newBuilder()
Creates anHttpRequestbuilder.- Returns:
- a new request builder
-
bodyPublisher
public abstract Optional<HttpRequest.BodyPublisher> bodyPublisher()
Returns anOptionalcontaining theHttpRequest.BodyPublisherset on this request. If noBodyPublisherwas set in the requests's builder, then theOptionalis empty.- Returns:
- an
Optionalcontaining this request'sBodyPublisher
-
method
public abstract String method()
Returns the request method for this request. If not set explicitly, the default method for any request is "GET".- Returns:
- this request's method
-
timeout
public abstract Optional<Duration> timeout()
Returns anOptionalcontaining this request's timeout duration. If the timeout duration was not set in the request's builder, then theOptionalis empty.- Returns:
- an
Optionalcontaining this request's timeout duration
-
expectContinue
public abstract boolean expectContinue()
Returns this request's expect continue setting.- Returns:
- this request's expect continue setting
-
uri
public abstract URI uri()
Returns this request'sURI.- Returns:
- this request's URI
-
version
public abstract Optional<HttpClient.Version> version()
Returns anOptionalcontaining the HTTP protocol version that will be requested for thisHttpRequest. If the version was not set in the request's builder, then theOptionalis empty. In that case, the version requested will be that of the sendingHttpClient. The correspondingHttpResponseshould be queried to determine the version that was actually used.- Returns:
- HTTP protocol version
-
headers
public abstract HttpHeaders headers()
The (user-accessible) request headers that this request was (or will be) sent with.- Returns:
- this request's HttpHeaders
-
equals
public final boolean equals(Object obj)
Tests this HTTP request instance for equality with the given object.If the given object is not an
HttpRequestthen this method returnsfalse. Two HTTP requests are equal if their URI, method, and headers fields are all equal.This method satisfies the general contract of the
Object.equalsmethod.- Overrides:
equalsin classObject- Parameters:
obj- the object to which this object is to be compared- Returns:
trueif, and only if, the given object is anHttpRequestthat is equal to this HTTP request- See Also:
Object.hashCode(),HashMap
-
hashCode
public final int hashCode()
Computes a hash code for this HTTP request instance.The hash code is based upon the HTTP request's URI, method, and header components, and satisfies the general contract of the
Object.hashCodemethod.- Overrides:
hashCodein classObject- Returns:
- the hash-code value for this HTTP request
- See Also:
Object.equals(java.lang.Object),System.identityHashCode(java.lang.Object)
-
-