• HttpClientUtil


    public class HttpClientUtil {
    
    	/**
    	 * Http 基本认证
    	 * @param username 用户名
    	 * @param password 密码
    	 * @return Base64加密值
    	 *
    	 */
    	public static String getBasicAuthentication(String username, String password) {
    		String valueToEncode = username + ":" + password;
    		return "Basic " + Base64.getEncoder().encodeToString(valueToEncode.getBytes());
    	}
    
    	public static String sendGet(String url) {
            CloseableHttpResponse resp = null;
            CloseableHttpClient client = null;
            try {
                client = HttpClients.createDefault();
                HttpGet get = new HttpGet(url);
                resp = client.execute(get);
                HttpEntity entity = resp.getEntity();
                return EntityUtils.toString(entity, "utf-8");
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (resp != null) {
                        resp.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    if (client != null) {
                        client.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }
    
    	public static String sendGet(String url,Map<String, String> headers) {
            CloseableHttpResponse resp = null;
            CloseableHttpClient client = null;
            try {
                client = HttpClients.createDefault();
                HttpGet get = new HttpGet(url);
                for(String key : headers.keySet()){
                    get.addHeader(key, headers.get(key));
                }
                resp = client.execute(get);
                HttpEntity entity = resp.getEntity();
                return EntityUtils.toString(entity, "utf-8");
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (resp != null) {
                        resp.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    if (client != null) {
                        client.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }
    
    	/**
         * post请求json
         * @param url
         * @param json
         * @return
         * @throws Exception
         */
        public static String sendJsonPost(String url, String json) throws Exception {
            return sendPost(url, json, "application/json");
        }
    
    
        public static String sendPost(String url, String content, String type) {
            CloseableHttpClient client = null;
            CloseableHttpResponse resp = null;
            try {
                client = HttpClients.createDefault();
                HttpPost post = new HttpPost(url);
                post.addHeader("Content-type", type);
                StringEntity entity = new StringEntity(content, ContentType.create(type, "UTF-8"));
                post.setEntity(entity);
                resp = client.execute(post);
                int statusCode = resp.getStatusLine().getStatusCode();
                return EntityUtils.toString(resp.getEntity(), "utf-8");
            } catch (UnsupportedCharsetException | ParseException | IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (client != null)
                        client.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                try {
                    if (resp != null)
                        resp.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }
    
    	public static String sendPost(String url, Map<String, String> headers, String content) {
            CloseableHttpClient client = null;
            CloseableHttpResponse resp = null;
            try {
                client = HttpClients.createDefault();
                HttpPost post = new HttpPost(url);
                for(String key : headers.keySet()){
                    post.addHeader(key, headers.get(key));
                }
                StringEntity entity = new StringEntity(content, "UTF-8");
                post.setEntity(entity);
                resp = client.execute(post);
                return EntityUtils.toString(resp.getEntity(), "utf-8");
            } catch (UnsupportedCharsetException | ParseException | IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (client != null)
                        client.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                try {
                    if (resp != null)
                        resp.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }
    
    	public static String sendPost(String url,Map<String, String> headers, Map<String,String> params){
            CloseableHttpClient client = null;
            CloseableHttpResponse resp = null;
            try {
                client = HttpClients.createDefault();
                HttpPost post = new HttpPost(url);
                for (String key : headers.keySet()) {
                    post.addHeader(key, headers.get(key));
                }
                List<NameValuePair> parameters = new ArrayList<>();
                for (Map.Entry<String, String> entry : params.entrySet()) {
                    parameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
                }
                UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters,StandardCharsets.UTF_8);
                post.setEntity(formEntity);
                resp = client.execute(post);
                return EntityUtils.toString(resp.getEntity(), StandardCharsets.UTF_8);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (client != null) {
                        client.close();
                    }
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                try {
                    if (resp != null) {
                        resp.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }
    
    	/**
    	 * Http Post 请求设置过期时间
    	 * @param url 请求地址
    	 * @param headers 请求头
    	 * @param content 参数内容 JSON
    	 * @return 响应
    	 */
    	public static String sendPost(String url, Map<String, String> headers, String content) {
    		CloseableHttpClient client = null;
    		CloseableHttpResponse resp = null;
    		try {
    			RequestConfig defaultRequestConfig = RequestConfig.custom()
    					.setConnectTimeout(5000)// 设置连接超时时间
    					.setConnectionRequestTimeout(5000) // 设置从 connect Manager 获取 Connection 超时时间
    					.setSocketTimeout(5000)// 请求获取数据的超时时间
    					.build();
    			client = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build();
    			HttpPost post = new HttpPost(url);
    			for (String key : headers.keySet()) {
    				post.addHeader(key, headers.get(key));
    			}
    			StringEntity entity = new StringEntity(content, StandardCharsets.UTF_8);
    			post.setEntity(entity);
    			resp = client.execute(post);
    			return EntityUtils.toString(resp.getEntity(), StandardCharsets.UTF_8);
    		} catch (UnsupportedCharsetException | ParseException | IOException e) {
    			e.printStackTrace();
    		} finally {
    			try {
    				if (client != null)
    					client.close();
    			} catch (IOException e1) {
    				e1.printStackTrace();
    			}
    			try {
    				if (resp != null)
    					resp.close();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    		return null;
    	}
    
    	public static String sendPut(String url, Map<String, String> headers, String content) {
            CloseableHttpClient client = null;
            CloseableHttpResponse resp = null;
            try {
                client = HttpClients.createDefault();
                HttpPut put = new HttpPut(url);
                for(String key : headers.keySet()){
                    put.addHeader(key, headers.get(key));
                }
                StringEntity entity = new StringEntity(content, "UTF-8");
                put.setEntity(entity);
                resp = client.execute(put);
                return EntityUtils.toString(resp.getEntity(), "utf-8");
            } catch (UnsupportedCharsetException | ParseException | IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (client != null)
                        client.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                try {
                    if (resp != null)
                        resp.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }
    
    	public static String sendPut(String url, Map<String, String> headers, byte[] content) {
            CloseableHttpClient client = null;
            CloseableHttpResponse resp = null;
            try {
                client = HttpClients.createDefault();
                HttpPut put = new HttpPut(url);
                for(String key : headers.keySet()){
                    put.addHeader(key, headers.get(key));
                }
                ByteArrayEntity entity = new ByteArrayEntity(content);
                put.setEntity(entity);
                resp = client.execute(put);
                return EntityUtils.toString(resp.getEntity(), "utf-8");
            } catch (UnsupportedCharsetException | ParseException | IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (client != null)
                        client.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                try {
                    if (resp != null)
                        resp.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }
    
    	public static String sendDelete(String url, Map<String, String> headers) {
            CloseableHttpClient client = null;
            CloseableHttpResponse resp = null;
            try {
                client = HttpClients.createDefault();
                HttpDelete delete = new HttpDelete(url);
                for(String key : headers.keySet()){
                    delete.addHeader(key, headers.get(key));
                }
                resp = client.execute(delete);
                return EntityUtils.toString(resp.getEntity(), "utf-8");
            } catch (UnsupportedCharsetException | ParseException | IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (client != null)
                        client.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                try {
                    if (resp != null)
                        resp.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }
    
    }
    
    • 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
  • 相关阅读:
    第十一章 GetAway服务网关详解
    第五章单元测试
    2023年上海市安全员B证证模拟考试题库及上海市安全员B证理论考试试题
    用Airtest快速实现手机文件读写与删除功能
    c++中的异常处理
    用户体验 | 如何度量用户体验?
    BigDecimal 类型的累加操作
    MySQL 过滤重复数据
    支持向量机SVM:从数学原理到实际应用
    【鸿蒙软件开发】文本输入(TextInput/TextArea)
  • 原文地址:https://blog.csdn.net/userenoch/article/details/127958496