import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; import java.util.Map; import org.apache.http.HttpEntity; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils;
Get实现(Map)
1 public static String doGet(String url, Map<String, Object> params) { 2 String httpStr = ""; 3 StringBuffer param = new StringBuffer(); 4 int i = 0; 5 for (String key : params.keySet()) { 6 if (i == 0) { 7 param.append("?"); 8 } else { 9 param.append("&"); 10 } 11 param.append(key).append("=").append(params.get(key)); 12 i++; 13 } 14 url += param; 15 CloseableHttpClient httpClient = HttpClients.createDefault(); 16 CloseableHttpResponse response = null; 17 try { 18 // 创建httpGet 19 HttpGet httpGet = new HttpGet(url); 20 // 执行get请求. 21 response = httpClient.execute(httpGet); 22 // 获取响应实体 23 HttpEntity entity = response.getEntity(); 24 if (entity != null) { 25 httpStr = EntityUtils.toString(entity, "UTF-8"); 26 } 27 } catch (Exception e) { 28 e.printStackTrace(); 29 } finally { 30 // 关闭连接,释放资源 31 try { 32 response.close(); 33 httpClient.close(); 34 } catch (Exception e) { 35 e.printStackTrace(); 36 } 37 } 38 return httpStr; 39 }
Post实现(JSON)
public static String doPost(String url, String json) { String httpStr = ""; CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); CloseableHttpResponse response = null; try { StringEntity stringEntity = new StringEntity(json,"UTF-8");// 解决中文乱码问题 stringEntity.setContentEncoding("UTF-8"); stringEntity.setContentType("application/json"); httpPost.setEntity(stringEntity); response = httpClient.execute(httpPost); // 获取响应实体 HttpEntity entity = response.getEntity(); if (entity != null) { httpStr = EntityUtils.toString(entity, "UTF-8"); } } catch (Exception e) { e.printStackTrace(); } finally { // 关闭连接,释放资源 try { response.close(); httpClient.close(); } catch (Exception e) { e.printStackTrace(); } } return httpStr; }
Post实现(Map)
public static String doPost(String url, Map<String, Object> params) { String httpStr = ""; CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); CloseableHttpResponse response = null; try { List<NameValuePair> pairList = new ArrayList<NameValuePair>(); for (Map.Entry<String, Object> entry : params.entrySet()) { NameValuePair pair = new BasicNameValuePair(entry.getKey(), entry.getValue().toString()); pairList.add(pair); } httpPost.setEntity(new UrlEncodedFormEntity(pairList, Charset .forName("UTF-8"))); response = httpClient.execute(httpPost); // 获取响应实体 HttpEntity entity = response.getEntity(); if (entity != null) { httpStr = EntityUtils.toString(entity, "UTF-8"); } } catch (Exception e) { e.printStackTrace(); } finally { // 关闭连接,释放资源 try { response.close(); httpClient.close(); } catch (Exception e) { e.printStackTrace(); } } return httpStr; }