public interface HttpClient
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();
- Since:
- 1.6
- Author:
- Jeremy Kuhn
-
Nested Class Summary
Modifier and TypeInterfaceDescriptionstatic interface
HttpClient.EndpointBuilder<A extends ExchangeContext,
B extends Exchange<A>, C extends InterceptableExchange<A>> A builder ofEndpoint
. -
Method Summary
Modifier and TypeMethodDescriptiondefault <A extends ExchangeContext>
HttpClient.EndpointBuilder<A, Exchange<A>, InterceptableExchange<A>> Creates an endpoint builder to create anEndpoint
bound to the specified host and port.<A extends ExchangeContext>
HttpClient.EndpointBuilder<A, Exchange<A>, InterceptableExchange<A>> endpoint
(InetSocketAddress remoteAddress) Creates an endpoint builder to create an Endpoint bound to specified server address.
-
Method Details
-
endpoint
default <A extends ExchangeContext> HttpClient.EndpointBuilder<A,Exchange<A>, endpointInterceptableExchange<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>, endpointInterceptableExchange<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
-