Java HTTP Client
An HTTP client is the program that starts an HTTP conversation by sending a request. The HttpClient class builds requests, sends them, and returns each answer as a response object.
A client asks for the /hello path
The client takes the port the server printed as its first argument and builds the /hello address with it. Run against that server, the program prints 200 and then the body Hello from the server.
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Main {
public static void main(String[] args) throws Exception {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://127.0.0.1:" + args[0] + "/hello"))
.GET()
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.statusCode());
System.out.println(response.body());
}
}The same shape asks another path
Only the path changes in this program, which asks the two-context server from the last lesson for its /bye path. That server answers with status 200 and the body Goodbye, and the program prints both values.
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Main {
public static void main(String[] args) throws Exception {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://127.0.0.1:" + args[0] + "/bye"))
.GET()
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.statusCode());
System.out.println(response.body());
}
}Example
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.concurrent.CompletableFuture;
public class Main {
public static void main(String[] args) throws Exception {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://127.0.0.1:" + args[0] + "/hello"))
.GET()
.build();
CompletableFuture<HttpResponse<String>> future = client.sendAsync(request, HttpResponse.BodyHandlers.ofString());
HttpResponse<String> response = future.join();
System.out.println(response.statusCode());
System.out.println(response.body());
}
}Key points
- A GET request asks a server for a body at one address.
- The send method waits for the answer and returns it as one response object.
- A response object carries the status code and the body as separate parts.
- BodyHandlers.ofString reads the answer body as a String.
- sendAsync hands back a future, and join on that future waits for the response.
In this example, sendAsync starts the /hello request and returns a CompletableFuture at once. The join call waits on that future and gives back the response, whose status is 200 and whose body is the greeting from the server of the last lesson.
Note
The /hello server from the last lesson answers with status 200 and the body Hello from the server.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What does the join call on the future from sendAsync give the program?