RequestUtil.java
34.9 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
package cn.fw.freya.utils;
import cn.fw.freya.common.Callback;
import cn.fw.freya.utils.http.*;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.*;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.*;
import org.apache.http.client.utils.DateUtils;
import org.apache.http.config.Lookup;
import org.apache.http.cookie.Cookie;
import org.apache.http.cookie.CookieOrigin;
import org.apache.http.cookie.CookieSpecProvider;
import org.apache.http.cookie.MalformedCookieException;
import org.apache.http.impl.client.*;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.impl.cookie.DefaultCookieSpec;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import java.io.*;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.Collectors;
/**
* 使用HttpClient模拟发送(http/https)请求
*
* @author kurisu
*/
@Slf4j
public class RequestUtil {
private static HttpClient client4HTTP;// 默认采用的http协议的HttpClient对象
private static HttpClient client4HTTPS;//默认采用的https协议的HttpClient对象
static {
try {
Lookup<CookieSpecProvider> cookieLookup = CookieSpecRegistries
.createDefaultBuilder()
.register("CustomCookieSpec", context -> new CustomCookieSpec())
.build();
client4HTTP = HCB.custom().setDefaultCookieSpecRegistry(cookieLookup).build();
client4HTTPS = HCB.custom().setDefaultCookieSpecRegistry(cookieLookup).build();
} catch (Exception e) {
log.error("创建https协议的HttpClient对象出错:", e);
}
}
/**
* 判定是否开启连接池、及url是http还是https <br>
* 如果已开启连接池,则自动调用build方法,从连接池中获取client对象<br>
* 否则,直接返回相应的默认client对象<br>
*
* @param config 请求参数配置
*/
private static void create(HttpConfig config) {
if (config.client() == null) {
if (config.url().toLowerCase().startsWith("https://")) {// 如果为空,设为默认client对象
config.client(client4HTTPS);
} else {
config.client(client4HTTP);
}
}
}
/**
* 以Get方式,请求资源或服务
*
* @param client client对象
* @param url 资源地址
* @param headers 请求头信息
* @param context http上下文,用于cookie操作
* @param encoding 编码
* @return 返回处理结果
*/
public static String get(HttpClient client, String url, Header[] headers, HttpContext context, String encoding) {
return get(HttpConfig
.custom()
.client(client)
.url(url)
.headers(headers)
.context(context)
.encoding(encoding));
}
/**
* 以Get方式,请求资源或服务
*
* @param config 请求参数配置
* @return 返回结果
*/
public static String get(HttpConfig config) {
return send(config.method(HttpMethods.GET));
}
/**
* 以Post方式,请求资源或服务
*
* @param client client对象
* @param url 资源地址
* @param headers 请求头信息
* @param parasMap 请求参数
* @param context http上下文,用于cookie操作
* @param encoding 编码
* @return 返回处理结果
*/
public static String post(HttpClient client, String url, Header[] headers, Map<String, Object> parasMap, HttpContext context, String encoding) {
return post(HttpConfig.custom().client(client).url(url).headers(headers).map(parasMap).context(context).encoding(encoding));
}
/**
* 以Post方式,请求资源或服务
*
* @param config 请求参数配置
* @return 返回处理结果
*/
public static String post(HttpConfig config) {
return send(config.method(HttpMethods.POST));
}
/**
* 以Put方式,请求资源或服务
*
* @param client client对象
* @param url 资源地址
* @param parasMap 请求参数
* @param headers 请求头信息
* @param context http上下文,用于cookie操作
* @param encoding 编码
* @return 返回处理结果
*/
public static String put(HttpClient client, String url, Map<String, Object> parasMap, Header[] headers, HttpContext context, String encoding) {
return put(HttpConfig.custom().client(client).url(url).headers(headers).map(parasMap).context(context).encoding(encoding));
}
/**
* 以Put方式,请求资源或服务
*
* @param config 请求参数配置
* @return 返回处理结果
*/
public static String put(HttpConfig config) {
return send(config.method(HttpMethods.PUT));
}
/**
* 以Delete方式,请求资源或服务
*
* @param client client对象
* @param url 资源地址
* @param headers 请求头信息
* @param context http上下文,用于cookie操作
* @param encoding 编码
* @return 返回处理结果
*/
public static String delete(HttpClient client, String url, Header[] headers, HttpContext context, String encoding) {
return delete(HttpConfig.custom().client(client).url(url).headers(headers).context(context).encoding(encoding));
}
/**
* 以Delete方式,请求资源或服务
*
* @param config 请求参数配置
* @return 返回处理结果
*/
public static String delete(HttpConfig config) {
return send(config.method(HttpMethods.DELETE));
}
/**
* 以Patch方式,请求资源或服务
*
* @param client client对象
* @param url 资源地址
* @param parasMap 请求参数
* @param headers 请求头信息
* @param context http上下文,用于cookie操作
* @param encoding 编码
* @return 返回处理结果
*/
public static String patch(HttpClient client, String url, Map<String, Object> parasMap, Header[] headers, HttpContext context, String encoding) {
return patch(HttpConfig.custom().client(client).url(url).headers(headers).map(parasMap).context(context).encoding(encoding));
}
/**
* 以Patch方式,请求资源或服务
*
* @param config 请求参数配置
* @return 返回处理结果
*/
public static String patch(HttpConfig config) {
return send(config.method(HttpMethods.PATCH));
}
/**
* 以Head方式,请求资源或服务
*
* @param client client对象
* @param url 资源地址
* @param headers 请求头信息
* @param context http上下文,用于cookie操作
* @param encoding 编码
* @return 返回处理结果
*/
public static String head(HttpClient client, String url, Header[] headers, HttpContext context, String encoding) {
return head(HttpConfig.custom().client(client).url(url).headers(headers).context(context).encoding(encoding));
}
/**
* 以Head方式,请求资源或服务
*
* @param config 请求参数配置
* @return 返回处理结果
*/
public static String head(HttpConfig config) {
return send(config.method(HttpMethods.HEAD));
}
/**
* 以Options方式,请求资源或服务
*
* @param client client对象
* @param url 资源地址
* @param headers 请求头信息
* @param context http上下文,用于cookie操作
* @param encoding 编码
* @return 返回处理结果
*/
public static String options(HttpClient client, String url, Header[] headers, HttpContext context, String encoding) {
return options(HttpConfig.custom().client(client).url(url).headers(headers).context(context).encoding(encoding));
}
/**
* 以Options方式,请求资源或服务
*
* @param config 请求参数配置
* @return 返回处理结果
*/
public static String options(HttpConfig config) {
return send(config.method(HttpMethods.OPTIONS));
}
/**
* 以Trace方式,请求资源或服务
*
* @param client client对象
* @param url 资源地址
* @param headers 请求头信息
* @param context http上下文,用于cookie操作
* @param encoding 编码
* @return 返回处理结果
*/
public static String trace(HttpClient client, String url, Header[] headers, HttpContext context, String encoding) {
return trace(HttpConfig.custom().client(client).url(url).headers(headers).context(context).encoding(encoding));
}
/**
* 以Trace方式,请求资源或服务
*
* @param config 请求参数配置
* @return 返回处理结果
*/
public static String trace(HttpConfig config) {
return send(config.method(HttpMethods.TRACE));
}
/**
* 下载文件
*
* @param client client对象
* @param url 资源地址
* @param headers 请求头信息
* @param context http上下文,用于cookie操作
* @param out 输出流
* @return 返回处理结果
*/
public static OutputStream down(HttpClient client, String url, Header[] headers, HttpContext context, OutputStream out) {
return down(HttpConfig.custom().client(client).url(url).headers(headers).context(context).out(out));
}
/**
* 下载文件
*
* @param config 请求参数配置
* @return 返回处理结果
*/
public static OutputStream down(HttpConfig config) {
if (config.method() == null) {
config.method(HttpMethods.GET);
}
return fmt2Stream(execute(config), config.out(), () -> config.map().clear());
}
/**
* 上传文件
*
* @param client client对象
* @param url 资源地址
* @param headers 请求头信息
* @param context http上下文,用于cookie操作
* @return 返回处理结果
*/
public static String upload(HttpClient client, String url, Header[] headers, HttpContext context) {
return upload(HttpConfig.custom().client(client).url(url).headers(headers).context(context));
}
/**
* 上传文件
*
* @param config 请求参数配置
* @return 返回处理结果
*/
public static String upload(HttpConfig config) {
if (config.method() != HttpMethods.POST && config.method() != HttpMethods.PUT) {
config.method(HttpMethods.POST);
}
return send(config);
}
/**
* 查看资源链接情况,返回状态码
*
* @param client client对象
* @param url 资源地址
* @param headers 请求头信息
* @param context http上下文,用于cookie操作
* @return 返回处理结果
*/
public static int status(HttpClient client, String url, Header[] headers, HttpContext context, HttpMethods method) {
return status(HttpConfig.custom().client(client).url(url).headers(headers).context(context).method(method));
}
/**
* 查看资源链接情况,返回状态码
*
* @param config 请求参数配置
* @return 返回处理结果
*/
public static int status(HttpConfig config) {
return fmt2Int(execute(config));
}
/**
* 请求资源或服务
*
* @param config 请求参数配置
* @return 返回处理结果
*/
public static String send(HttpConfig config) {
return fmt2String(execute(config), config.outenc(), () -> config.map().clear());
}
/**
* 请求资源或服务,返回HttpResult对象
*
* @param config 请求参数配置
* @return 返回HttpResult处理结果
*/
@Nullable
public static HttpResult sendAndGetResp(HttpConfig config) {
Header[] reqHeaders = config.headers();
//执行结果
HttpResponse resp = execute(config);
if (Objects.isNull(resp)) {
return null;
}
HttpResult result = new HttpResult(resp);
result.setResult(fmt2String(resp, config.outenc(), () -> config.map().clear()));
result.setReqHeaders(reqHeaders);
return result;
}
/**
* 请求资源或服务
*
* @param config 请求参数配置
* @return 返回HttpResponse对象
*/
@Nullable
private static HttpResponse execute(@NonNull HttpConfig config) {
create(config);//获取链接
HttpResponse resp = null;
try {
//创建请求对象
HttpRequestBase request = getRequest(config.url(), config.method());
//设置超时
request.setConfig(config.requestConfig());
//设置header信息
request.setHeaders(config.headers());
//判断是否支持设置entity(仅HttpPost、HttpPut、HttpPatch支持)
if (HttpEntityEnclosingRequestBase.class.isAssignableFrom(request.getClass())) {
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
if (Objects.equals(request.getClass(), HttpGet.class)) {
//检测url中是否存在参数
//注:只有get请求,才自动截取url中的参数,post等其他方式,不再截取
config.url(Utils.checkHasParas(config.url(), nvps, config.inenc()));
}
//装填参数
HttpEntity entity = Utils.map2HttpEntity(nvps, config.map(), config.inenc());
//设置参数到请求对象中
((HttpEntityEnclosingRequestBase) request).setEntity(entity);
log.info("请求地址:" + config.url());
if (nvps.size() > 0) {
log.info("请求参数:" + nvps.toString());
}
if (config.json() != null) {
log.info("请求参数:" + config.json());
}
} else {
int idx = config.url().indexOf("?");
log.info("请求地址:" + config.url().substring(0, (idx > 0 ? idx : config.url().length())));
if (idx > 0) {
log.info("请求参数:" + config.url().substring(idx + 1));
}
}
//执行请求操作,并拿到结果(同步阻塞)
HttpContext context = config.context();
if (context == null) {
resp = config.client().execute(request);
} else {
resp = config.client().execute(request, context);
}
if (config.isReturnRespHeaders()) {
//获取所有response的header信息
config.headers(resp.getAllHeaders());
}
//获取结果实体
return resp;
} catch (IOException e) {
log.error("请求失败:", e);
return null;
}
}
/**
* 转化为字符串
*
* @param resp 响应对象
* @param encoding 编码
* @return 返回处理结果
*/
private static String fmt2String(HttpResponse resp, String encoding, Callback cb) {
String body = "";
try {
if (Objects.isNull(resp)) {
return body;
}
if (resp.getEntity() != null) {
// 按指定编码转换结果实体为String类型
body = EntityUtils.toString(resp.getEntity(), encoding);
} else {//有可能是head请求
body = resp.getStatusLine().toString();
}
EntityUtils.consume(resp.getEntity());
} catch (IOException e) {
log.error("解析返回值失败", e);
} finally {
close(resp);
cb.call();
}
return body;
}
/**
* 转化为数字
*
* @param resp 响应对象
* @return 返回处理结果
*/
private static int fmt2Int(HttpResponse resp) {
int statusCode = -1;
try {
if (Objects.isNull(resp)) {
return statusCode;
}
statusCode = resp.getStatusLine().getStatusCode();
EntityUtils.consume(resp.getEntity());
} catch (IOException e) {
log.error("解析返回值失败", e);
} finally {
close(resp);
}
return statusCode;
}
/**
* 转化为流
*
* @param resp 响应对象
* @param out 输出流
* @return 返回输出流
*/
public static OutputStream fmt2Stream(HttpResponse resp, OutputStream out, Callback cb) {
try {
if (Objects.isNull(resp)) {
return out;
}
resp.getEntity().writeTo(out);
EntityUtils.consume(resp.getEntity());
} catch (IOException e) {
log.error("解析返回值失败", e);
} finally {
close(resp);
cb.call();
}
return out;
}
/**
* 根据请求方法名,获取request对象
*
* @param url 资源地址
* @param method 请求方式
* @return 返回Http处理request基类
*/
private static HttpRequestBase getRequest(String url, HttpMethods method) {
HttpRequestBase request;
switch (method.getCode()) {
case 0:
// HttpGet
request = new HttpGet(url);
break;
case 2:
// HttpHead
request = new HttpHead(url);
break;
case 3:
// HttpPut
request = new HttpPut(url);
break;
case 4:
// HttpDelete
request = new HttpDelete(url);
break;
case 5:
// HttpTrace
request = new HttpTrace(url);
break;
case 6:
// HttpPatch
request = new HttpPatch(url);
break;
case 7:
// HttpOptions
request = new HttpOptions(url);
break;
default:
request = new HttpPost(url);
break;
}
return request;
}
/**
* 尝试关闭response
*
* @param resp HttpResponse对象
*/
private static void close(HttpResponse resp) {
try {
if (resp == null) {
return;
}
//如果CloseableHttpResponse 是resp的父类,则支持关闭
if (CloseableHttpResponse.class.isAssignableFrom(resp.getClass())) {
((CloseableHttpResponse) resp).close();
}
} catch (IOException e) {
log.error("尝试关闭response失败", e);
}
}
static class CustomCookieSpec extends DefaultCookieSpec {
@Override
public List<Cookie> parse(Header header, CookieOrigin cookieOrigin) throws MalformedCookieException {
String value = header.getValue();
final String prefix = "expires=";
if (value.contains(prefix)) {
String expires = value.substring(value.indexOf(prefix) + prefix.length());
expires = expires.substring(0, expires.indexOf(";"));
final String date = DateUtils.formatDate(DateUtils.parseDate(expires), "EEE, dd-MMM-yy HH:mm:ss z");
String[] cookieList = value.split(";");
value = Arrays.stream(cookieList).map(r -> r.contains(prefix) ? prefix + date : r).collect(Collectors.joining(";"));
}
header = new BasicHeader(header.getName(), value);
List<Cookie> cookieList = super.parse(header, cookieOrigin);
for (Cookie cookie : cookieList) {
((BasicClientCookie) cookie).setAttribute(BasicClientCookie.PATH_ATTR, cookie.getPath());
((BasicClientCookie) cookie).setAttribute(BasicClientCookie.DOMAIN_ATTR, cookie.getDomain());
}
return cookieList;
}
}
/**
* 发送Post请求,参数拼接到url
*
* @param httpUrl 请求路径
* @param map 请求参数
* @return
*/
public static String sendPost(String httpUrl, Map<String, String> map) {
HttpURLConnection connection = null;
InputStream is = null;
OutputStream os = null;
BufferedReader br = null;
String result = null;
try {
StringBuilder param = new StringBuilder();
boolean isFirstPar = true;
for (String key : map.keySet()) {
if (!isFirstPar) {
param.append("&");
}
isFirstPar = false;
String val = URLEncoder.encode(map.get(key), StandardCharsets.UTF_8);
param.append(String.format("%1$s=%2$s", key, val));
}
URL url = new URL(httpUrl);
connection = (HttpURLConnection) url.openConnection();// 通过远程url连接对象打开连接
connection.setRequestMethod("POST");// 设置连接请求方式
connection.setConnectTimeout(3000);// 设置连接主机服务器超时时间
connection.setReadTimeout(3000);// 设置读取主机服务器返回数据超时时间
connection.setDoOutput(true);// 默认值为:false,当向远程服务器传送数据/写数据时,需要设置为true
connection.setDoInput(true);// 默认值为:true,当前向远程服务读取数据时,设置为true,该参数可有可无
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");// 设置传入参数的格式: 请求参数应该是 name1=value1&name2=value2 的形式。
os = connection.getOutputStream();// 通过连接对象获取一个输出流
os.write(param.toString().getBytes());// 通过输出流对象将参数写出去/传输出去,它是通过字节数组写出的
if (connection.getResponseCode() == 200) {// 通过连接对象获取一个输入流,向远程读取
is = connection.getInputStream();
// 对输入流对象进行包装:charset根据工作项目组的要求来设置
br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
StringBuilder sb = new StringBuilder();
String temp;
// 循环遍历一行一行读取数据
while ((temp = br.readLine()) != null) {
sb.append(temp);
sb.append("\r\n");
}
result = sb.toString();
}
} catch (IOException e) {
log.error(String.format("发送请求发生异常, 异常信息为: %s", e.getMessage()));
} finally {// 使用finally块释放资源
try {
if (Objects.nonNull(br)) {
br.close();
}
if (Objects.nonNull(os)) {
os.close();
}
if (Objects.nonNull(is)) {
is.close();
}
} catch (IOException e) {
log.error(String.format("关闭流发生异常, 异常信息为: %s", e.getMessage()));
}
if (Objects.nonNull(connection)) {// 断开与远程地址url的连接
connection.disconnect();
}
}
return result;
}
/**
* 发送Post请求,参数放到Body中
*
* @param url 请求路径
* @param map 参数体
* @return 返回结果
*/
public static String sendPostBody(String url, Map<String, Object> map) {
OutputStreamWriter out = null;
BufferedReader in = null;
StringBuilder result = new StringBuilder();
try {
URL realUrl = new URL(url);
URLConnection conn = realUrl.openConnection();// 打开和URL之间的连接
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "application/json");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
out = new OutputStreamWriter(conn.getOutputStream(), StandardCharsets.UTF_8);// 获取URLConnection对象对应的输出流
// 发送请求参数
if (Objects.nonNull(map)) {
JSONObject obj = new JSONObject();
for (Map.Entry<String, Object> mt : map.entrySet()) {
obj.put(mt.getKey(), mt.getValue());
}
out.write(obj.toJSONString());
}
out.flush();// flush输出流的缓冲
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));// 定义BufferedReader输入流来读取URL的响应
String line;
while ((line = in.readLine()) != null) {
result.append(line);
}
} catch (Exception e) {
log.error(String.format("发送请求发生异常, 异常信息为: %s", e.getMessage()));
return null;
} finally {// 使用finally块释放资源
try {
if (Objects.nonNull(in)) {
in.close();
}
if (Objects.nonNull(out)) {
out.close();
}
} catch (IOException e) {
log.error(String.format("关闭流发生异常, 异常信息为: %s", e.getMessage()));
}
}
return result.toString();
}
/**
* 发送HttpGet请求
*
* @param getUrl 请求地址
* @return
*/
public static String httpGet(String getUrl) {
StringBuilder buffer = new StringBuilder();
HttpURLConnection connection = null;
InputStream inputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;
try {
URL url = new URL(getUrl);
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(false);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setRequestMethod("GET");
connection.connect();
// 将返回的输入流转换成字符串
inputStream = connection.getInputStream();
inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
bufferedReader = new BufferedReader(inputStreamReader);
String str;
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
} catch (Exception e) {
log.error(String.format("发送请求发生异常, 异常信息为: %s", e.getMessage()));
} finally {// 使用finally块释放资源
try {
if (Objects.nonNull(bufferedReader)) {
bufferedReader.close();
}
if (Objects.nonNull(inputStreamReader)) {
inputStreamReader.close();
}
if (Objects.nonNull(inputStream)) {
inputStream.close();
}
} catch (Exception e) {
log.error(String.format("释放资源发生异常, 异常信息为: %s", e.getMessage()));
}
if (Objects.nonNull(connection)) {
connection.disconnect();
}
}
return buffer.toString();
}
/**
* 利用代理服务器请求数据
*
* @param apiURL 需要请求的接口地址
* @param params 请求接口传参
* @param proxyHost 代理服务器路径
* @param proxyPort 代理服务器端口号
* @return 返回结果
* @throws Exception
*/
private static String proxyPOST(String apiURL, List<BasicNameValuePair> params, String proxyHost, Integer proxyPort) throws Exception {
String result;
HttpPost post = new HttpPost(apiURL);
CloseableHttpClient client = null;
CloseableHttpResponse response = null;
HttpEntity entity = null;
try {
post.setHeader("Accept", "application/json, text/javascript, */*; q=0.01");
post.setHeader("X-Requested-With", "XMLHttpRequest");
post.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
// 表单请求参数
post.setEntity(new UrlEncodedFormEntity(params));
// 设置客户端超时时间
RequestConfig config = RequestConfig.custom()
.setSocketTimeout(50000)
.setConnectTimeout(50000)
.setConnectionRequestTimeout(50000)
.build();
HttpClientBuilder httpClientBuilder = HttpClients.custom()
.setDefaultRequestConfig(config);
log.info(proxyHost);
// 取代理服务器的ip和端口
HttpHost targetHost = new HttpHost(proxyHost, proxyPort);
CredentialsProvider credsProvider = new BasicCredentialsProvider();
/*credsProvider.setCredentials(
new AuthScope(targetHost.getHostName(), targetHost.getPort()),
new UsernamePasswordCredentials("", "")
);*/
httpClientBuilder.setDefaultCredentialsProvider(credsProvider);
httpClientBuilder.setProxy(targetHost);
log.info("设置代理完成...");
client = httpClientBuilder.build();
log.info("获取客户端请求...");
// 执行POST请求
response = client.execute(post);
entity = response.getEntity();
int statusCode = response.getStatusLine().getStatusCode();
log.info("执行请求结束...响应状态码:" + statusCode);
result = EntityUtils.toString(entity, "UTF-8");
} catch (Exception e) {
post.abort();
throw e;
} finally {
EntityUtils.consume(entity);
// 关闭连接 释放资源
post.releaseConnection();
if (response != null) {
response.close();
}
if (client != null) {
client.close();
}
}
return result;
}
/**
* 设置全局代理(更改系统属性方式)
*
* @throws IOException
*/
private void setGlobalProxy() throws IOException {
Properties prop = System.getProperties();
// 设置http访问要使用的代理服务器的地址
prop.setProperty("http.proxyHost", "183.45.78.31");
// 设置http访问要使用的代理服务器的端口
prop.setProperty("http.proxyPort", "8080");
// 设置不需要通过代理服务器访问的主机,可以使用*通配符,多个地址用|分隔
prop.setProperty("http.nonProxyHosts", "localhost|192.168.0.*");
// 设置安全访问使用的代理服务器地址与端口
// 它没有https.nonProxyHosts属性,它按照http.nonProxyHosts 中设置的规则访问
prop.setProperty("https.proxyHost", "183.45.78.31");
prop.setProperty("https.proxyPort", "443");
// 使用ftp代理服务器的主机、端口以及不需要使用ftp代理服务器的主机
prop.setProperty("ftp.proxyHost", "183.45.78.31");
prop.setProperty("ftp.proxyPort", "21");
prop.setProperty("ftp.nonProxyHosts", "localhost|192.168.0.*");
// socks代理服务器的地址与端口
prop.setProperty("socksProxyHost", "183.45.78.31");
prop.setProperty("socksProxyPort", "1080");
// 设置登陆到代理服务器的用户名和密码
Authenticator.setDefault(new MyAuthenticator("userName", "Password"));
}
private static void setProxy() throws IOException {
URL url = new URL("http://www.baidu.com");
// 创建代理服务器
InetSocketAddress addr = new InetSocketAddress("27.159.66.41", 20263);
// Proxy proxy = new Proxy(Proxy.Type.SOCKS, addr); // Socket 代理
Proxy proxy = new Proxy(Proxy.Type.HTTP, addr); // http 代理
//Authenticator.setDefault(new MyAuthenticator("username", "password"));// 设置代理的用户和密码
HttpURLConnection connection = (HttpURLConnection) url.openConnection(proxy);// 设置代理访问
InputStreamReader in = new InputStreamReader(connection.getInputStream());
BufferedReader reader = new BufferedReader(in);
while (true) {
String s = reader.readLine();
if (s != null) {
System.out.println(s);
}
}
}
static class MyAuthenticator extends Authenticator {
private String user = "";
private String password = "";
public MyAuthenticator(String user, String password) {
this.user = user;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password.toCharArray());
}
}
public static void main(String[] args) throws Exception {
List<BasicNameValuePair> params = new ArrayList<>();
final BasicNameValuePair pair1 = new BasicNameValuePair("principalId", "NXXZX8888");
final BasicNameValuePair pair2 = new BasicNameValuePair("pcursor", "");
final BasicNameValuePair pair3 = new BasicNameValuePair("count", "150");
final BasicNameValuePair pair4 = new BasicNameValuePair("operationName", "playbackFeedsQuery");
params.add(pair1);
params.add(pair2);
params.add(pair3);
params.add(pair4);
final BasicNameValuePair pair5 = new BasicNameValuePair("variables", JSON.toJSONString(params));
List<BasicNameValuePair> params1 = new ArrayList<>();
final BasicNameValuePair pair6 = new BasicNameValuePair("query", "query playbackFeedsQuery($principalId: String, $pcursor: String, $count: Int)" +
" {playbackFeeds(principalId: $principalId, pcursor: $pcursor, count: $count) {pcursor list " +
"{productId, coverUrl, caption, createTime, duration, viewCount, likeCount, commentCount, likeStatus, __typename, baseUrl, manifestUrl}}}");
params1.add(pair5);
params1.add(pair6);
System.out.println(proxyPOST("https://live.kuaishou.com/live_graphql", params1, "27.159.66.41", 20108));
}
}