xiejun
2023-09-18 978f74777e10a0531c4413ab36320d2be36f42ea
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
package com.vci.rmip.code.client.codeapply.Apply410.utils;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.shaded.com.google.gson.JsonObject;
import com.vci.rmip.code.client.codeapply.Apply410.object.R;
 
import org.apache.commons.lang.StringUtils;
import org.apache.http.*;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
 
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
 
public class HttpUtil {
    private static final CloseableHttpClient httpclient = HttpClients.createDefault();
 
    /**
     * 发送HttpGet请求
     * @param url
     * @return
     */
    public static String sendGet(String url) {
 
        HttpGet httpget = new HttpGet(url);
        CloseableHttpResponse response = null;
        try {
            response = httpclient.execute(httpget);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        String result = null;
        try {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                result = EntityUtils.toString(entity);
            }
        } catch (ParseException | IOException e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
 
    /**
     * 发送HttpGet带参请求
     * @param url
     * @param params
     * @return
     */
    public static R sendGet(String url, Map<String, String> params, Map<String,String> headers) {
        R r=new R();
        // 获取连接客户端工具
        CloseableHttpClient httpClient = HttpClients.createDefault();
        String entityStr = null;
        CloseableHttpResponse response = null;
        try {
            /*
             * 由于GET请求的参数都是拼装在URL地址后方,所以我们要构建一个URL,带参数
             */
            URIBuilder uriBuilder = new URIBuilder(url);
            /** 第一种添加参数的形式 */
        /*uriBuilder.addParameter("name", "root");
        uriBuilder.addParameter("password", "123456");*/
            /** 第二种添加参数的形式 */
            List<NameValuePair> list = new LinkedList<>();
            //设置头部
            for(Map.Entry<String,String> entry:params.entrySet()){
                BasicNameValuePair param1 = new BasicNameValuePair(entry.getKey(), entry.getValue());
                list.add(param1);
            }
            uriBuilder.setParameters(list);
            // 根据带参数的URI对象构建GET请求对象
            HttpGet httpGet = new HttpGet(uriBuilder.build());
            /*
             * 添加请求头信息
             */
            if(headers!=null&&headers.size()>0) {
                for (Map.Entry<String, String> entry : headers.entrySet()) {
                    httpGet.addHeader(entry.getKey(), entry.getValue());
                }
            }
            // 执行请求
            response = httpClient.execute(httpGet);
            // 获得响应的实体对象
            HttpEntity entity = response.getEntity();
            // 使用Apache提供的工具类进行转换成字符串
            entityStr = EntityUtils.toString(entity, "UTF-8");
            if(StringUtils.isNotBlank(entityStr)){
                r = JSONObject.toJavaObject(JSONObject.parseObject(entityStr), R.class);
            }
        } catch (ClientProtocolException e) {
            System.err.println("Http协议出现问题");
            e.printStackTrace();
        } catch (ParseException e) {
            System.err.println("解析错误");
            e.printStackTrace();
        } catch (URISyntaxException e) {
            System.err.println("URI解析异常");
            e.printStackTrace();
        } catch (IOException e) {
            System.err.println("IO异常");
            e.printStackTrace();
        } finally {
            // 释放连接
            if (null != response) {
                try {
                    response.close();
                    httpClient.close();
                } catch (IOException e) {
                    System.err.println("释放连接出错");
                    e.printStackTrace();
                }
            }
        }
        return r;
    }
    /**
     * 发送HttpGet带参请求
     * @param url
     * @param header
     * @return
     */
    public static String sendGet(String url, Map<String, String> header) {
        HttpGet httpGet = new HttpGet(url);
 
 
        //设置头部
        for(Map.Entry entry:header.entrySet()){
//            System.out.println(entry.getKey()+ "###########" + entry.getValue());
            httpGet.setHeader(entry.getKey().toString(),entry.getValue().toString());
        }
//        System.out.println(jsonObject.toString());
 
 
//        HttpGet httpget = new HttpGet(url);
        CloseableHttpResponse response = null;
        try {
            response = httpclient.execute(httpGet);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        String result = null;
        try {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                result = EntityUtils.toString(entity);
            }
        } catch (ParseException | IOException e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
    /**
     * 发送HttpPost请求,参数为map
     * @param url
     * @param jsonDataStr
     * @return
     */
    public static R sendPost(String url, String jsonDataStr,Map<String,String> headers) {
        R r=new R();
//        JsonObject formparams = new JsonObject();
//        for (Map.Entry<String, String> entry : map.entrySet()) {
//            formparams.add(entry.getKey(), entry.getValue();
//        }
        //json 格式
        //UrlEncodedFormEntity entity = new UrlEncodedFormEntity(jsonDataStr, Consts.UTF_8);
//        System.out.println(jsonObject.toString());
        StringEntity entity = new StringEntity(jsonDataStr, Consts.UTF_8);
        HttpPost httppost = new HttpPost(url);
        /*
         * 添加请求头信息
         */
        if(headers!=null&&headers.size()>0) {
            for (Map.Entry<String, String> entry : headers.entrySet()) {
                httppost.addHeader(entry.getKey(), entry.getValue());
            }
        }
        httppost.setEntity(entity);
        CloseableHttpResponse response = null;
        try {
            response = httpclient.execute(httppost);
        } catch (IOException e) {
            e.printStackTrace();
        }
        HttpEntity entity1 = response.getEntity();
        String result = null;
        try {
            result = EntityUtils.toString(entity1, "UTF-8");
            // 使用Apache提供的工具类进行转换成字符串
            if(StringUtils.isNotBlank(result)){
                r = JSONObject.toJavaObject(JSONObject.parseObject(result), R.class);
            }
        } catch (ParseException | IOException e) {
            e.printStackTrace();
        }
        return r;
    }
    /**
     * 发送HttpPost请求,参数为map
     * @param url
     * @param map
     * @return
     */
    public static R sendPost(String url, Map<String,String> map,Map<String,String> headers) {
        R r=new R();
//        JsonObject formparams = new JsonObject();
//        for (Map.Entry<String, String> entry : map.entrySet()) {
//            formparams.add(entry.getKey(), entry.getValue();
//        }
        //json 格式
//        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
 
        JsonObject jsonObject = new JsonObject();
        for(Map.Entry entry:map.entrySet()){
//            System.out.println(entry.getKey()+ "###########" + entry.getValue());
            jsonObject.addProperty(entry.getKey().toString(),entry.getValue().toString());
        }
//        System.out.println(jsonObject.toString());
        StringEntity entity = new StringEntity(jsonObject.toString(), Consts.UTF_8);
        HttpPost httppost = new HttpPost(url);
        /*
         * 添加请求头信息
         */
        if(headers!=null&&headers.size()>0) {
            for (Map.Entry<String, String> entry : headers.entrySet()) {
                httppost.addHeader(entry.getKey(), entry.getValue());
            }
        }
        httppost.setEntity(entity);
        CloseableHttpResponse response = null;
        try {
            response = httpclient.execute(httppost);
        } catch (IOException e) {
            e.printStackTrace();
        }
        HttpEntity entity1 = response.getEntity();
        String result = null;
        try {
            result = EntityUtils.toString(entity1, "UTF-8");
            // 使用Apache提供的工具类进行转换成字符串
            if(StringUtils.isNotBlank(result)){
                r = JSONObject.toJavaObject(JSONObject.parseObject(result), R.class);
            }
        } catch (ParseException | IOException e) {
            e.printStackTrace();
        }
        return r;
    }
    /**
     * 发送HttpPost请求,参数为map
     * @param url
     * @param map
     * @return
     */
    public static String sendPost(String url, Map<String,String> map) {
//        JsonObject formparams = new JsonObject();
//        for (Map.Entry<String, String> entry : map.entrySet()) {
//            formparams.add(entry.getKey(), entry.getValue();
//        }
        //json 格式
//        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
        JsonObject jsonObject = new JsonObject();
        for(Map.Entry entry:map.entrySet()){
//            System.out.println(entry.getKey()+ "###########" + entry.getValue());
            jsonObject.addProperty(entry.getKey().toString(),entry.getValue().toString());
        }
//        System.out.println(jsonObject.toString());
        StringEntity entity = new StringEntity(jsonObject.toString(), Consts.UTF_8);
        HttpPost httppost = new HttpPost(url);
        httppost.setEntity(entity);
        CloseableHttpResponse response = null;
        try {
            response = httpclient.execute(httppost);
        } catch (IOException e) {
            e.printStackTrace();
        }
        HttpEntity entity1 = response.getEntity();
        String result = null;
        try {
            result = EntityUtils.toString(entity1);
        } catch (ParseException | IOException e) {
            e.printStackTrace();
        }
        return result;
    }
    /**
     * 发送HttpPost请求,参数为map
     * @param url
     * @param dataMap
     * @return
     */
    public static String sendFormPost(String url, Map<String,String> dataMap, Map<String,String> headers) {
        String result = null;
        try {
 
            //json 格式
            List<NameValuePair> nvps = new ArrayList<>();
//
            HttpPost httppost = new HttpPost(url);
            if (dataMap != null && dataMap.size() > 0) {
                for (Map.Entry<String, String> entry : dataMap.entrySet()) {
                    nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
                }
            }
            /*
             * 添加请求头信息
             */
            if (headers != null && headers.size() > 0) {
                for (Map.Entry<String, String> entry : headers.entrySet()) {
                    httppost.addHeader(entry.getKey(), entry.getValue());
                }
            }
            httppost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8")); //将参数传入post方法中
            //httppost.setEntity(entity);
            CloseableHttpResponse response = null;
            response = httpclient.execute(httppost);
            HttpEntity entity1 = response.getEntity();
 
            result = EntityUtils.toString(entity1, "UTF-8");
            // 使用Apache提供的工具类进行转换成字符串
            if (StringUtils.isNotBlank(result)) {
                return result;
            }
        }catch (Throwable e){
            e.printStackTrace();;
        }
        return result;
    }
    /**
     * 发送不带参数的HttpPost请求
     * @param url
     * @return
     */
    public static String sendPost(String url) {
        HttpPost httppost = new HttpPost(url);
        CloseableHttpResponse response = null;
        try {
            response = httpclient.execute(httppost);
        } catch (IOException e) {
            e.printStackTrace();
        }
        HttpEntity entity = response.getEntity();
        String result = null;
        try {
            result = EntityUtils.toString(entity);
        } catch (ParseException | IOException e) {
            e.printStackTrace();
        }
        return result;
    }
 
 
}