1 package com.*.utils; 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 * Created by Administrator on 2018/4/27. 22 */ 23 public class HttpClientUtil { 24 25 public static String doGet(String url, Map<String, String> param) { 26 27 // 创建Httpclient对象 28 CloseableHttpClient httpclient = HttpClients.createDefault(); 29 30 String resultString = ""; 31 CloseableHttpResponse response = null; 32 try { 33 // 创建uri 34 URIBuilder builder = new URIBuilder(url); 35 if (param != null) { 36 for (String key : param.keySet()) { 37 builder.addParameter(key, param.get(key)); 38 } 39 } 40 URI uri = builder.build(); 41 42 // 创建http GET请求 43 HttpGet httpGet = new HttpGet(uri); 44 45 // 执行请求 46 response = httpclient.execute(httpGet); 47 // 判断返回状态是否为200 48 if (response.getStatusLine().getStatusCode() == 200) { 49 resultString = EntityUtils.toString(response.getEntity(), "UTF-8"); 50 } 51 } catch (Exception e) { 52 e.printStackTrace(); 53 } finally { 54 try { 55 if (response != null) { 56 response.close(); 57 } 58 httpclient.close(); 59 } catch (IOException e) { 60 e.printStackTrace(); 61 } 62 } 63 return resultString; 64 } 65 66 public static String doGet(String url) { 67 return doGet(url, null); 68 } 69 70 public static String doPost(String url, Map<String, String> param) { 71 // 创建Httpclient对象 72 CloseableHttpClient httpClient = HttpClients.createDefault(); 73 CloseableHttpResponse response = null; 74 String resultString = ""; 75 try { 76 // 创建Http Post请求 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 // 模拟表单 85 UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList,"utf-8"); 86 httpPost.setEntity(entity); 87 } 88 // 执行http请求 89 response = httpClient.execute(httpPost); 90 resultString = EntityUtils.toString(response.getEntity(), "utf-8"); 91 } catch (Exception e) { 92 e.printStackTrace(); 93 } finally { 94 try { 95 response.close(); 96 } catch (IOException e) { 97 // TODO Auto-generated catch block 98 e.printStackTrace(); 99 } 100 } 101 102 return resultString; 103 } 104 105 public static String doPost(String url) { 106 return doPost(url, null); 107 } 108 109 public static String doPostJson(String url, String json) { 110 // 创建Httpclient对象 111 CloseableHttpClient httpClient = HttpClients.createDefault(); 112 CloseableHttpResponse response = null; 113 String resultString = ""; 114 try { 115 // 创建Http Post请求 116 HttpPost httpPost = new HttpPost(url); 117 // 创建请求内容 118 StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON); 119 httpPost.setEntity(entity); 120 // 执行http请求 121 response = httpClient.execute(httpPost); 122 resultString = EntityUtils.toString(response.getEntity(), "utf-8"); 123 } catch (Exception e) { 124 e.printStackTrace(); 125 } finally { 126 try { 127 response.close(); 128 } catch (IOException e) { 129 // TODO Auto-generated catch block 130 e.printStackTrace(); 131 } 132 } 133 134 return resultString; 135 } 136 }