-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpClientExecutorExample.java
More file actions
158 lines (137 loc) · 5.98 KB
/
HttpClientExecutorExample.java
File metadata and controls
158 lines (137 loc) · 5.98 KB
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
package com.github.mrbox.easyhttp.example;
import com.github.mrbox.easyhttp.client.apache.PoolingHttpClientExecutor;
import com.github.mrbox.easyhttp.config.HttpConfig;
import com.github.mrbox.easyhttp.core.HttpRequest;
import com.github.mrbox.easyhttp.core.HttpResponse;
import com.github.mrbox.easyhttp.core.MultipartBody;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
public class HttpClientExecutorExample {
private static final PoolingHttpClientExecutor executor = new PoolingHttpClientExecutor();
public static void main(String[] args) throws Exception {
// GET 请求示例
getExample();
// POST JSON 请求示例
postJsonExample();
// POST form 表单请求示例
postFormExample();
// 文件上传示例
multipartUploadExample();
// 配置代理示例
//proxyConfigExample();
// 不要忘记关闭 executor 释放资源
executor.close();
}
/**
* 发送 GET 请求示例
*/
private static void getExample() {
HttpRequest request = HttpRequest.get("http://httpbin.org/get")
.queryParam("param1", "value1") // 逐个设置
.queryParams(new HashMap<String, String>(){{ // 批量设置
put("param2", "value2");
put("param3", "value3");
}})
.header("User-Agent", "EasyHttp-Client")
.build();
try {
HttpResponse response = executor.execute(request);
System.out.println("GET Response Status: " + response.getStatusCode());
System.out.println("GET Response Body: " + response.getBodyAsString(StandardCharsets.UTF_8));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 发送 POST 请求 (JSON) 示例
*/
private static void postJsonExample() {
String jsonBody = "{\"name\": \"EasyHttp\", \"version\": \"1.0\"}";
HttpRequest request = HttpRequest.post("http://httpbin.org/post")
.body(jsonBody)
.header("Content-Type", "application/json") // 可不设置,默认使用 application/json
.build();
try {
HttpResponse response = executor.execute(request);
System.out.println("POST JSON Response Status: " + response.getStatusCode());
System.out.println("POST JSON Response Body: " + response.getBodyAsString(StandardCharsets.UTF_8));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 提交表单数据 (FORM) 示例
*/
private static void postFormExample() {
HttpRequest request = HttpRequest.post("http://httpbin.org/post")
.formParam("param1", "value1")
.formParams(new HashMap<String, String>(){{
put("param2", "value2");
put("param3", "value3");
}})
.build();
try {
HttpResponse response = executor.execute(request);
System.out.println("POST FORM Response Status: " + response.getStatusCode());
System.out.println("POST FORM Response Body: " + response.getBodyAsString(StandardCharsets.UTF_8));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 文件上传 (Multipart) 示例
*/
private static void multipartUploadExample() {
// 创建临时文件用于上传测试
File tempFile = null;
try {
// 创建临时文件
tempFile = File.createTempFile("easyhttp-test-", ".txt");
// 写入一些测试内容
Files.write(tempFile.toPath(), "This is a test file for multipart upload.".getBytes(StandardCharsets.UTF_8));
MultipartBody multipartBody = MultipartBody.builder()
.addTextPart("description", "File upload test")
.addFilePart("file", tempFile)
.build();
HttpRequest request = HttpRequest.post("http://httpbin.org/post")
.multipartBody(multipartBody)
.build();
HttpResponse response = executor.execute(request);
System.out.println("Multipart Upload Response Status: " + response.getStatusCode());
System.out.println("Multipart Upload Response Body: " + response.getBodyAsString(StandardCharsets.UTF_8));
} catch (Exception e) {
e.printStackTrace();
} finally {
// 清理临时文件
if (tempFile != null && tempFile.exists()) {
if (!tempFile.delete()) {
System.out.println("Warning: Could not delete temporary file: " + tempFile.getAbsolutePath());
}
}
}
}
/**
* 配置代理示例
*/
private static void proxyConfigExample() {
// 创建 HttpConfig 并设置代理
HttpConfig proxyConfig = HttpConfig.builder()
.proxyConfig(new HttpConfig.ProxyConfig("127.0.0.1", 7897)) // 替换为你的代理主机
.build();
// 使用配置创建新的 executor 实例
// 注意:如果你需要频繁切换代理,应该管理多个 executor 实例或者在每次请求时重新配置
try (PoolingHttpClientExecutor proxyExecutor = new PoolingHttpClientExecutor(proxyConfig)) {
HttpRequest request = HttpRequest.get("http://httpbin.org/ip")
.build();
HttpResponse response = proxyExecutor.execute(request);
System.out.println("Proxy GET Response Status: " + response.getStatusCode());
System.out.println("Proxy GET Response Body: " + response.getBodyAsString(StandardCharsets.UTF_8));
} catch (Exception e) {
System.out.println("Proxy configuration example failed, please check proxy settings.");
e.printStackTrace();
}
}
}