Make A Synchronous GET Request in HTTP 2 Client in Java 9


Configured to run the program in Eclipse IDE: Select the java file containing the main function. Right click and select Run As/Run Configuration. Select Arguments and copy code: –add-modules jdk.incubator.httpclient to VM arguments Textbox

package pmk.learnjava9withrealapps.http2_client;

import java.net.URI;
import jdk.incubator.http.HttpClient;
import jdk.incubator.http.HttpRequest;
import jdk.incubator.http.HttpResponse;

public class Main {

	public static void main(String[] args) {

		try {
			HttpClient httpClient = HttpClient.newHttpClient();
			HttpRequest httpRequest = HttpRequest.newBuilder()
										.uri(new URI("http://localhost:8080/myserver/api/demo/helloworld"))
										.GET()
										.build();
			HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandler.asString());
			System.out.println("Status Code: " + httpResponse.statusCode());
			System.out.println("Content: " + httpResponse.body());
		} catch (Exception e) {
			System.err.println("Error: " + e.getMessage());
		}

	}

}




Status Code: 200
Content: Hello World