An HTTP client is used to create an Endpoint
representing an HTTP server and on which client-to-server HTTP exchanges are initiated.
The endpoint(java.net.InetSocketAddress)
method creates endpoints bound to a the address of an HTTP server. HTTP client exchanges (request/response) are initiated from the endpoint thus
obtained.
The following code show how to send a request to an HTTP server:
HttpClient httpClient = ...;
Endpoint endpoint = httpClient.endpoint("example.com". 80).build();
String response = endpoint.exchange(Method.GET, "/")
.flatMap(Exchange::response)
.flatMapMany(response -> response.body().string().stream())
.reduceWith(() -> new StringBuilder(), (acc, chunk) -> acc.append(chunk))
.map(StringBuilder::toString)
.block();
endpoint.close().block();
UnboundExchange
are created created from the client, they must be explicitly bound to an actual endpoint before they can be sent, so UnboundExchange.bind(Endpoint)
must be invoked
before publishers returned by Exchange.response()
or Exchange.webSocket()
are subscribed.
- Since:
- 1.6
- Author:
- Jeremy Kuhn
-
Nested Class Summary
Modifier and TypeInterfaceDescriptionstatic interface
HttpClient.EndpointBuilder<A extends ExchangeContext,
B extends Exchange<A>, C extends InterceptedExchange<A>> A builder ofEndpoint
. -
Method Summary
Modifier and TypeMethodDescriptiondefault <A extends ExchangeContext>
HttpClient.EndpointBuilder<A, Exchange<A>, InterceptedExchange<A>> Creates an endpoint builder to create anEndpoint
bound to the specified host and port.<A extends ExchangeContext>
HttpClient.EndpointBuilder<A, Exchange<A>, InterceptedExchange<A>> endpoint
(InetSocketAddress remoteAddress) Creates an endpoint builder to create an Endpoint bound to specified server address.default <T extends ExchangeContext>
Mono<? extends UnboundExchange<T>> exchange()
Creates an unbound HTTP exchange.default <T extends ExchangeContext>
Mono<? extends UnboundExchange<T>> Creates an unbound HTTP exchange.<T extends ExchangeContext>
Mono<? extends UnboundExchange<T>> Creates an unbound HTTP exchange with a context.default <T extends ExchangeContext>
Mono<? extends UnboundExchange<T>> Creates an unbound HTTP exchange.default <T extends ExchangeContext>
Mono<? extends UnboundExchange<T>> Creates an unbound HTTP exchange.default <T extends ExchangeContext>
Mono<? extends UnboundExchange<T>> exchange
(T context) Creates an unbound HTTP exchange.
-
Method Details
-
exchange
Creates an unbound HTTP exchange.
This method is a shortcut for
exchange(Method.GET, "/", null)
.- Returns:
- an HTTP exchange mono
-
exchange
Creates an unbound HTTP exchange.
This method is a shortcut for
exchange(Method.GET, "/", context)
.- Parameters:
context
- the exchange context- Returns:
- an HTTP exchange mono
-
exchange
default <T extends ExchangeContext> Mono<? extends UnboundExchange<T>> exchange(String requestTarget) Creates an unbound HTTP exchange.
This method is a shortcut for
exchange(Method.GET, requestTarget, null)
.- Parameters:
requestTarget
- the request target path- Returns:
- an HTTP exchange mono
-
exchange
default <T extends ExchangeContext> Mono<? extends UnboundExchange<T>> exchange(String requestTarget, T context) Creates an unbound HTTP exchange.
This method is a shortcut for
exchange(Method.GET, requestTarget, context)
.- Parameters:
requestTarget
- the request target pathcontext
- the exchange context- Returns:
- an HTTP exchange mono
-
exchange
default <T extends ExchangeContext> Mono<? extends UnboundExchange<T>> exchange(Method method, String requestTarget) Creates an unbound HTTP exchange.
This method is a shortcut for
exchange(method, requestTarget, null)
.- Parameters:
method
- the HTTP methodrequestTarget
- the request target path- Returns:
- an HTTP exchange mono
-
exchange
<T extends ExchangeContext> Mono<? extends UnboundExchange<T>> exchange(Method method, String requestTarget, T context) throws IllegalArgumentException Creates an unbound HTTP exchange with a context.
- Parameters:
method
- the HTTP methodrequestTarget
- the request target pathcontext
- the exchange context- Returns:
- an HTTP exchange mono
- Throws:
IllegalArgumentException
- if the specified request target is invalid
-
endpoint
default <A extends ExchangeContext> HttpClient.EndpointBuilder<A,Exchange<A>, endpointInterceptedExchange<A>> (String host, int port) Creates an endpoint builder to create an
Endpoint
bound to the specified host and port.- Type Parameters:
A
- the exchange context type- Parameters:
host
- the host of the HTTP serverport
- the port of the HTTP server- Returns:
- an endpoint builder
-
endpoint
<A extends ExchangeContext> HttpClient.EndpointBuilder<A,Exchange<A>, endpointInterceptedExchange<A>> (InetSocketAddress remoteAddress) Creates an endpoint builder to create an Endpoint bound to specified server address.
- Type Parameters:
A
- the exchange context type- Parameters:
remoteAddress
- the address of the HTTP server- Returns:
- an endpoint builder
-