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

| Field | Value |
|-------|-------|
| Fully Qualified Name | `io.github.biezhi.java11.http.Example` |
| Layer | Utility (Static example method in a demonstration library) |
| Module | `http` (Package: `io.github.biezhi.java11.http`) |

## 1. Role

### Example.proxy()

This method demonstrates how to configure and use an HTTP client with a proxy server in Java 11's `java.net.http` API. The method constructs an `HttpClient` instance pre-configured with a manual proxy selector pointing to a local proxy at `127.0.0.1:1080` (commonly used for SOCKS or HTTP proxy setups such as Shadowsocks or Privoxy). It then creates a `HttpRequest` targeting Google's homepage via HTTPS using the GET method, sends the request through the proxied client, and outputs both the HTTP status code and response body to standard output. The method serves as a standalone code example — illustrating the proxy configuration workflow within the `java11-http` example library by Biezhi. Its role in the larger system is educational: it is a static utility method intended to be referenced by developers learning how to route HTTP traffic through a proxy in modern Java. There are no conditional branches; the method executes a linear sequence of client setup, request construction, and response handling.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["proxy()"])
    CLIENT_BUILDER["HttpClient.newBuilder()"]
    PROXY_CONFIG["ProxySelector.of(InetSocketAddress localhost:1080)"]
    CLIENT_BUILD["client.build() with proxy config"]
    REQUEST_NEW_BUILDER["HttpRequest.newBuilder()"]
    URI_BUILD["uri(new URI(https://www.google.com))"]
    HTTP_METHOD[".GET()"]
    REQUEST_BUILD["request.build()"]
    CLIENT_SEND["client.send(request, BodyHandlers.ofString())"]
    STATUS_PRINT["System.out.println(response.statusCode())"]
    BODY_PRINT["System.out.println(response.body())"]
    END_NODE(["Return / Next"])

    START --> CLIENT_BUILDER
    CLIENT_BUILDER --> PROXY_CONFIG
    PROXY_CONFIG --> CLIENT_BUILD
    CLIENT_BUILD --> REQUEST_NEW_BUILDER
    REQUEST_NEW_BUILDER --> URI_BUILD
    URI_BUILD --> HTTP_METHOD
    HTTP_METHOD --> REQUEST_BUILD
    REQUEST_BUILD --> CLIENT_SEND
    CLIENT_SEND --> STATUS_PRINT
    STATUS_PRINT --> BODY_PRINT
    BODY_PRINT --> END_NODE
```

**Constant Resolution:**

| Constant | Resolved Value | Business Meaning |
|----------|---------------|-----------------|
| Proxy host | `"127.0.0.1"` | Local loopback address — proxy runs on the same machine |
| Proxy port | `1080` | Standard port for SOCKS/HTTP proxy (e.g., Shadowsocks, Privoxy, Dante) |
| Target URI | `"https://www.google.com"` | Google homepage — example endpoint for the HTTP GET request |

**Processing description:**
1. **Client Builder**: A new `HttpClient` builder is instantiated via `HttpClient.newBuilder()`, which provides a mutable configuration chain.
2. **Proxy Configuration**: The builder is instructed to use a manual proxy by calling `.proxy(ProxySelector.of(...))`, which wraps an `InetSocketAddress` for localhost:1080. This ensures all requests from this client will be routed through the specified proxy.
3. **Client Creation**: The client is built with `.build()`, producing an immutable `HttpClient` instance bound to the proxy configuration.
4. **Request Builder**: A new `HttpRequest` builder is created via `HttpRequest.newBuilder()`, providing a mutable chain for request configuration.
5. **URI Setup**: The request URI is set to `https://www.google.com` via `new URI(...)`.
6. **HTTP Method**: The `.GET()` method configures the request to use the HTTP GET verb (which sends no body).
7. **Request Construction**: `.build()` produces an immutable `HttpRequest`.
8. **Send Request**: The request is sent synchronously via `client.send(...)` with `HttpResponse.BodyHandlers.ofString()` as the body handler, which collects the response body as a `String`.
9. **Print Status**: The HTTP status code (e.g., 200, 301, 500) is printed to stdout.
10. **Print Body**: The full response body (HTML content of the Google homepage) is printed to stdout.

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| - | (none) | - | This method takes no parameters. It uses hardcoded values for the proxy address and target URI. |

**External state / instance fields read:** None — this is a fully self-contained static method with no dependencies on instance fields or external state.

## 4. CRUD Operations / Called Services

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

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

This method performs no traditional CRUD (Create/Read/Update/Delete) database operations. It makes an outbound HTTP network call.

| Type | Component | Description |
|------|-----------|-------------|
| NETWORK_OUT | `HttpClient.send()` | Synchronously sends an HTTP GET request to `https://www.google.com` through proxy `127.0.0.1:1080`. Returns an `HttpResponse<String>` containing the remote server's status code and body. |

## 5. Dependency Trace

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| - | (none) | - | - |

No callers found. This is a standalone example method in the `java11-http` demonstration library, not integrated into any production application flow.

## 6. Per-Branch Detail Blocks

> This method contains no conditional branches (if/else, switch, loops). The execution is strictly linear.

**Block 1** — [SEQUENCE] `(no branching)` (L119)

> Establish the HTTP client with proxy configuration.

| # | Type | Code |
|---|------|------|
| 1 | SET | `HttpClient client = HttpClient.newBuilder()` // Create a mutable HTTP client builder |
| 2 | EXEC | `.proxy(ProxySelector.of(new InetSocketAddress("127.0.0.1", 1080)))` // Configure proxy — localhost:1080 |
| 3 | EXEC | `.build()` // Produce immutable HttpClient |

**Block 2** — [SEQUENCE] `(no branching)` (L123)

> Construct the HTTP GET request for Google's homepage.

| # | Type | Code |
|---|------|------|
| 1 | SET | `HttpRequest request = HttpRequest.newBuilder()` // Create a mutable HTTP request builder |
| 2 | EXEC | `.uri(new URI("https://www.google.com"))` // Set target URI |
| 3 | EXEC | `.GET()` // Set HTTP method to GET |
| 4 | EXEC | `.build()` // Produce immutable HttpRequest |

**Block 3** — [SEQUENCE] `(no branching)` (L127)

> Send the request and print the response.

| # | Type | Code |
|---|------|------|
| 1 | SET | `HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString())` // Async-free sync call through proxy |
| 2 | EXEC | `System.out.println(response.statusCode())` // Print HTTP status code |
| 3 | EXEC | `System.out.println(response.body())` // Print response body (HTML) |
| 4 | RETURN | (implicit `void`) |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `HttpClient` | Class | Java 11 built-in HTTP client — an immutable, thread-safe client for sending HTTP requests and handling responses |
| `HttpRequest` | Class | Java 11 built-in request object — an immutable container for HTTP request data (URI, headers, body, method) |
| `ProxySelector` | Class | Java 11 built-in proxy selector — determines which proxy (or direct connection) to use for a given URI |
| `InetSocketAddress` | Class | Java socket address container — holds a hostname (IP) and port number for network connections |
| `BodyHandlers.ofString()` | Method | Response body handler — collects the full HTTP response body as a UTF-8 `String` |
| `BodyHandlers.discarding()` | Method | Response body handler (used in `basicAuth()` adjacent method) — discards the response body to save memory when body content is not needed |
| localhost | Network term | The loopback network interface (`127.0.0.1`) — refers to the local machine itself |
| 1080 | Port | Standard port for SOCKS/HTTP proxies (e.g., Shadowsocks, Privoxy, Dante, polipo) |
| Google homepage | URI | `https://www.google.com` — example HTTPS endpoint used as a simple test target for the HTTP client |
| `java11-http` | Library | Biezhi's example library demonstrating Java 11 HTTP client features including proxy, basic auth, WebSocket, and compression |

---

*Document generated from source: `src/main/java/io/github/biezhi/java11/http/Example.java`, lines 117-130*
