Post&&Get实现

网友投稿 234 2022-11-21

Post&&Get实现

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 params) { 2 String = ""; 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 = HttpClients.createDefault(); 16 CloseableHttpResponse response = null; 17 try { 18 // 创建httpGet 19 HttpGet = new HttpGet(url); 20 // 执行get请求. 21 response = httpClient.execute(httpGet); 22 // 获取响应实体 23 HttpEntity entity = response.getEntity(); 24 if (entity != null) { 25 = 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 = ""; CloseableHttpClient = HttpClients.createDefault(); 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) { = 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 params) { String = ""; CloseableHttpClient = HttpClients.createDefault(); HttpPost = new HttpPost(url); CloseableHttpResponse response = null; try { List pairList = new ArrayList(); for (Map.Entry entry : params.entrySet()) { NameValuePair pair = new BasicNameValuePair(entry.getKey(), entry.getValue().toString()); pairList.add(pair); } UrlEncodedFormEntity(pairList, Charset .forName("UTF-8"))); response = httpClient.execute(httpPost); // 获取响应实体 HttpEntity entity = response.getEntity(); if (entity != null) { = EntityUtils.toString(entity, "UTF-8"); } } catch (Exception e) { e.printStackTrace(); } finally { // 关闭连接,释放资源 try { response.close(); httpClient.close(); } catch (Exception e) { e.printStackTrace(); } } return httpStr; }

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:中国移动再次入围A类企业并对未来发展提出了5G+计划
下一篇:深入理解Java main方法详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~