2021与蓝度共同重构项目,服务端
liuhaonan
2022-10-25 d495f9b8cdc83663e4189bc3cc72ac9543ff5555
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
package com.sandu.ximon.admin.utils;
 
import com.sandu.ximon.admin.utils.request.CommonHeader;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
 
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;
 
public class VnnoxRequestUtil {
 
    private static final String UTF8 = "utf-8";
 
 
    public static String get(String url){
        CloseableHttpClient client = HttpClients.createDefault();
        HttpGet get = new HttpGet(url);
        String result = null;
        try {
            HttpResponse httpResponse = client.execute(get);
            InputStream inStream = httpResponse.getEntity().getContent();
 
            result = streamToString(inStream,UTF8);
 
            inStream.close();
        } catch (Exception e) {
            return null;
        }
        return result;
    }
 
public static String GetWithHeader (String url, List<NameValuePair> urlParameters, Map<String, String> header) throws URISyntaxException {
 
 
    URI finalURI = new URIBuilder(url).setParameters(urlParameters).build();
    CloseableHttpClient client = HttpClients.createDefault();
    HttpGet get = new HttpGet(finalURI);
    for(String key: header.keySet()) {
        get.setHeader(key, header.get(key));
    }
    String result = null;
    try {
//            StringEntity s = new StringEntity(params, UTF8);
//            s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
//                    "application/json"));
        HttpResponse httpResponse = client.execute(get);
        InputStream inStream = httpResponse.getEntity().getContent();
 
        result = streamToString(inStream, UTF8);
 
        inStream.close();
    } catch (Exception e) {
        return null;
    }
    return result;
}
 
 
 
    public static String PostWithHeader (String URL, String json, CommonHeader header) {
        System.out.println("****************************************");
        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost post = new HttpPost(URL);
        for(String key: header.keySet()) {
            post.setHeader(key, header.get(key));
        }
        String result = null;
        try {
            StringEntity s = new StringEntity(json, UTF8);
            s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
                    "application/json"));
            post.setEntity(s);
            HttpResponse httpResponse = client.execute(post);
            InputStream inStream = httpResponse.getEntity().getContent();
 
            result = streamToString(inStream,UTF8);
 
            inStream.close();
        } catch (Exception e) {
            System.out.println("error"   + e.getMessage());
            return null;
        }
        return result;
    }
 
 
 
    public static String streamToString(InputStream in, String encoding){
        // 将流转换为字符串
        try {
            StringBuffer sb = new StringBuffer();
            byte[] b = new byte[1024];
            for (int n; (n = in.read(b)) != -1;) {
                sb.append(new String(b, 0, n, encoding));
            }
            return sb.toString();
        }  catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("提取 requestBody 异常", e);
        }
    }
 
}