apache httpclient新版本的get/post请求
2017-10-26 16:18
495 查看
直接撸代码
public final class HttpUtils { private static final Logger logger = LoggerFactory.getLogger(HttpUtils.class); private HttpUtils(){} /** * get请求 * @param url * @param headers * @return */ public static String get(String url, Map<String,String> headers) { HttpGet httpGet = new HttpGet(url); headers.entrySet().forEach(entry -> httpGet.addHeader(entry.getKey(), entry.getValue())); return executeRequest(httpGet); } /** * get请求 * @param url * @param headers * @param params 参数 * @return */ public static String get(String url, Map<String,String> headers, Map<String,Object> params) { StringBuffer sb = new StringBuffer(); params.entrySet().forEach(entry -> sb.append("&").append(entry.getKey()).append("=").append(entry.getValue())); if (url.indexOf("?") == -1) { url = url + "?" + sb.toString().replaceFirst("&", ""); } else { url = url + sb.toString(); } HttpGet httpGet = new HttpGet(url); headers.entrySet().forEach(entry -> httpGet.addHeader(entry.getKey(), entry.getValue())); return executeRequest(httpGet); } /** * post请求 * @param url * @param headers * @param params 参数 * @return */ public static String post(String url, Map<String,String> headers, Map<String,String> params) throws UnsupportedEncodingException { HttpPost httpPost = new HttpPost(url); headers.entrySet().forEach(entry -> httpPost.addHeader(entry.getKey(), entry.getValue())); // 创建参数队列 List<NameValuePair> nameValuePairs = new ArrayList<>(); params.entrySet().forEach(entry -> nameValuePairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()))); HttpEntity httpEntity = new UrlEncodedFormEntity(nameValuePairs, "UTF-8"); httpPost.setEntity(httpEntity); return executeRequest(httpPost); } /** * 执行请求 * @param request * @return */ public static String executeRequest(final HttpUriRequest request) { String result = ""; CloseableHttpClient httpClient = HttpClients.createDefault(); try { result = httpClient.execute(request, response -> { int statusCode = response.getStatusLine().getStatusCode(); if (statusCode >= 200 && statusCode < 300) { HttpEntity entity = response.getEntity(); return entity != null ? EntityUtils.toString(entity) : null; } else { throw new ClientProtocolException("Unexpected response status: " + statusCode); } }); } catch (IOException e) { e.printStackTrace(); logger.error("url: {}, request failed!", request.getURI().getHost() + request.getURI().getPath()); logger.error("",e); } finally { closeHttpClient(httpClient); } return result; } public static void closeHttpClient(CloseableHttpClient httpClient) { try { httpClient.close(); } catch (IOException e) { logger.error("",e); } } }
相关文章推荐
- org.apache.http.client.HttpClient get/post请求
- HttpClient4.x进行Get/Post请求并使用ResponseHandler处理响应
- AsyncHttpClient 、HttpURLConnection get/post请求、httpClient
- 【实战】(二)android模拟qq登录(apache的httpClient发送get和post请求)
- httpclient的get/post请求
- Android使用Apache HttpClient发送GET、POST请求
- JAVA中使用Apache HttpComponents Client的进行GET/POST请求使用案例
- 使用apache的httpclient发送post请求传送和解析json
- Java用org.apache.http.client的HttpClient发送Post请求
- Java--org.apache.http.client的HttpClient发送Post请求,获取返回Header
- HttpClient4.x进行Get/Post请求并使用ResponseHandler处理响应
- Java HttpPost请求基于apache的httpclient
- java apache commons HttpClient发送get和post请求的学习整理(转)
- Java用org.apache.http.client的HttpClient发送Post请求 可获取返回Header
- Java用org.apache.http.client的HttpClient发送Post请求 可获取返回Header
- Apache 大三方法HTTP请求链接响应之HttpClient的GET和POST工具类封装
- java apache commons HttpClient发送get和post请求的学习整理
- java apache commons HttpClient发送get和post请求的学习整理
- java apache commons HttpClient发送get和post请求的学习整理
- android使用apache httpclient发送post请求