1 packagecom.xxx.common.util;2
3 importjava.io.IOException;4 importjava.net.URI;5 importjava.util.ArrayList;6 importjava.util.List;7 importjava.util.Map;8 importorg.apache.http.NameValuePair;9 importorg.apache.http.client.entity.UrlEncodedFormEntity;10 importorg.apache.http.client.methods.CloseableHttpResponse;11 importorg.apache.http.client.methods.HttpGet;12 importorg.apache.http.client.methods.HttpPost;13 importorg.apache.http.client.utils.URIBuilder;14 importorg.apache.http.entity.ContentType;15 importorg.apache.http.entity.StringEntity;16 importorg.apache.http.impl.client.CloseableHttpClient;17 importorg.apache.http.impl.client.HttpClients;18 importorg.apache.http.message.BasicNameValuePair;19 importorg.apache.http.util.EntityUtils;20
21 public classHttpClientUtil {22
23 public static String doGet(String url, Mapparam) {24 //创建Httpclient对象
25 CloseableHttpClient httpclient =HttpClients.createDefault();26 String resultString = "";27 CloseableHttpResponse response = null;28 try{29 //创建uri
30 URIBuilder builder = newURIBuilder(url);31 if (param != null) {32 for(String key : param.keySet()) {33 builder.addParameter(key, param.get(key));34 }35 }36 URI uri =builder.build();37
38 //创建http GET请求
39 HttpGet httpGet = newHttpGet(uri);40
41 //执行请求
42 response =httpclient.execute(httpGet);43 //判断返回状态是否为200
44 if (response.getStatusLine().getStatusCode() == 200) {45 resultString = EntityUtils.toString(response.getEntity(), "UTF-8");46 }47 } catch(Exception e) {48 e.printStackTrace();49 } finally{50 try{51 if (response != null) {52 response.close();53 }54 httpclient.close();55 } catch(IOException e) {56 e.printStackTrace();57 }58 }59 returnresultString;60 }61
62 public staticString doGet(String url) {63 return doGet(url, null);64 }65
66 public static String doPost(String url, Mapparam) {67 //创建Httpclient对象
68 CloseableHttpClient httpClient =HttpClients.createDefault();69 CloseableHttpResponse response = null;70 String resultString = "";71 try{72 //创建Http Post请求
73 HttpPost httpPost = newHttpPost(url);74 //创建参数列表
75 if (param != null) {76 List paramList = new ArrayList<>();77 for(String key : param.keySet()) {78 paramList.add(newBasicNameValuePair(key, param.get(key)));79 }80 //模拟表单
81 UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList, "utf-8");82 httpPost.setEntity(entity);83 }84 //执行http请求
85 response =httpClient.execute(httpPost);86 resultString = EntityUtils.toString(response.getEntity(), "utf-8");87 } catch(Exception e) {88 e.printStackTrace();89 } finally{90 try{91 response.close();92 } catch(IOException e) {93 e.printStackTrace();94 }95 }96 returnresultString;97 }98
99 public staticString doPost(String url) {100 return doPost(url, null);101 }102
103 /**
104 * 请求的参数类型为json105 *@paramurl106 *@paramjson107 *@return
108 * {username:"",pass:""}109 */
110 public staticString doPostJson(String url, String json) {111 //创建Httpclient对象
112 CloseableHttpClient httpClient =HttpClients.createDefault();113 CloseableHttpResponse response = null;114 String resultString = "";115 try{116 //创建Http Post请求
117 HttpPost httpPost = newHttpPost(url);118 //创建请求内容
119 StringEntity entity = newStringEntity(json, ContentType.APPLICATION_JSON);120 httpPost.setEntity(entity);121 //执行http请求
122 response =httpClient.execute(httpPost);123 resultString = EntityUtils.toString(response.getEntity(), "utf-8");124 } catch(Exception e) {125 e.printStackTrace();126 } finally{127 try{128 response.close();129 } catch(IOException e) {130 e.printStackTrace();131 }132 }133 returnresultString;134 }135 }