HttpClientUtil请求http地址的工具类
2016-09-07 17:32
495 查看
直接贴代码:
import java.io.IOException; import java.util.HashMap; import java.util.Map; import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.config.RequestConfig; 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.util.EntityUtils; /** * HttpClientUtil */ public class HttpClientUtil { // 连接主机超时(30s) public static final int HTTP_CONNECT_TIMEOUT_30S = 30 * 1000; // 从主机读取数据超时(3min) public static final int HTTP_READ_TIMEOUT_3MIN = 180 * 1000; /** * httpPost */ public static String httpPost(String url, String jsonParam) throws ClientProtocolException, IOException { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); // 设置请求头和请求参数 if (null != jsonParam && !jsonParam.isEmpty()) { StringEntity entity = new StringEntity(jsonParam, "utf-8"); entity.setContentEncoding("UTF-8"); entity.setContentType("application/json"); httpPost.setEntity(entity); } // 超时时间设置 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(HTTP_READ_TIMEOUT_3MIN) .setConnectTimeout(HTTP_CONNECT_TIMEOUT_30S).build(); httpPost.setConfig(requestConfig); // 发送请求 CloseableHttpResponse response = httpclient.execute(httpPost); // 获取返回内容 try { HttpEntity entity = response.getEntity(); String str = EntityUtils.toString(entity); EntityUtils.consume(entity); // 此句关闭了流 return str; } finally { response.close(); } } /** * httpPost get Cookies */ public static Map<String, Object> httpPostGetCookies(String url, String jsonParam) throws ClientProtocolException, IOException { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); // 设置请求头和请求参数 if (null != jsonParam && !jsonParam.isEmpty()) { StringEntity entity = new StringEntity(jsonParam, "utf-8"); entity.setContentEncoding("UTF-8"); entity.setContentType("application/json"); httpPost.setEntity(entity); } // 超时时间设置 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(HTTP_READ_TIMEOUT_3MIN) .setConnectTimeout(HTTP_CONNECT_TIMEOUT_30S).build(); httpPost.setConfig(requestConfig); // 发送请求 CloseableHttpResponse response = httpclient.execute(httpPost); // 获取返回内容 try { HttpEntity entity = response.getEntity(); String str = EntityUtils.toString(entity); EntityUtils.consume(entity); // 此句关闭了流 // 获取数据内容 Map<String, Object> map = new HashMap<String, Object>(); map.put("result", str); // 获取返回到额Cookies Header[] headers = response.getHeaders("Set-Cookie"); map.put("cookies", headers); return map; } finally { response.close(); } } /** * httpGet */ public static String httpGet(String url) throws ClientProtocolException, IOException { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet(url); // 超时时间设置 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(HTTP_READ_TIMEOUT_3MIN) .setConnectTimeout(HTTP_CONNECT_TIMEOUT_30S).build(); httpGet.setConfig(requestConfig); // 发送请求 CloseableHttpResponse response = httpclient.execute(httpGet); // 获取返回内容 try { HttpEntity entity = response.getEntity(); String strResult = EntityUtils.toString(entity); return strResult; } finally { response.close(); } } /** * httpGet with Cookies */ public static String httpGetWithCookies(String url, Header[] headers) throws ClientProtocolException, IOException { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet(url); // 超时时间设置 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(HTTP_READ_TIMEOUT_3MIN) .setConnectTimeout(HTTP_CONNECT_TIMEOUT_30S).build(); httpGet.setConfig(requestConfig); // 设置请求头 if (headers != null && headers.length > 0) { httpGet.setHeaders(headers); } // 发送请求 CloseableHttpResponse response = httpclient.execute(httpGet); // 获取返回内容 try { HttpEntity entity = response.getEntity(); String strResult = EntityUtils.toString(entity); return strResult; } finally { response.close(); } } }
相关文章推荐
- C# HttpClientUtil 工具类 表单请求提交 附件上传下载
- HttpClientProxyUtil HTTPClient请求工具类源码
- android HttpClient get请求与post请求工具类
- Http请求工具类 httputil
- Http与HttpClient(已被废弃)通信的介绍,附带一个Http发送GET请求工具类的例子
- HttpClientUtil(HttpClient工具类)
- HttpClientUtil工具类
- httpclient post请求,需要登录的地址
- Http与HttpClient(已被废弃)通信的介绍,附带一个Http发送GET请求工具类的例子
- java http HttpClientUtil 工具类
- HttpClientUtil-用于后台发起请求
- Http请求工具类HttpUtil
- HttpClient (HTTP 请求工具类)
- 轻松把玩HttpClient之封装HttpClient工具类(五),携带Cookie的请求
- 常用工具类之HttpclientUtil
- android HttpClient get请求与post请求工具类
- Util:Http请求的工具类
- 轻松把玩HttpClient之封装HttpClient工具类(五),携带Cookie的请求
- HttpClientUtil发送json格式请求
- HttpClient与HttpURLConnection网络请求工具类