---

# (DD05) Business Logic — Example.proxy() [14 LOC]

| Field | Value |
|-------|-------|
| Fully Qualified Name | `io.github.biezhi.java11.http.Example` |
| Layer | Utility (Java 11 HttpClient feature demonstration) |
| Module | `http` (Package: `io.github.biezhi.java11.http`) |

## 1. Role

### Example.proxy()

This method demonstrates how to configure a Java 11 `HttpClient` with a custom HTTP proxy (127.0.0.1:1080, typical for local proxy tools such as Charles, Fiddler, or Polipo), then issues an HTTP GET request to Google's homepage and outputs the response. It serves as an educational example within the `java11` feature showcase project (maintained by biezhi on GitHub) that illustrates the Java 11 HTTP Client API's built-in proxy support — a previously external dependency in earlier JDK versions. The method implements the **builder pattern** through chained `HttpClient.newBuilder()` calls and the **delegate pattern** by offloading network I/O to `HttpClient.send()`. It is a standalone utility example with no production callers; its role is purely instructional, showing developers how to wire a proxy into the standard library HTTP client introduced in JDK 11 via `java.net.http.HttpClient`. The Chinese comment preceding it ("设置代理", meaning "Set proxy") confirms its purpose as a configuration-and-execute demonstration.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["proxy()"])
    CONFIG["Build HttpClient with proxy 127.0.0.1:1080"]
    BUILD_REQ["Build HttpRequest to https://www.google.com via newBuilder chain"]
    SEND["Send request (GET) with BodyHandlers.ofString()"]
    PRINT_STATUS["Print response statusCode"]
    PRINT_BODY["Print response body"]
    END_NODE(["Return / Next"])

    START --> CONFIG
    CONFIG --> BUILD_REQ
    BUILD_REQ --> SEND
    SEND --> PRINT_STATUS
    PRINT_STATUS --> PRINT_BODY
    PRINT_BODY --> END_NODE
```

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| - | (none) | - | - |

The method has no parameters. All configuration values — the proxy address (`127.0.0.1:1080`) and target URI (`https://www.google.com`) — are **hardcoded literals** within the method body, making this a demonstration that requires no external input.

**External state accessed:**
| # | External Reference | Type | Business Description |
|---|-------------------|------|---------------------|
| 1 | `HttpClient` (JDK) | Class | Java 11 built-in HTTP client (`java.net.http.HttpClient`) — replaces the deprecated `HttpURLConnection` |
| 2 | `HttpRequest` (JDK) | Class | Java 11 HTTP request builder (`java.net.http.HttpRequest`) with fluent chain-based construction |
| 3 | `ProxySelector` (JDK) | Class | Java proxy selector (`java.net.ProxySelector`) used to configure the proxy host for outgoing connections |
| 4 | `InetSocketAddress` (JDK) | Class | Java socket address (`java.net.InetSocketAddress`) encoding the proxy endpoint (host + port) |
| 5 | `HttpResponse` (JDK) | Class | Java 11 HTTP response wrapper (`java.net.http.HttpResponse`) carrying status code and body |
| 6 | `System.out` (JDK) | Object | Standard output stream used for printing the status code and response body |

## 4. CRUD Operations / Called Services

### Pre-computed evidence from code analysis graph:

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| - | `Example.proxy` | - | - | Calls `proxy` in `Example` |

This method does **not** interact with any database, service component (SC), or business system. It is a pure HTTP client demonstration using only JDK standard library classes. The method calls are confined to the Java 11 `java.net.http` package:

| # | Called Method | Category | Description |
|---|--------------|----------|-------------|
| 1 | `HttpClient.newBuilder()` | JDK API | Creates a builder instance for configuring an HTTP client |
| 2 | `.proxy(ProxySelector.of(...))` | JDK API | Configures the client to route requests through the specified proxy server |
| 3 | `.build()` | JDK API | Finalizes the HttpClient builder and returns a configured client instance |
| 4 | `HttpRequest.newBuilder()` | JDK API | Creates a builder instance for constructing an HTTP request |
| 5 | `.uri(new URI(...))` | JDK API | Sets the target URI (https://www.google.com) on the request builder |
| 6 | `.GET()` | JDK API | Sets the HTTP method to GET on the request builder |
| 7 | `.build()` | JDK API | Finalizes the HttpRequest builder and returns an immutable request |
| 8 | `client.send(request, BodyHandlers.ofString())` | JDK API | Synchronously sends the HTTP request and collects the body as a String |
| 9 | `System.out.println(...)` | JDK API | Prints the HTTP status code to standard output |
| 10 | `System.out.println(...)` | JDK API | Prints the response body (HTML content) to standard output |

## 5. Dependency Trace

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | N/A | `Example.proxy` | N/A (standalone example, no callers) |

This method is not called by any other class in the codebase. It is a standalone static method used as a self-contained Java 11 HttpClient API demonstration. No caller trace chain exists — the method is invoked directly (e.g., from a `main` method or test harness) by developers wishing to experiment with the proxy configuration feature.

## 6. Per-Branch Detail Blocks

**Block 1** — [EXEC] (L118)

> Constructs a configured HttpClient with a proxy server.

| # | Type | Code |
|---|------|------|
| 1 | SET | `HttpClient client = HttpClient.newBuilder().proxy(ProxySelector.of(new InetSocketAddress("127.0.0.1", 1080))).build()` | Build HttpClient builder chain: set proxy host `127.0.0.1` on port `1080` (standard local proxy port), then build the configured client |

**Block 2** — [EXEC] (L123)

> Constructs an HTTP GET request to Google's homepage.

| # | Type | Code |
|---|------|------|
| 1 | SET | `HttpRequest request = HttpRequest.newBuilder().uri(new URI("https://www.google.com")).GET().build()` | Build HttpRequest: set URI to `https://www.google.com`, set method to GET, then build the immutable request |
| 2 | EXEC | `.uri(new URI("https://www.google.com"))` | Set the target HTTP endpoint |
| 3 | EXEC | `.GET()` | Set HTTP method to GET |

**Block 3** — [EXEC] (L128)

> Sends the request synchronously and collects the response body as a String.

| # | Type | Code |
|---|------|------|
| 1 | SET | `HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString())` | Synchronously execute the HTTP request via the proxy-configured client; body handler decodes the response body as a String |
| 2 | EXEC | `client.send(request, BodyHandlers.ofString())` | Delegate to JDK HttpClient for actual network I/O — request travels through the 127.0.0.1:1080 proxy |

**Block 4** — [EXEC] (L129–130)

> Outputs the HTTP response status code and body to standard output.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `System.out.println(response.statusCode())` | Print the HTTP status code (e.g., 200, 301, 404) |
| 2 | EXEC | `System.out.println(response.body())` | Print the full HTML response body returned by Google |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| HTTP Client (JDK 11) | Technology | The built-in `java.net.http.HttpClient` introduced in Java 11 as a replacement for the legacy `HttpURLConnection`. Provides asynchronous/synchronous request support, automatic HTTP/2, and proxy configuration. |
| Proxy | Technical | An intermediary server that forwards HTTP requests on behalf of the client. Commonly used for debugging (Charles, Fiddler), caching, or access control. Here configured as `127.0.0.1:1080` (localhost). |
| `ProxySelector` | JDK Class | Java class (`java.net.ProxySelector`) that determines which proxy to use for a given URL. The static factory `ProxySelector.of()` creates a selector from a single address. |
| `BodyHandlers` | JDK Class | Java HTTP Client response body handler that determines how the response payload is collected. `ofString()` collects the body into a String. |
| `HttpRequest.newBuilder()` | JDK API | Builder factory for constructing immutable HTTP requests with fluent method chaining (uri, method, headers, build). |
| `HttpClient.newBuilder()` | JDK API | Builder factory for configuring HTTP clients (proxy, authenticator, followRedirects, version, etc.) before building. |
| 设置代理 (Chinese comment) | Language | "Set proxy" — describes the purpose of configuring the HTTP client to route through a proxy server. |
| biezhi/java11 | Repository | Open-source GitHub project by Wei Wang (biezhi) showcasing Java 11 new features through runnable code examples. |

---