欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

HttpClient请求工具类

程序员文章站 2022-04-08 11:51:04
以上所需的maven依赖如下: ......
  1 package com.yangche.utils;
  2 
  3 import org.apache.http.namevaluepair;
  4 import org.apache.http.client.clientprotocolexception;
  5 import org.apache.http.client.entity.urlencodedformentity;
  6 import org.apache.http.client.methods.*;
  7 import org.apache.http.client.utils.uribuilder;
  8 import org.apache.http.entity.contenttype;
  9 import org.apache.http.entity.stringentity;
 10 import org.apache.http.impl.client.closeablehttpclient;
 11 import org.apache.http.impl.client.httpclients;
 12 import org.apache.http.message.basicnamevaluepair;
 13 import org.apache.http.util.entityutils;
 14 import org.springframework.util.stringutils;
 15 
 16 import java.io.ioexception;
 17 import java.io.unsupportedencodingexception;
 18 import java.net.uri;
 19 import java.net.urisyntaxexception;
 20 import java.util.arraylist;
 21 import java.util.list;
 22 import java.util.map;
 23 
 24 public class httpclientutil {
 25     public static string doget(string url, map<string,object> param){
 26         //创建httpclient对象
 27         closeablehttpclient httpclient = httpclients.createdefault();
 28         string resultstring="";
 29         closeablehttpresponse response=null;
 30         try {
 31             uribuilder builder=new uribuilder(url);
 32             if(param!=null){
 33                 for (string key:param.keyset()){
 34                     if(param.get(key) instanceof list){
 35                         list list=(list)param.get(key);
 36                         for (object listring:list){
 37                             builder.addparameter(key,string.valueof(listring));
 38                         }
 39                     }else {
 40                         builder.addparameter(key,string.valueof(param.get(key)));
 41                     }
 42                 }
 43             }
 44             uri uri=builder.build();
 45             //创建httpget请求
 46             httpget httpget=new httpget(uri);
 47             //执行请求
 48             response=httpclient.execute(httpget);
 49             resultstring= entityutils.tostring(response.getentity(),"utf-8");
 50         } catch (urisyntaxexception e) {
 51             e.printstacktrace();
 52         } catch (clientprotocolexception e) {
 53             e.printstacktrace();
 54         } catch (ioexception e) {
 55             e.printstacktrace();
 56         }finally {
 57             try {
 58                 if(response!=null){
 59                     response.close();
 60                 }
 61                 httpclient.close();
 62             } catch (ioexception e) {
 63                 e.printstacktrace();
 64             }
 65         }
 66         return resultstring;
 67     }
 68     public static string doget(string url){
 69         return doget(url,null);
 70     }
 71     public static string dopost(string url,map<string,string> param){
 72         //创建httpclient对象
 73         closeablehttpclient httpclient = httpclients.createdefault();
 74         closeablehttpresponse response=null;
 75         string resultstring="";
 76         //创建httppost请求
 77         httppost httppost = new httppost(url);
 78         //创建参数列表
 79         if(param!=null){
 80             list<namevaluepair> paramlist=new arraylist<>();
 81             for (string key:param.keyset()){
 82                 paramlist.add(new basicnamevaluepair(key,param.get(key)));
 83             }
 84             try {
 85                 //模拟表单
 86                 urlencodedformentity entity=new urlencodedformentity(paramlist);
 87                 httppost.setentity(entity);
 88                 //执行http请求
 89                 response=httpclient.execute(httppost);
 90                 resultstring=entityutils.tostring(response.getentity(),"utf-8");
 91             } catch (unsupportedencodingexception e) {
 92                 e.printstacktrace();
 93             } catch (clientprotocolexception e) {
 94                 e.printstacktrace();
 95             } catch (ioexception e) {
 96                 e.printstacktrace();
 97             }finally {
 98                 try {
 99                     response.close();
100                 } catch (ioexception e) {
101                     e.printstacktrace();
102                 }
103             }
104         }
105         return resultstring;
106     }
107     public static string dopost(string url){
108         return dopost(url,null);
109     }
110 
111     /**
112      * restful请求
113      * @param url
114      * @param requesttype 请求类型:post、delete、patch
115      * @param json
116      * @return
117      */
118     public static string dorequest(string url,string requesttype,string json){
119         //创建httpclient对象
120         closeablehttpclient httpclient=httpclients.createdefault();
121         closeablehttpresponse response=null;
122         string resultstring="";
123         try {
124             //创建不同类型的请求
125             httppost httppost=new httppost(url);
126             httpdeletewithbody httpdelete =new httpdeletewithbody(url);
127             httppatch httppatch=new httppatch(url);
128             //创建请求内容
129             stringentity entity=new stringentity(json, contenttype.application_json);
130             //执行http请求
131             if(!stringutils.isempty(requesttype)&&requesttype.equalsignorecase("post")){
132                 httppost.setentity(entity);
133                 response=httpclient.execute(httppost);
134             }else if(!stringutils.isempty(requesttype)&&requesttype.equalsignorecase("delete")){
135                 httpdelete.setentity(entity);
136                 response=httpclient.execute(httpdelete);
137             }else if(!stringutils.isempty(requesttype)&&requesttype.equalsignorecase("patch")){
138                 httppatch.setentity(entity);
139                 response=httpclient.execute(httppatch);
140             }
141             resultstring=entityutils.tostring(response.getentity(),"utf-8");
142         } catch (ioexception e) {
143             e.printstacktrace();
144         } finally {
145             try {
146                 response.close();
147             } catch (ioexception e) {
148                 e.printstacktrace();
149             }
150         }
151     }
152 
153     private static class httpdeletewithbody extends httpentityenclosingrequestbase {
154         public static final string method_name="delete";
155         @override
156         public string getmethod() {
157             return method_name;
158         }
159         public httpdeletewithbody (final string uri){
160             super();
161             seturi(uri.create(uri));
162         }
163         public httpdeletewithbody (final uri uri){
164             super();
165             seturi(uri);
166         }
167         public httpdeletewithbody(){
168             super();
169         }
170     }
171 }

 以上所需的maven依赖如下:

 1 <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
 2 <dependency>
 3     <groupid>org.apache.httpcomponents</groupid>
 4     <artifactid>httpcore</artifactid>
 5     <version>4.4.10</version>
 6 </dependency>
 7 
 8 <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
 9 <dependency>
10     <groupid>org.apache.httpcomponents</groupid>
11     <artifactid>httpclient</artifactid>
12     <version>4.5.6</version>
13 </dependency>