HTTP 请求 的方法Util

2018-11-20 03:20:11来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

HTTP请求 的一系列方法总结


 

  1  /**
  2         * *******************************传统请求--开始**************************************
  3         * */
  4     
  5     /**
  6      * HttpClient 发送 Post 请求
  7      * @param url  路径:String
  8      * @param mapParam 参数:java.util.Map
  9      * @return
 10      */
 11     public static String sendClientPost(String url,Map<String,String> headers,Map<String,String> mapParam){
 12         return httpPost(url, headers,new JSONObject(mapParam), false);
 13     }
 14     
 15     /**
 16      * HttpClient 发送 Post 请求
 17      * @param url  路径:String
 18      * @param JsonObjectParam 参数:org.json.JSONObject
 19      * @return
 20      */
 21     public static String sendClientPost(String url,Map<String,String> headers,JSONObject JsonObjectParam){
 22         return httpPost(url, headers,JsonObjectParam, false);
 23     }
 24     
 25     
 26     /**
 27      * HttpClient 发送 Get 请求
 28      * @param url 路径:String
 29      * @return
 30      */
 31     public static String sendClientGet(String url){
 32         return httpGet(url,null);
 33     }
 34     /**
 35      * HttpClient 发送 Get 请求
 36      * @param url 路径:String
 37      * @param headers Map<String,String>
 38      * @return
 39      */
 40     public static String sendClientGet(String url,Map<String,String> headers){
 41         return httpGet(url,headers);
 42     }
 43     
 44     /**
 45      * HttpClient 发送 Delete 请求
 46      * @param url 路径:String
 47      * @return
 48      */
 49     public static String sendClientDelete(String url){
 50         return httpDelete(url,null);
 51     }
 52     /**
 53      * HttpClient 发送 Delete 请求
 54      * @param url 路径:String
 55      * @param headers Map<String,String>
 56      * @return
 57      */
 58     public static String sendClientDelete(String url,Map<String,String> headers){
 59         return httpDelete(url,headers);
 60     }
 61     
 62     
 63     /**
 64      * urlConnectionGet
 65      * @param url  路径
 66      * @return
 67      */
 68     public static String sendConnectionGet(String url,String param){
 69         return sendGet(url,param);
 70     } 
 71     
 72     /**
 73      * 向指定 URL 发送POST方法的请求
 74      * @param url 发送请求的 URL
 75      * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
 76      * @return 所代表远程资源的响应结果
 77      */
 78     public static String sendConnectionPost(String url,String param){
 79         return sendPost(url,param);
 80     }
 81     /**
 82         * *******************************传统请求--结束**************************************
 83         * */
 84     
 85  
 86     /**
 87         * *******************************Jsoup请求--开始**************************************
 88         * */
 89     
 90        /**
 91         * 发送 POST 请求
 92         * @param url 请求地址
 93         * @param cookie cookie:String
 94         * @param headers 头信息:Map<String,String>
 95         * @param params 参数:Map<String,Object>
 96         * @return String
 97         */
 98        public static String sendJsoupPost(String url,String cookie,Map<String,String> headers,Map<String,Object> params,boolean isJsonParam) {
 99            String sendRequest = sendRequest(url, cookie, headers,params, "POST",isJsonParam);
100            return sendRequest;
101        }
102        
103        /**
104         * 发送 POST 请求
105         * @param url 请求地址
106         * @param headers 头信息:Map<String,String>
107         * @param params 参数:Map<String,Object>
108         * @return String
109         */
110        public static String sendJsoupPost(String url,Map<String,String> headers,Map<String,Object> params,boolean isJsonParam) {
111            String sendRequest = sendRequest(url, null, headers,params, "POST", isJsonParam);
112            return sendRequest;
113        }
114        
115        /**
116         * 发送 POST 请求
117         * @param url 请求地址
118         * @param cookie cookie:String
119         * @param headers 头信息:Map<String,String>
120         * @param params 参数:Map<String,Object>
121         * @return String
122         */
123        public static String sendJsoupPost(String url,Map<String,Object> params,boolean isJsonParam) {
124            String sendRequest = sendRequest(url, null, null,params, "POST", isJsonParam);
125            return sendRequest;
126        }
127        
128        
129        /**
130         * 发送 POST 请求
131         * @param url 请求地址
132         * @return String
133         */
134        public static String sendJsoupPost(String url) {
135            String sendRequest = sendRequest(url,  null, null,null, "POST",false);
136            return sendRequest;
137        }
138        
139        
140        /**
141         * 发送 GET 请求
142         * @param url 请求地址: String
143         * @param cookie cookie:String
144         * @param headers 头信息:Map<String,String>
145         * @param params 参数:Map<String,Object>
146         * @return String
147         */
148        public static String sendJsoupGet(String url,String cookie,Map<String,String> headers,Map<String,Object> params) {
149            String sendRequest = sendRequest(url, cookie, headers,params, "GET",false);
150            return sendRequest;
151        }
152        
153        /**
154         * 发送 GET 请求
155         * @param url 请求地址: String
156         * @param headers 头信息:Map<String,String>
157         * @param params 参数:Map<String,Object>
158         * @return String
159         */
160        public static String sendJsoupGet(String url,Map<String,String> headers,Map<String,Object> params) {
161            String sendRequest = sendRequest(url, null, headers,params, "GET",false);
162            return sendRequest;
163        }
164        
165        /**
166         * 发送 GET 请求
167         * @param url 请求地址: String
168         * @return String
169         */
170        public static String sendJsoupGet(String url) {
171            String sendRequest = sendRequest(url, null, null,null, "GET",false);
172            return sendRequest;
173        }
174        
175        /**
176         * 发送 GET 请求
177         * @param url 请求地址: String
178         * @param params 参数:Map<String,Object>
179         * @return String
180         */
181        public static String sendJsoupGet(String url,Map<String,Object> params) {
182            String sendRequest = sendRequest(url, null, null,params, "GET",false);
183            return sendRequest;
184        }
185        
186     /**
187         * *******************************Jsoup请求--结束**************************************
188         * */
189        
190        
191        /**
192         * *******************************获取网页对象--开始**************************************
193         * */
194        
195        /**
196         * 发送 GET 请求
197         * @param url 请求地址: String
198         * @return Document
199         */
200        public static Document sendJsoupGetDocument(String url) {
201            Document sendRequestDocument = sendRequestDocument(url, null, null,null, "GET",false);
202            return sendRequestDocument;
203        }
204        
205        /**
206         * 发送 GET 请求
207         * @param url 请求地址: String
208         * @param cookie cookie:String
209         * @param headers 头信息:Map<String,String>
210         * @param params 参数:Map<String,Object>
211         * @return Document
212         */
213        public static Document sendJsoupGetDocument(String url,String cookie,Map<String,String> headers,Map<String,Object> params) {
214            Document sendRequest = sendRequestDocument(url, cookie, headers,params, "GET",false);
215            return sendRequest;
216        }
217        
218        /**
219         * 发送 POST 请求
220         * @param url 请求地址: String
221         * @return Document
222         */
223        public static Document sendJsoupPostDocument(String url) {
224            Document sendRequestDocument = sendRequestDocument(url, null, null,null, "POST",false);
225            return sendRequestDocument;
226        }
227        
228        /**
229         * 发送 POST 请求
230         * @param url 请求地址
231         * @param cookie cookie:String
232         * @param headers 头信息:Map<String,String>
233         * @param params 参数:Map<String,Object>
234         * @return Document
235         */
236        public static Document sendJsoupPostDocument(String url,String cookie,Map<String,String> headers,Map<String,Object> params,boolean isJsonParam) {
237            Document sendRequest = sendRequestDocument(url, cookie, headers,params, "POST", isJsonParam);
238            return sendRequest;
239        }
240        
241        
242     /**
243         * *******************************获取网页对象--结束**************************************
244         * */
245        
246     
247     
248     
249     
250     
251     
252     /**
253      * post请求
254      * @param url         url地址
255      * @param jsonParam     参数
256      * @param noNeedResponse    不需要返回结果
257      * @return
258      */
259     private static String httpPost(String url,Map<String,String> headers,JSONObject jsonParam, boolean noNeedResponse){
260         //post请求返回结果
261         CloseableHttpClient httpClient = HttpClients.createDefault();
262 //        DefaultHttpClient httpClient = new DefaultHttpClient();
263         String jsonResult = null;
264         HttpPost method = new HttpPost(url);
265         
266         if(headers!=null){
267             Set<String> keySet = headers.keySet();
268             for (String key : keySet) {
269                 method.addHeader(key, headers.get(key));
270             }
271         }
272         try {
273             if (null != jsonParam) {
274                 //解决中文乱码问题
275                 StringEntity entity = new StringEntity(jsonParam.toString(), "UTF-8");
276                 entity.setContentEncoding("UTF-8");
277                 entity.setContentType("application/json");
278                 method.setEntity(entity);
279             }
280             HttpResponse result = httpClient.execute(method);
281             url = URLDecoder.decode(url, "UTF-8");
282             /**请求发送成功,并得到响应**/
283             //if (result.getStatusLine().getStatusCode() == 200) {
284                 try {
285                     /**读取服务器返回过来的json字符串数据**/
286                     jsonResult = EntityUtils.toString(result.getEntity());
287                     if (noNeedResponse) {
288                         return null;
289                     }
290                     /**把json字符串转换成json对象**/
291                 } catch (Exception e) {
292                     logger.error("HttpPost请求提交失败:" + url, e);
293                 }
294             //}
295         } catch (Exception e) {
296             logger.error("HttpPost请求提交失败:" + url, e);
297         }
298         return jsonResult;
299     }
300  
301  
302     /**
303      * 发送get请求
304      * @param url    路径
305      * @return
306      */
307     private static String httpGet(String url,Map<String,String> headers){
308         //get请求返回结果
309         String jsonResult = null;
310         try {
311             CloseableHttpClient httpClient = HttpClients.createDefault();
312 //            DefaultHttpClient client = new DefaultHttpClient();
313             //发送get请求
314             HttpGet request = new HttpGet(url);
315             if(headers!=null){
316                 Set<String> keySet = headers.keySet();
317                 for (String key : keySet) {
318                     request.addHeader(key, headers.get(key));
319                 }
320             }
321             
322             HttpResponse response = httpClient.execute(request);
323  
324             /**请求发送成功,并得到响应**/
325             if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
326                 /**读取服务器返回过来的json字符串数据**/
327                 jsonResult = EntityUtils.toString(response.getEntity());
328             } else {
329                 logger.error("HttpGet请求提交失败:" + url);
330             }
331         } catch (Exception e) {
332             logger.error("HttpGet请求提交失败:" + url, e);
333         }
334         return jsonResult;
335     }
336     
337     /**
338      * 发送get请求
339      * @param url 路径
340      * @return
341      */
342     private static String httpDelete(String url,Map<String,String> headers){
343         //get请求返回结果
344         String jsonResult = null;
345         try {
346             CloseableHttpClient httpClient = HttpClients.createDefault();
347 //            DefaultHttpClient client = new DefaultHttpClient();
348             //发送get请求
349             HttpDelete request = new HttpDelete(url);
350             if(headers!=null){
351                 Set<String> keySet = headers.keySet();
352                 for (String key : keySet) {
353                     request.addHeader(key, headers.get(key));
354                 }
355             }
356             
357             HttpResponse response = httpClient.execute(request);
358             
359             /**请求发送成功,并得到响应**/
360             if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
361                 /**读取服务器返回过来的json字符串数据**/
362                 jsonResult = EntityUtils.toString(response.getEntity());
363             } else {
364                 logger.error("HttpDelete请求提交失败:" + url);
365             }
366         } catch (Exception e) {
367             logger.error("HttpDelete请求提交失败:" + url, e);
368         }
369         return jsonResult;
370     }
371     
372     /**
373      * 向指定URL发送GET方法的请求
374      * @param url 发送请求的URL
375      * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
376      * @return URL 所代表远程资源的响应结果
377      */
378     private static String sendGet(String url, String param) {
379         
380         String result = "";
381         BufferedReader in = null;
382         try {
383             String urlNameString = url + "?" + param;
384             URL realUrl = new URL(urlNameString);
385             // 打开和URL之间的连接
386             URLConnection connection = realUrl.openConnection();
387             // 设置通用的请求属性
388             connection.setRequestProperty("accept", "*/*");
389             connection.setRequestProperty("connection", "Keep-Alive");
390             connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
391             // 建立实际的连接
392             connection.connect();
393             // 获取所有响应头字段
394             // Map<String, List<String>> map = connection.getHeaderFields();
395             // 遍历所有的响应头字段
396             // 定义 BufferedReader输入流来读取URL的响应
397             in = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8"));
398             String line;
399             while ((line = in.readLine()) != null) {
400                 result += line;
401             }
402         } catch (Exception e) {
403             System.out.println("发送GET请求出现异常!" + e);
404             e.printStackTrace();
405         }
406         // 使用finally块来关闭输入流
407         finally {
408             try {
409                 if (in != null) {
410                     in.close();
411                 }
412             } catch (Exception e2) {
413                 e2.printStackTrace();
414             }
415         }
416         return result;
417     }
418     
419 
420     /**
421      * 向指定 URL 发送POST方法的请求
422      * @param url 发送请求的 URL
423      * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
424      * @return 所代表远程资源的响应结果
425      */
426     private static String sendPost(String url, String param) {
427         
428         PrintWriter out = null;
429         BufferedReader in = null;
430         String result = "";
431         try {
432             URL realUrl = new URL(url);
433             // 打开和URL之间的连接
434             URLConnection conn = realUrl.openConnection();
435             // 设置通用的请求属性
436             conn.setRequestProperty("accept", "*/*");
437             conn.setRequestProperty("connection", "Keep-Alive");
438             conn.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
439             // 发送POST请求必须设置如下两行
440             conn.setDoOutput(true);
441             conn.setDoInput(true);
442             // 获取URLConnection对象对应的输出流
443             out = new PrintWriter(conn.getOutputStream());
444             // 发送请求参数
445             out.print(param);
446             // flush输出流的缓冲
447             out.flush();
448             // 定义BufferedReader输入流来读取URL的响应
449             in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
450             String line;
451             while ((line = in.readLine()) != null) {
452                 result += line;
453             }
454         } catch (Exception e) {
455             System.out.println("发送 POST 请求出现异常!"+e);
456             e.printStackTrace();
457         }finally{
458             try{
459                 if(out!=null){
460                     out.close();
461                 }
462                 if(in!=null){
463                     in.close();
464                 }
465             }catch(Exception ex){
466                 ex.printStackTrace();
467             }
468         }
469         return result;
470     }
471    
472     /** 发送 利用JSOUP 发送 Request 请求
473      * @param url 请求地址: String
474      * @param cookie cookie:String
475      * @param headers 头信息:Map<String,String>
476      * @param params 参数:Map<String,Object>
477      * @param method 请求方式: String,GER OR POST
478      * @return String
479      */
480     private static String sendRequest(String url,String cookie,Map<String,String> headers,Map<String,Object> params,String method,boolean isJsonParam) {
481         String res = null;
482         try {
483             Document document = null;
484             Connection connect = getConnection(url, cookie, headers, params,isJsonParam);
485             connect.ignoreContentType(true);
486             if("GET".equals(method)) {
487                 document = connect.get();
488             }else if("POST".equals(method)) {
489                 document = connect.post();
490             }
491             
492             if(document != null) {
493                 res = document.body().text();
494             }
495         } catch (Exception e) {
496             e.printStackTrace();
497         }
498         return res;
499     }
500     
501     
502     /** 发送 利用JSOUP 发送 Request 请求
503      * @param url 请求地址: String
504      * @param cookie cookie:String
505      * @param headers 头信息:Map<String,String>
506      * @param params 参数:Map<String,Object>
507      * @param method 请求方式: String,GER OR POST
508      * @return String
509      */
510     private static Document sendRequestDocument(String url,String cookie,Map<String,String> headers,Map<String,Object> params,String method,boolean isJsonParam) {
511         Document document = null;
512         try {
513             Connection connect = getConnection(url, cookie, headers, params,isJsonParam);
514             connect.ignoreContentType(true);
515             if("GET".equals(method)) {
516                 document = connect.get();
517             }else if("POST".equals(method)) {
518                 document = connect.post();
519             }
520         } catch (Exception e) {
521             e.printStackTrace();
522         }
523         return document;
524     }
525     
526     /** 
527      * 获取JSOUP Connection
528      * @param url 请求地址: String
529      * @param cookie cookie:String
530      * @param headers 头信息:Map<String,String>
531      * @param params 头信息:Map<String,Object>
532      * @return Connection org.jsoup.Connection
533      */
534     private static Connection getConnection(String url,String cookie,Map<String,String> headers,Map<String,Object> params,boolean isJsonParam) {
535         Connection connect = Jsoup.connect(url);
536         if(cookie != null) {
537             connect.header("Cookie", cookie);
538         }
539         
540         if(params != null) {
541             Map<String,String> param = new HashMap<String,String>();
542             Set<String> keySet = params.keySet();
543             for (String key : keySet) {
544                 if(params.get(key) != null){
545                     param.put(key, params.get(key)==null ? null : params.get(key)+"");
546                 }
547             }
548             if(isJsonParam){
549                 connect.requestBody(new JSONObject(param).toString());
550 //                connect.postDataCharset(new JSONObject(param).toString());
551             }else{
552                 
553                 connect.data(param);
554             }
555         }
556         
557         if(headers!= null) {
558             connect.headers(headers);
559         }else if(headers==null) {
560             connect.header("Accept", "application/json");
561             connect.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0");
562         }
563         return connect;
564     }
565     

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:springboot集成调用Azkaban

下一篇:微信消息模板发送