16
16
import java .security .KeyStore ;
17
17
import java .security .SecureRandom ;
18
18
import java .security .cert .X509Certificate ;
19
- import java .text .MessageFormat ;
20
19
import java .util .Arrays ;
21
20
import java .util .List ;
22
21
import java .util .Map ;
@@ -61,8 +60,8 @@ public static Result<String> get(String url, Map<String, String> headers) throws
61
60
* <p>
62
61
* 此方法构造一个带有参数的URL并执行GET请求如果参数为空,则执行不带参数的GET请求
63
62
*
64
- * @param url 请求的URL
65
- * @param params 请求的参数,格式为键值对
63
+ * @param url 请求的URL
64
+ * @param params 请求的参数,格式为键值对
66
65
* @param headers 请求的头信息,格式为键值对
67
66
* @param timeOut 请求的超时时间
68
67
* @return 返回请求的结果
@@ -92,11 +91,11 @@ public static Result<String> get(String url, Map<String, String> headers, TimeOu
92
91
return buildResult (okHttpClient , request );
93
92
}
94
93
95
- public static Result <String > postForm (String url , String params ) throws IOException {
94
+ public static < T > Result <String > postForm (String url , T params ) throws IOException {
96
95
return postForm (url , params , null );
97
96
}
98
97
99
- public static Result <String > postForm (String url , String params , Map <String , String > headers ) throws IOException {
98
+ public static < T > Result <String > postForm (String url , T params , Map <String , String > headers ) throws IOException {
100
99
return postForm (url , params , headers , null );
101
100
}
102
101
@@ -106,22 +105,22 @@ public static Result<String> postForm(String url, String params, Map<String, Str
106
105
* 本方法专注于处理Form类型的POST请求,通过提供的URL、请求参数、自定义请求头和超时设置,
107
106
* 来执行网络请求,并返回请求的结果。使用较低级别的HTTP API来实现自定义请求的需求。
108
107
*
109
- * @param url 请求的URL,指定了资源的位置
110
- * @param params 请求参数,以字符串形式提交,通常为键值对的形式
108
+ * @param url 请求的URL,指定了资源的位置
109
+ * @param params 请求参数,以字符串形式提交,通常为键值对的形式
111
110
* @param headers 请求头信息,包含HTTP请求头部字段和对应的值,可为空
112
111
* @param timeOut 请求的超时设置,决定了请求各阶段的最大等待时间
113
112
* @return 返回请求的结果,包括状态码、响应头和响应体等信息
114
113
* @throws IOException 如果请求过程中发生I/O错误,如网络中断等
115
114
*/
116
- public static Result <String > postForm (String url , String params , Map <String , String > headers , TimeOut timeOut ) throws IOException {
115
+ public static < T > Result <String > postForm (String url , T params , Map <String , String > headers , TimeOut timeOut ) throws IOException {
117
116
return post (url , params , headers , timeOut , Form_TYPE );
118
117
}
119
118
120
- public static Result <String > postJson (String url , String params ) throws IOException {
119
+ public static < T > Result <String > postJson (String url , T params ) throws IOException {
121
120
return postJson (url , params , null );
122
121
}
123
122
124
- public static Result <String > postJson (String url , String params , Map <String , String > headers ) throws IOException {
123
+ public static < T > Result <String > postJson (String url , T params , Map <String , String > headers ) throws IOException {
125
124
return postJson (url , params , headers , null );
126
125
}
127
126
@@ -130,13 +129,13 @@ public static Result<String> postJson(String url, String params, Map<String, Str
130
129
* 此方法用于发送带有JSON数据的HTTP POST请求到指定的URL
131
130
* 它允许自定义请求头和超时设置,以满足不同的网络请求需求
132
131
*
133
- * @param url 请求的URL地址,指定资源位置
134
- * @param params 请求的JSON参数,以字符串形式表示
132
+ * @param url 请求的URL地址,指定资源位置
133
+ * @param params 请求的JSON参数,以字符串形式表示
135
134
* @param headers 请求头信息,包含请求的附加信息,如认证信息、接受类型等
136
135
* @param timeOut 连接和读取超时设置,用于控制网络请求的响应时间
137
136
* @return 返回包含响应结果的Result对象,响应体以字符串形式存储
138
137
*/
139
- public static Result <String > postJson (String url , String params , Map <String , String > headers , TimeOut timeOut ) throws IOException {
138
+ public static < T > Result <String > postJson (String url , T params , Map <String , String > headers , TimeOut timeOut ) throws IOException {
140
139
return post (url , params , headers , timeOut , JSON_TYPE );
141
140
}
142
141
@@ -145,17 +144,23 @@ public static Result<String> postJson(String url, String params, Map<String, Str
145
144
* 该方法负责发起HTTP POST请求,根据提供的参数构建请求并发送
146
145
* 主要用于需要向服务器提交数据的场景
147
146
*
148
- * @param url 请求的URL,表示服务器的地址
149
- * @param params 请求的参数,通常为JSON字符串或其他格式的数据
150
- * @param headers 请求头部,包含一些附加信息如认证信息、内容类型等
151
- * @param timeOut 请求的超时时间设置
147
+ * @param url 请求的URL,表示服务器的地址
148
+ * @param params 请求的参数,通常为JSON字符串或其他格式的数据
149
+ * @param headers 请求头部,包含一些附加信息如认证信息、内容类型等
150
+ * @param timeOut 请求的超时时间设置
152
151
* @param mediaType 请求体的媒体类型,通常指明发送的数据格式(如"application/json")
153
152
* @return 返回请求的结果,包含响应的所有信息
154
153
* @throws IOException 如果请求过程中发生错误,可能会抛出此异常
155
154
*/
156
- public static Result <String > post (String url , String params , Map <String , String > headers , TimeOut timeOut , MediaType mediaType ) throws IOException {
155
+ public static < T > Result <String > post (String url , T params , Map <String , String > headers , TimeOut timeOut , MediaType mediaType ) throws IOException {
157
156
log .info ("请求url:{},请求入参:{},请求头部:{}" , url , params , headers );
158
- RequestBody requestBody = RequestBody .create (params , mediaType );
157
+ RequestBody requestBody ;
158
+ // 判断请求参数类型
159
+ if (params instanceof String ) {
160
+ requestBody = RequestBody .create ((String ) params , mediaType );
161
+ } else {
162
+ requestBody = RequestBody .create (JSON .toJSONString (params ), mediaType );
163
+ }
159
164
Request request = buildRequest (url , headers , requestBody , RequestMethod .POST );
160
165
OkHttpClient okHttpClient = buildOkHttpClient (url , timeOut );
161
166
return buildResult (okHttpClient , request );
@@ -192,8 +197,8 @@ public static Result<String> upload(String url, File file, Map<String, String> h
192
197
/**
193
198
* 文件上传接口
194
199
*
195
- * @param url 文件上传的URL,指定文件将被上传到的服务器地址
196
- * @param file 待上传的文件,通过File对象指定
200
+ * @param url 文件上传的URL,指定文件将被上传到的服务器地址
201
+ * @param file 待上传的文件,通过File对象指定
197
202
* @param headers 请求头信息,用于设置HTTP请求的头字段
198
203
* @param timeOut 超时设置,控制请求的读写超时时间
199
204
* @return 返回一个Result对象,包含上传结果和一个字符串消息
@@ -213,7 +218,7 @@ public static Result<String> upload(String url, File file, Map<String, String> h
213
218
* 构建httpClient
214
219
* 根据给定的URL和超时设置构建一个OkHttpClient实例,支持HTTPS连接和自定义超时设置
215
220
*
216
- * @param url 要请求的URL,用于确定使用HTTP还是HTTPS
221
+ * @param url 要请求的URL,用于确定使用HTTP还是HTTPS
217
222
* @param timeOut 超时设置,用于设置连接、读取和写入的超时时间
218
223
* @return 构建好的OkHttpClient实例
219
224
*/
@@ -234,10 +239,10 @@ private static OkHttpClient buildOkHttpClient(String url, TimeOut timeOut) {
234
239
/**
235
240
* 构建请求
236
241
*
237
- * @param url 请求的URL地址
238
- * @param headers 请求头信息,为键值对形式的Map
242
+ * @param url 请求的URL地址
243
+ * @param headers 请求头信息,为键值对形式的Map
239
244
* @param requestBody 请求体内容,根据不同请求方法包含不同的数据
240
- * @param method 请求方法,包括GET、POST、PUT、PATCH、DELETE等
245
+ * @param method 请求方法,包括GET、POST、PUT、PATCH、DELETE等
241
246
* @return 返回构建完成的Request对象
242
247
*/
243
248
private static Request buildRequest (String url , Map <String , String > headers , RequestBody requestBody , RequestMethod method ) {
@@ -277,15 +282,14 @@ private static Request buildRequest(String url, Map<String, String> headers, Req
277
282
*/
278
283
private static Result <String > buildResult (OkHttpClient okHttpClient , Request request ) throws IOException {
279
284
try (Response response = okHttpClient .newCall (request ).execute ()) {
280
- if (! response .isSuccessful ()) {
281
- throw new MyRuntimeException ( MessageFormat . format ( "错误:OkHttp请求异常。请求:{0} 结果:{1}"
282
- , request . body (), response .body () ));
285
+ log . info ( "请求执行完成,响应状态码:{},响应信息:{}" , response . code (), response .message ());
286
+ if ( response . body () != null ) {
287
+ return Result . buildResult ( response . body (). string () , response .code (), response . message ( ));
283
288
}
284
- assert response .body () != null ;
285
- return Result .buildResult (response .body ().string (), response .code (), response .message ());
289
+ return Result .buildResult (null , response .code (), response .message ());
286
290
} catch (IOException e ) {
287
291
log .error ("错误:OkHttp请求异常" , e );
288
- throw e ;
292
+ throw new MyRuntimeException ( e ) ;
289
293
}
290
294
}
291
295
0 commit comments