Class HttpClientDemo

java.lang.Object
modernfeatures.java11.HttpClientDemo

public class HttpClientDemo extends Object
Demonstrates usage of Java 11's HttpClient for HTTP requests.

This example covers:

  • Synchronous HTTP GET requests using HttpClient.send()
  • Asynchronous HTTP GET requests using HttpClient.sendAsync()

The class shows how to:

  • Build and send a synchronous request, and process the response.
  • Build and send an asynchronous request, register a callback to handle the response, and ensure the program waits for the async operation to complete.
 Usage:
   java modernfeatures.java11.HttpClientDemo
 
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    main(String[] args)
    Main method demonstrating: How to perform a synchronous HTTP GET request and print the response. How to perform an asynchronous HTTP GET request, register a callback for the response, and wait for the asynchronous operation to finish.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • HttpClientDemo

      public HttpClientDemo()
  • Method Details

    • main

      public static void main(String[] args) throws Exception
      Main method demonstrating:
      • How to perform a synchronous HTTP GET request and print the response.
      • How to perform an asynchronous HTTP GET request, register a callback for the response, and wait for the asynchronous operation to finish.
      Parameters:
      args - command-line arguments (not used)
      Throws:
      Exception - if an error occurs during HTTP operations