1 of 7

Client Web API Access

CS 240 – Advanced Software Construction

2 of 7

HTTP Request Diagram

Server

Client App

Web

HTTP Request

HTTP Response

3 of 7

Client connects to Server

  • Client establishes a network connection with the server
  • A connection allows the client to send bytes to the server and vice versa

Server

Client App

bytes

bytes

4 of 7

Connecting to a Server with the HttpClient Class

  • Java’s HttpClient class can be used by clients to make HTTP requests of a server and receive HTTP responses from the server

  • HTTP GET Example
    • GetExample.java

  • HTTP POST Example
    • PostExample.java

5 of 7

HTTP GET Request/Response Steps

  1. Client: Build request (create/set URI, set read timeout, set headers, set request method to GET
  2. Client: Send request (specify BodyHandler)

  1. Server: Mapped HTTP handler function is called with context object
  2. Server: Process request and return response

  1. Client: Get Response
  2. Cient: Process response (use response code, body, and headers as needed)

6 of 7

HTTP POST Request/Response Steps

  • Client: Build request (create/set URI, set read timeout, set headers, set request method to POST with BodyPublisher)
  • Client: Send request (specify BodyHandler)

  • Server: Mapped HTTP handler function is called with context object
  • Server: Process request and return response (including reading request body JSON and converting to request Object)

  • Client: Get Response
  • Cient: Process response (use response code, body, and headers as needed)

7 of 7

Citations

Diagrams created by Ken Rodham