1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
| // HTTP 请求建造者
class HttpRequestBuilder {
private String url;
private String method = "GET";
private Map<String, String> headers = new HashMap<>();
private Map<String, String> queryParams = new HashMap<>();
private String body;
private int timeout = 30000; // 默认30秒超时
public HttpRequestBuilder url(String url) {
this.url = url;
return this;
}
public HttpRequestBuilder get() {
this.method = "GET";
return this;
}
public HttpRequestBuilder post() {
this.method = "POST";
return this;
}
public HttpRequestBuilder put() {
this.method = "PUT";
return this;
}
public HttpRequestBuilder delete() {
this.method = "DELETE";
return this;
}
public HttpRequestBuilder header(String name, String value) {
headers.put(name, value);
return this;
}
public HttpRequestBuilder headers(Map<String, String> headers) {
this.headers.putAll(headers);
return this;
}
public HttpRequestBuilder contentType(String contentType) {
return header("Content-Type", contentType);
}
public HttpRequestBuilder authorization(String token) {
return header("Authorization", "Bearer " + token);
}
public HttpRequestBuilder param(String name, String value) {
queryParams.put(name, value);
return this;
}
public HttpRequestBuilder params(Map<String, String> params) {
queryParams.putAll(params);
return this;
}
public HttpRequestBuilder body(String body) {
this.body = body;
return this;
}
public HttpRequestBuilder jsonBody(Object obj) {
// 这里简化处理,实际应该使用JSON库
this.body = obj.toString();
return contentType("application/json");
}
public HttpRequestBuilder timeout(int timeoutMs) {
this.timeout = timeoutMs;
return this;
}
public HttpRequest build() {
if (url == null) {
throw new IllegalStateException("URL is required");
}
return new HttpRequest(url, method, headers, queryParams, body, timeout);
}
}
// HTTP 请求对象
class HttpRequest {
private final String url;
private final String method;
private final Map<String, String> headers;
private final Map<String, String> queryParams;
private final String body;
private final int timeout;
public HttpRequest(String url, String method, Map<String, String> headers,
Map<String, String> queryParams, String body, int timeout) {
this.url = url;
this.method = method;
this.headers = new HashMap<>(headers);
this.queryParams = new HashMap<>(queryParams);
this.body = body;
this.timeout = timeout;
}
public String execute() {
// 模拟HTTP请求执行
StringBuilder result = new StringBuilder();
result.append("执行HTTP请求:\n");
result.append("URL: ").append(getFullUrl()).append("\n");
result.append("Method: ").append(method).append("\n");
result.append("Headers: ").append(headers).append("\n");
if (body != null) {
result.append("Body: ").append(body).append("\n");
}
result.append("Timeout: ").append(timeout).append("ms\n");
result.append("请求执行成功!");
return result.toString();
}
private String getFullUrl() {
if (queryParams.isEmpty()) {
return url;
}
StringBuilder fullUrl = new StringBuilder(url);
fullUrl.append("?");
boolean first = true;
for (Map.Entry<String, String> param : queryParams.entrySet()) {
if (!first) {
fullUrl.append("&");
}
fullUrl.append(param.getKey()).append("=").append(param.getValue());
first = false;
}
return fullUrl.toString();
}
// Getter methods
public String getUrl() { return url; }
public String getMethod() { return method; }
public Map<String, String> getHeaders() { return new HashMap<>(headers); }
public Map<String, String> getQueryParams() { return new HashMap<>(queryParams); }
public String getBody() { return body; }
public int getTimeout() { return timeout; }
}
// 使用示例
public class HttpRequestBuilderDemo {
public static void main(String[] args) {
System.out.println("=== HTTP 请求建造者演示 ===\n");
// GET 请求
HttpRequest getRequest = new HttpRequestBuilder()
.url("https://api.example.com/users")
.get()
.param("page", "1")
.param("limit", "10")
.header("User-Agent", "MyApp/1.0")
.authorization("abc123token")
.timeout(5000)
.build();
System.out.println("GET 请求:");
System.out.println(getRequest.execute());
System.out.println("\n" + "=".repeat(50) + "\n");
// POST 请求
HttpRequest postRequest = new HttpRequestBuilder()
.url("https://api.example.com/users")
.post()
.contentType("application/json")
.authorization("abc123token")
.body("{\"name\": \"张三\", \"email\": \"zhangsan@example.com\"}")
.timeout(10000)
.build();
System.out.println("POST 请求:");
System.out.println(postRequest.execute());
System.out.println("\n" + "=".repeat(50) + "\n");
// PUT 请求
Map<String, String> commonHeaders = new HashMap<>();
commonHeaders.put("Accept", "application/json");
commonHeaders.put("Cache-Control", "no-cache");
HttpRequest putRequest = new HttpRequestBuilder()
.url("https://api.example.com/users/123")
.put()
.headers(commonHeaders)
.contentType("application/json")
.authorization("abc123token")
.body("{\"name\": \"李四\", \"email\": \"lisi@example.com\"}")
.build();
System.out.println("PUT 请求:");
System.out.println(putRequest.execute());
}
}
|