1 package com.xxx.common.util; 2 3 import java.io.IOException; 4 import java.net.URI; 5 import java.util.ArrayList; 6 import java.util.List; 7 import java.util.Map; 8 import org.apache.http.NameValuePair; 9 import org.apache.http.client.entity.UrlEncodedFormEntity; 10 import org.apache.http.client.methods.CloseableHttpResponse; 11 import org.apache.http.client.methods.HttpGet; 12 import org.apache.http.client.methods.HttpPost; 13 import org.apache.http.client.utils.URIBuilder; 14 import org.apache.http.entity.ContentType; 15 import org.apache.http.entity.StringEntity; 16 import org.apache.http.impl.client.CloseableHttpClient; 17 import org.apache.http.impl.client.HttpClients; 18 import org.apache.http.message.BasicNameValuePair; 19 import org.apache.http.util.EntityUtils; 20 21 public class HttpClientUtil { 22 23 public static String doGet(String url, Map<String, String> param) { 24 // 创建Httpclient对象 25 CloseableHttpClient httpclient = HttpClients.createDefault(); 26 String resultString = ""; 27 CloseableHttpResponse response = null; 28 try { 29 // 创建uri 30 URIBuilder builder = new URIBuilder(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 = new HttpGet(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 return resultString; 60 } 61 62 public static String doGet(String url) { 63 return doGet(url, null); 64 } 65 66 public static String doPost(String url, Map<String, String> param) { 67 // 创建Httpclient对象 68 CloseableHttpClient httpClient = HttpClients.createDefault(); 69 CloseableHttpResponse response = null; 70 String resultString = ""; 71 try { 72 // 创建Http Post请求 73 HttpPost httpPost = new HttpPost(url); 74 // 创建参数列表 75 if (param != null) { 76 List<NameValuePair> paramList = new ArrayList<>(); 77 for (String key : param.keySet()) { 78 paramList.add(new BasicNameValuePair(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 return resultString; 97 } 98 99 public static String doPost(String url) { 100 return doPost(url, null); 101 } 102 103 /** 104 * 请求的参数类型为json 105 * @param url 106 * @param json 107 * @return 108 * {username:"",pass:""} 109 */ 110 public static String 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 = new HttpPost(url); 118 // 创建请求内容 119 StringEntity entity = new StringEntity(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 return resultString; 134 } 135 }