# 错误：net/http：请求被取消（等待响应头时超过客户端超时）

- **ID:** `go/goroutine-leak-http-request`
- **领域:** go
- **类别:** network_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

未带上下文或超时的 HTTP 客户端调用无限阻塞，导致 goroutine 泄漏，最终被客户端超时取消。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.18 | active | — | — |
| 1.21 | active | — | — |
| 1.22 | active | — | — |

## 解决方案

1. **** (95% 成功率)
   ```
   ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
resp, err := http.DefaultClient.Do(req)
   ```
2. **** (90% 成功率)
   ```
   client := &http.Client{Timeout: 5 * time.Second}
resp, err := client.Get(url)
   ```

## 无效尝试

- **** — Leaks the goroutine longer; the root cause is missing cancellation. (70% 失败率)
- **** — http.Get uses DefaultClient with no timeout; can block forever. (90% 失败率)
