HttpClient的get和post请求
import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import org.apache.http.ParseException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; import java.util.Map; /** * HTTP请求远程API 辅助工具 * @author kongweiteng * @version 2017-04-10 */ @Component public class HttpUtil{ protected static Logger logger = LoggerFactory.getLogger(HttpUtil.class);//日志工具 /** * 功能:get请求 * @param url 请求地址以及请求参数 * @return text 返回文本 */ public static String get(String url){ HttpClient client = HttpClients.createDefault();// 创建一个默认httpclient客户端 HttpGet get = new HttpGet(url);// 创建一个get请求 String text = ""; try { HttpResponse response = client.execute(get);// 用客户端去执行get请求得到一个响应 HttpEntity entity = response.getEntity();// 取出响应实体 text = EntityUtils.toString(entity); } catch (ClientProtocolException e) { logger.error("http工具类执行get请求时出现ClientProtocolException异常!"); e.printStackTrace(); } catch (ParseException e) { logger.error("http工具类执行get请求时出现ParseException异常!"); e.printStackTrace(); } catch (IOException e) { logger.error("http工具类执行get请求时出现IOException异常!"); e.printStackTrace(); } finally { get.releaseConnection();// 释放连接 } return text; } /** * 功能:post请求 * @param url 请求路径 * @param map 参数名值对 * @return 返回文本 */ public static String post(String url, Map<String, String> map) { HttpClient client = HttpClients.createDefault();// 创建一个默认httpclient客户端 List<NameValuePair> paramsList = new ArrayList<NameValuePair>();// 定义名值对类型的list for (Map.Entry<String, String> entry : map.entrySet()) { paramsList.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } String text = ""; HttpPost post = new HttpPost(url);// 创建一个post请求 try { HttpEntity httpEntity = new UrlEncodedFormEntity(paramsList, "utf-8");// 把list放到http请求实体中 post.setEntity(httpEntity);// 把请求实体放到post请求中 HttpResponse response = client.execute(post);// 用客户端去执行post请求得到一个响应 HttpEntity entity = response.getEntity();// 取出响应实体 text = EntityUtils.toString(entity); } catch (UnsupportedEncodingException e) { logger.error("http工具类执行post请求时出现UnsupportedEncodingException异常!"); e.printStackTrace(); } catch (ClientProtocolException e) { logger.error("http工具类执行post请求时出现ClientProtocolException异常!"); e.printStackTrace(); } catch (ParseException e) { logger.error("http工具类执行post请求时出现ParseException异常!"); e.printStackTrace(); } catch (IOException e) { logger.error("http工具类执行post请求时出现IOException异常!"); e.printStackTrace(); } finally { post.releaseConnection(); } return text; } }
街道任务要求写一个远程调用API数据接口,基于HTTP请求下,就考虑到使用httpclient的get和post请求,只是前期代码中没有考虑请求超时或者链接超时的问题,后期根据具体情况进行配置。