简单封装的httpclient4请求类
2015-08-09 22:13
561 查看
项目中经常会用到跨项目之间的请求,这时候我们经常采用的httpclient,由于大多数情况下并用不到一些非常复杂的功能,所以这里我将一些基本的请求封装了一下,以方便日常开发中使用。
以下类实现了如果重新new 一个Class的情况下,多次请求使用的是一个session。
以下类实现了如果重新new 一个Class的情况下,多次请求使用的是一个session。
package util.http; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.http.Header; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.CookieStore; 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.cookie.Cookie; import org.apache.http.impl.client.AbstractHttpClient; import org.apache.http.impl.client.BasicCookieStore; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.cookie.BasicClientCookie; import org.apache.http.message.BasicHeader; import org.apache.http.message.BasicNameValuePair; import org.apache.http.protocol.BasicHttpContext; import org.apache.http.protocol.HttpContext; import org.apache.http.util.EntityUtils; import util.common.MapUtil; public class SimpleHttpClient { private HttpClientBuilder httpclientBuilder; private CloseableHttpClient httpclient; private HttpContext context; private Header[] responseHeaders; public SimpleHttpClient() { //httpclient = HttpClients.createDefault(); httpclientBuilder = HttpClients.custom(); httpclient = httpclientBuilder.build(); context = new BasicHttpContext(); } @SuppressWarnings("unchecked") private List<BasicNameValuePair> toNameValuePairList(Map<String, String> m) { List<BasicNameValuePair> nameValuePairList = new ArrayList<BasicNameValuePair>(); List<String> keylist = MapUtil.keyList(m); for (String key : keylist) { nameValuePairList.add(new BasicNameValuePair(key, m.get(key))); } return nameValuePairList; } public String get(String url) throws ClientProtocolException, IOException { HttpGet httpget = new HttpGet(url); CloseableHttpResponse response = httpclient.execute(httpget, context); responseHeaders = response.getAllHeaders(); String result = EntityUtils.toString(response.getEntity()); response.close(); return result; } public String post(String url) throws ClientProtocolException, IOException { HttpPost httppost = new HttpPost(url); CloseableHttpResponse response = httpclient.execute(httppost, context); responseHeaders = response.getAllHeaders(); String result = EntityUtils.toString(response.getEntity()); response.close(); return result; } public String post(String url, Header[] headers) throws ClientProtocolException, IOException { HttpPost httppost = new HttpPost(url); for (Header header : headers) { httppost.setHeader(header); } CloseableHttpResponse response = httpclient.execute(httppost, context); responseHeaders = response.getAllHeaders(); String result = EntityUtils.toString(response.getEntity()); response.close(); return result; } public String post(String url, Map<String, String> params) throws ClientProtocolException, IOException { HttpPost httppost = new HttpPost(url); List<BasicNameValuePair> nameValuePairList = toNameValuePairList(params); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairList,"UTF-8")); CloseableHttpResponse response = httpclient.execute(httppost, context); responseHeaders = response.getAllHeaders(); String result = EntityUtils.toString(response.getEntity()); response.close(); return result; } public String post(String url, List<Header> headers, Map<String, String> params) throws ClientProtocolException, IOException { httpclient = httpclientBuilder.disableCookieManagement().setDefaultHeaders(headers).build(); HttpPost httppost = new HttpPost(url); List<BasicNameValuePair> nameValuePairList = toNameValuePairList(params); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairList,"UTF-8")); CloseableHttpResponse response = httpclient.execute(httppost, context); responseHeaders = response.getAllHeaders(); String result = EntityUtils.toString(response.getEntity()); response.close(); return result; } public InputStream execute(String url) throws ClientProtocolException, IOException { HttpPost httppost = new HttpPost(url); CloseableHttpResponse response = httpclient.execute(httppost, context); responseHeaders = response.getAllHeaders(); InputStream is = response.getEntity().getContent(); return is; } public List<Header> getResponseHeader(String name) { List<Header> hs = new ArrayList<Header>(); for (Header header : responseHeaders) { String n = header.getName(); //String v = header.getValue(); if (n.equals(name)) { hs.add(header); } } return hs; } public void close() throws IOException { this.httpclient.close(); } public CloseableHttpClient getHttpclient() { return httpclient; } public HttpContext getContext() { return context; } public Header[] getResponseHeaders() { return responseHeaders; } public HttpClientBuilder getHttpclientBuilder() { return httpclientBuilder; } //示例 @SuppressWarnings("unused") private static void main(String[] args) throws ClientProtocolException, IOException { SimpleHttpClient hc = new SimpleHttpClient(); String url = "http://xxxxxxxxxxxxxxxxxx"; Map<String, String> params = new HashMap<String, String>(); params.put("phone", "1380003333"); params.put("captcha", "abcde"); String result = hc.post(url, params); System.out.println(result); } }
相关文章推荐
- TCP/IP 网络子系统 在Linux 内核中实现详解 (基于2.6.35版)
- SongTaste闭站谢客 网络音乐731大限已至
- 通过windows中间层过滤驱动修改接收数据包的内容
- TCP连接时需要进行三次握手?
- Mac下安装npm,http-server,安装虚拟服务器
- BP神经网络模型与学习算法
- TCP/IP解析(一):TCP/IP的工作方式
- Android网络编程
- android 网络框架Android_Async_Http 初识
- 计算机网络之面试常考
- 【费用流】 COGS 461. [网络流24题] 餐巾
- android 网络框架 volley 初识
- Socket 通信原理(Android客户端和服务器以TCP&&UDP方式互通)
- CCSDS空间网络互连
- HttpListener 实现web服务端
- tcp/ip学习笔记
- TCP如何区分已连接套接字和已请求连接字
- 从WebView到http协议
- 【网络流】 POJ 2125 Destroying The Graph
- TCP/IP协议学习之四(DNS协议)