admin
2022-03-25 17055fd8d36504b79a5def28f5d4b4740faf012d
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
package org.yeshi.utils;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.security.KeyStore;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
import javax.net.ssl.SSLContext;
 
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.apache.commons.io.FileUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.yeshi.utils.entity.ProxyIP;
 
import net.sf.json.JSONObject;
 
public class HttpUtil {
 
    public static List<String> getUrlListFromText(String text) {
        List<String> urlList = new ArrayList<String>();
        // 链接
        String URL_REGEX = "(((http|https)://)|(www\\.))[a-zA-Z0-9\\._-]+\\.[a-zA-Z]{2,6}(:[0-9]{1,4})?(/[a-zA-Z0-9\\&%_\\./-~-]*)?";
        Pattern p = Pattern.compile(URL_REGEX);
        Matcher matcher = p.matcher(text);
        while (matcher.find()) {
            String url = matcher.group();
            if (!StringUtil.isNullOrEmpty(url)) {
                urlList.add(url);
            }
        }
        return urlList;
    }
 
    /**
     * 获取链接中的参数
     *
     * @param url
     * @return
     */
    public static Map<String, String> getPramsFromUrl(String url) {
        Map<String, String> params = new HashMap<>();
        if (url != null && url.indexOf("?") > -1) {
            String paramStr = url.split("\\?")[1];
            String[] sts = paramStr.split("&");
            for (String st : sts) {
                if (st.indexOf("=") > -1) {
                    params.put(st.split("=")[0], st.split("=")[1]);
                }
            }
        }
        return params;
    }
 
    public static String getHost(String host) {
        try {
            URL url = new URL(host);
            return url.getHost();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return "";
    }
 
    public static String getShortLink(String url) {
        // return url;
        String shortLink = getShortLink3(url);
        if (StringUtil.isNullOrEmpty(shortLink))
            shortLink = getShortLink1(url);
        if (StringUtil.isNullOrEmpty(shortLink))
            shortLink = getShortLink2(url);
        return shortLink;
    }
 
    private static String getShortLink1(String url) {
        try {
            String totalUrl = String.format(
                    "http://suo.im/api.htm?format=json&url=%s&key=5d7728078e676d45275f816b@681bb0446f1e6af4f8fc6ce3cad2a684",
                    URLEncoder.encode(url));
            String result = get(totalUrl, null);
            System.out.println(result);
            try {
                JSONObject json = JSONObject.fromObject(result);
                return json.optString("url");
            } catch (Exception e) {
            }
        } catch (Exception e) {
        }
        return null;
    }
 
    // 搜狐网址
    private static String getShortLink2(String url) {
        try {
            String totalUrl = String.format("https://sohu.gg/api/?key=2zumZxsL8MuX&url=%s",
                    URLEncoder.encode(url, "UTF-8"));
            String result = get(totalUrl, null);
            if (result != null && result.startsWith("http"))
                return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
    private static String getShortLink3(String url) {
//        try {
//            String totalUrl = String.format("http://h5.flq001.com/short/createShort?url=%s",
//                    URLEncoder.encode(url, "UTF-8"));
//            String result = get(totalUrl, null);
//            JSONObject data = JSONObject.fromObject(result);
//            return data.optString("short_url");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        return null;
    }
 
    public static String get(String url) {
        return get(url, null);
    }
 
    /**
     * get请求
     *
     * @param url
     * @param timeoutMS-请求超时时间
     * @return
     */
    public static String get(String url, int timeoutMS) {
        HttpClient client = new HttpClient();
        client.getHttpConnectionManager().getParams().setConnectionTimeout(timeoutMS);
        try {
            GetMethod method = new GetMethod(url);
            client.executeMethod(method);
            return method.getResponseBodyAsString();
        } catch (Exception e) {
 
        }
        return null;
    }
 
    public static String get(String url, Map<String, String> headers) {
        HttpClient client = new HttpClient();
        try {
            GetMethod method = new GetMethod(url);
            if (headers != null) {
                Iterator<String> keys = headers.keySet().iterator();
                while (keys.hasNext()) {
                    String key = keys.next();
                    method.addRequestHeader(key, headers.get(key));
                }
            }
            client.executeMethod(method);
            return method.getResponseBodyAsString();
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
 
    public static String get(String url, Map<String, String> headers, ProxyIP ipInfo) {
        HttpClient client = new HttpClient();
        if (ipInfo != null)
            client.getHostConfiguration().setProxy(ipInfo.getIp(), ipInfo.getPort());
        client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
        try {
            GetMethod method = new GetMethod(url);
            if (headers != null) {
                Iterator<String> keys = headers.keySet().iterator();
                while (keys.hasNext()) {
                    String key = keys.next();
                    method.addRequestHeader(key, headers.get(key));
                }
            }
            client.executeMethod(method);
            return method.getResponseBodyAsString();
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
 
    public static String get(String url, Map<String, String> headers, ProxyIP ipInfo, String userName,
                             String proxyPwd) {
        CloseableHttpClient httpclient = null;
        if (!StringUtil.isNullOrEmpty(userName)) {
            CredentialsProvider credsProvider = new BasicCredentialsProvider();
            credsProvider.setCredentials(new AuthScope(ipInfo.getIp(), ipInfo.getPort()),
                    new UsernamePasswordCredentials(userName, proxyPwd));
            httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
        } else
            HttpClients.createDefault();
        HttpHost proxy = null;
        if (ipInfo.getPort() != 0 && ipInfo.getPort() != 80) {
            proxy = new HttpHost(ipInfo.getIp(), ipInfo.getPort());
        } else
            proxy = new HttpHost(ipInfo.getIp());
        RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
        HttpPost post = new HttpPost(url);
        try {
            post.setConfig(config);
            CloseableHttpResponse response = httpclient.execute(post);
            return EntityUtils.toString(response.getEntity());
        } catch (Exception e) {
            return null;
        }
 
    }
 
    public static String getAsString(String url, String fromCharset, String toCharset) {
        HttpClient client = new HttpClient();
        GetMethod method = new GetMethod(url);
        try {
            client.executeMethod(method);
            String response = method.getResponseBodyAsString();
            String s1 = new String(response.getBytes(fromCharset), toCharset);
            return s1;
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
 
    public static String get(String url, Map<String, String> params, Map<String, String> headers) {
        HttpClient client = new HttpClient();
        try {
            Iterator<String> keys = params.keySet().iterator();
            url += "?";
            while (keys.hasNext()) {
                String key = keys.next();
                url += String.format("%s=%s&", key, URLEncoder.encode(params.get(key), "UTF-8"));
            }
            GetMethod method = new GetMethod(url);
            if (headers != null) {
                keys = headers.keySet().iterator();
                while (keys.hasNext()) {
                    String key = keys.next();
                    headers.get(key);
                    method.setRequestHeader(key, headers.get(key));
                }
            }
            client.executeMethod(method);
            System.out.println(method.getResponseHeader("cookies"));
            String result = method.getResponseBodyAsString();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
 
    public static InputStream getAsInputStream(String url) {
        HttpClient client = new HttpClient();
        GetMethod method = new GetMethod(url);
        try {
            client.executeMethod(method);
            return method.getResponseBodyAsStream();
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
 
    /**
     * 下载图片文件
     *
     * @param url
     * @return
     */
    public static InputStream downLoadImg(String url) throws org.yeshi.utils.exception.HttpException {
        HttpClient client = new HttpClient();
        GetMethod method = new GetMethod(url);
        try {
            client.executeMethod(method);
            Header contentTypeHeader = method.getResponseHeader("Content-Type");
            if (contentTypeHeader == null || contentTypeHeader.getValue().contains("text"))
                throw new org.yeshi.utils.exception.HttpException(2, "图片下载出错");
            return method.getResponseBodyAsStream();
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
 
    public static InputStream getAsInputStream(String url, Map<String, String> headers) {
        HttpClient client = new HttpClient();
        GetMethod method = new GetMethod(url);
        Iterator<String> keys = headers.keySet().iterator();
        while (keys.hasNext()) {
            String key = keys.next();
            method.setRequestHeader(key, headers.get(key));
        }
 
        try {
            client.executeMethod(method);
            return method.getResponseBodyAsStream();
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
 
    public static InputStream getImage(String url) {
        HttpClient client = new HttpClient();
        GetMethod method = new GetMethod(url);
        try {
            method.setRequestHeader("Content-Type", "image/jpeg");
            client.executeMethod(method);
            return method.getResponseBodyAsStream();
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
 
    public static String postSimple(String url) {
        HttpClient client = new HttpClient();
        PostMethod method = new PostMethod(url);
        try {
            client.executeMethod(method);
            return method.getResponseBodyAsString();
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
 
    public static String post(String url) {
        HttpClient client = new HttpClient();
        PostMethod method = new PostMethod(url);
        method.addRequestHeader("Content-Type", "text/html;charset=UTF-8");
        method.setRequestHeader("Content-Type", "text/html;charset=UTF-8");
        try {
            client.executeMethod(method);
            return convertInputStreamToString(method.getResponseBodyAsStream());
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
 
    public static String post(String url, Map<String, String> map) {
        Iterator<String> its = map.keySet().iterator();
        NameValuePair[] params = new NameValuePair[map.keySet().size()];
        int p = 0;
        while (its.hasNext()) {
            String key = its.next();
            NameValuePair np = new NameValuePair(key, map.get(key));
            params[p] = np;
            p++;
        }
 
        HttpClient client = new HttpClient();
        PostMethod method = new PostMethod(url);
        method.addRequestHeader("Content-Type", "text/html;charset=UTF-8");
        method.setRequestHeader("Content-Type", "text/html;charset=UTF-8");
        method.setRequestBody(params);
        try {
            client.executeMethod(method);
            return convertInputStreamToString(method.getResponseBodyAsStream());
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
 
    public static String post(String url, Map<String, String> map, Map<String, String> headers, ProxyIP ipInfo) {
        HttpClient client = new HttpClient();
        if (ipInfo != null)
            client.getHostConfiguration().setProxy(ipInfo.getIp(), ipInfo.getPort());
        PostMethod pm = new PostMethod(url);// 创建HttpPost对象
        NameValuePair[] ns = new NameValuePair[map.keySet().size()];
        Iterator<String> its = map.keySet().iterator();
        int index = 0;
        while (its.hasNext()) {
            String key = its.next();
            ns[index++] = (new NameValuePair(key, map.get(key)));
        }
 
        if (headers != null) {
            its = headers.keySet().iterator();
            while (its.hasNext()) {
                String key = its.next();
                pm.setRequestHeader(key, headers.get(key));
            }
        }
 
        try {
            pm.setRequestBody(ns);
            client.executeMethod(pm);
            return pm.getResponseBodyAsString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
 
    public static String post(String url, Map<String, String> map, Map<String, String> headers) {
        HttpClient client = new HttpClient();
//         client.getHostConfiguration().setProxy("192.168.3.122", 8888);
        PostMethod pm = new PostMethod(url);// 创建HttpPost对象
        NameValuePair[] ns = new NameValuePair[map.keySet().size()];
        Iterator<String> its = map.keySet().iterator();
        int index = 0;
        while (its.hasNext()) {
            String key = its.next();
            ns[index++] = (new NameValuePair(key, map.get(key)));
        }
 
        if (headers != null) {
            its = headers.keySet().iterator();
            while (its.hasNext()) {
                String key = its.next();
                pm.setRequestHeader(key, headers.get(key));
            }
        }
 
        try {
            pm.setRequestBody(ns);
            client.executeMethod(pm);
            return pm.getResponseBodyAsString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
 
    private static String convertInputStreamToString(InputStream inputStream) {
        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
        StringBuffer stringBuffer = new StringBuffer();
        String str = "";
        try {
            while ((str = br.readLine()) != null) {
                stringBuffer.append(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuffer.toString();
    }
 
    @SuppressWarnings("deprecation")
    public static String post(String url, String entity) {
        HttpClient client = new HttpClient();
        PostMethod method = new PostMethod(url);
        method.addRequestHeader("Content-Type", "text/html;charset=UTF-8");
        method.setRequestHeader("Content-Type", "text/html;charset=UTF-8");
        method.setRequestBody(entity);
        try {
            client.executeMethod(method);
            return convertInputStreamToString(method.getResponseBodyAsStream());
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
 
    @SuppressWarnings("deprecation")
    public static String post(String url, String entity, String charset) {
        HttpClient client = new HttpClient();
        PostMethod method = new PostMethod(url);
        method.addRequestHeader("Content-Type", "text/html;charset=" + charset);
        method.setRequestHeader("Content-Type", "text/html;charset=" + charset);
        method.setRequestBody(entity);
        try {
            client.executeMethod(method);
            return convertInputStreamToString(method.getResponseBodyAsStream());
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
 
    public static String postInputStream(String url, File file) throws FileNotFoundException {
        HttpClient client = new HttpClient();
        PostMethod postMethod = new PostMethod(url);
        Part[] parts = {new StringPart("filename", file.getName()), new StringPart("filelength", file.length() + ""),
                new StringPart("content-type", "image/jpg"), new FilePart("file", file)};
        postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));
 
        try {
            client.executeMethod(postMethod);
            return convertInputStreamToString(postMethod.getResponseBodyAsStream());
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
 
    public static File downloadFile(String url, String targetPath) {
        try {
            URL httpurl = new URL(url);
            FileUtils.copyURLToFile(httpurl, new File(targetPath));
        } catch (Exception e) {
            e.printStackTrace();
        }
        File f = new File(targetPath);
        if (!f.exists() || f.length() <= 0)
            return null;
        return f;
    }
 
    @SuppressWarnings("deprecation")
    public static InputStream postForInputstream(String url, String entity) {
        HttpClient client = new HttpClient();
 
        PostMethod method = new PostMethod(url);
        method.addRequestHeader("Content-Type", "text/html;charset=UTF-8");
        method.setRequestHeader("Content-Type", "text/html;charset=UTF-8");
        method.setRequestBody(entity);
        try {
            client.executeMethod(method);
            return method.getResponseBodyAsStream();
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
 
    public static String post(String url, String fileKey, File f) {
        PostMethod filePost = new PostMethod(url);
        HttpClient client = new HttpClient();
        try {
            Part[] parts = {new FilePart(fileKey, f)};
            filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
            client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
            int status = client.executeMethod(filePost);
            if (status == HttpStatus.SC_OK) {
                return filePost.getResponseBodyAsString();
            } else {
                System.out.println("上传失败");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            filePost.releaseConnection();
        }
        return null;
    }
 
    @SuppressWarnings("deprecation")
    public static String httpsPost(String url, String body, String pwd, InputStream cert) throws Exception {
        String result = "";
        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        InputStream instream = cert;
        try {
            keyStore.load(instream, pwd.toCharArray());
        } finally {
            instream.close();
        }
 
        // Trust own CA and all self-signed certs
        SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, pwd.toCharArray()).build();
        // Allow TLSv1 protocol only
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[]{"TLSv1"}, null,
                SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
        CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
        try {
            HttpPost httppost = new HttpPost(url);
            httppost.setHeader("Content-Type", "text/html;charset=UTF-8");
            System.out.println("executing request" + httppost.getRequestLine());
            httppost.setEntity(new StringEntity(body, "UTF-8"));
            CloseableHttpResponse response = httpclient.execute(httppost);
            try {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    result = convertInputStreamToString(entity.getContent());
                }
                EntityUtils.consume(entity);
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
 
        return result;
    }
 
    /**
     * 获取post返回location
     *
     * @param url
     * @return
     */
    public static String getLocation(String url) {
        HttpClient client = new HttpClient();
        PostMethod method = new PostMethod(url);
        try {
            client.executeMethod(method);
            return method.getResponseHeader("location").getValue();
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}