HTTP Client工具类
HTTP Client工具类: import org.apache.http.Header;
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.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils; import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map; public class HTTPSample { private static CloseableHttpClient httpClient = null; public static CloseableHttpClient httpClient(){
if(httpClient!=null) return HTTPSample.httpClient;
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
// Increase max total connection to 200
cm.setMaxTotal(200);
// Increase default max connection per route to 20
cm.setDefaultMaxPerRoute(20);
HTTPSample.httpClient = HttpClients.custom().setConnectionManager(cm).build();
return httpClient;
} public static String httpGet(String url, Map<String, String> requestParams) { HttpGet httpGet = null;
String result = "";
try {
// ��������
StringBuilder builder = new StringBuilder(url);
builder.append("?");
for (Map.Entry<String, String> entry : requestParams.entrySet()) {
builder.append((String) entry.getKey());
builder.append("=");
builder.append((String) entry.getValue());
builder.append("&");
} String tmpUrl = builder.toString();
tmpUrl = tmpUrl.substring(0, tmpUrl.length() - 1); httpGet = new HttpGet(tmpUrl); // System.out.println("executing request " + httpGet.getURI());
// System.out.println("-------------------------------------"); HttpResponse response = httpClient().execute(httpGet); // reponse header
// System.out.println(response.getStatusLine().getStatusCode()); Header[] headers = response.getAllHeaders();
// for (Header header : headers) {
// System.out.println(header.getName() + ": " + header.getValue());
// } // System.out.println(); // ��ҳ����
HttpEntity httpEntity = response.getEntity();
// System.out.println(EntityUtils.toString(httpEntity));
result = EntityUtils.toString(httpEntity);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (httpGet != null) {
httpGet.abort();
}
}
return result;
} public static String httpPost(String url, Map<String, String> requestParams, String urlEncode) { HttpPost httpPost = null;
String result = "";
try {
// ��������
List<NameValuePair> params = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : requestParams.entrySet()) {
params.add(new BasicNameValuePair((String) entry.getKey(),
(String) entry.getValue()));
} httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params, urlEncode)); // System.out.println("executing request " + httpPost.getURI());
// System.out.println("-------------------------------------"); // reponse header
HttpResponse response = httpClient().execute(httpPost);
// System.out.println(response.getStatusLine().getStatusCode()); Header[] headers = response.getAllHeaders();
// for (Header header : headers) {
// System.out.println(header.getName() + ": " + header.getValue());
// } // System.out.println(); // ��ҳ����
HttpEntity httpEntity = response.getEntity();
result = EntityUtils.toString(httpEntity); } catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (httpPost != null) {
httpPost.abort();
}
}
return result;
}
}
HTTP Client工具类的更多相关文章
- Go/Python/Erlang编程语言对比分析及示例 基于RabbitMQ.Client组件实现RabbitMQ可复用的 ConnectionPool(连接池) 封装一个基于NLog+NLog.Mongo的日志记录工具类LogUtil 分享基于MemoryCache(内存缓存)的缓存工具类,C# B/S 、C/S项目均可以使用!
Go/Python/Erlang编程语言对比分析及示例 本文主要是介绍Go,从语言对比分析的角度切入.之所以选择与Python.Erlang对比,是因为做为高级语言,它们语言特性上有较大的相似性, ...
- C#工具类OracleHelper,基于Oracle.ManagedDataAccess.Client封装
基于Oracle.ManagedDataAccess.Client封装的Oracle工具类OracleHelper,代码如下: using System; using System.Data; usi ...
- MongoDBDao 工具类(包含分页取数据)
mongdb工具类 package e16wifi.statistic.com.mongodb; import java.util.ArrayList; import java.util.List; ...
- Rhino+envjs-1.2.js 在java运行网站js 工具类
java爬虫遇到个页面加密的东西,找了些资料学习学习 做了个java运行js的工具类,希望对大家有用,其中用到client(获取js)可以自行换成自己的client.主要是用了 Rhino就是Java ...
- HttpClient4.5 SSL访问工具类
要从网上找一个HttpClient SSL访问工具类太难了,原因是HttpClient版本太多了,稍有差别就不能用,最后笔者干脆自己封装了一个访问HTTPS并绕过证书工具类. 主要是基于新版本Http ...
- HttpClient_httpclient 4.3.1 post get的工具类
package com.ryx.util; import java.util.ArrayList; import java.util.List; import java.util.Map; impor ...
- Mina工具类v1.5
package com.cucpay.fundswap.util; import java.net.InetSocketAddress; import java.nio.charset.Charset ...
- fastdfs-client-java工具类封装
FastDFS是通过StorageClient来执行上传操作的 通过看源码我们知道,FastDFS有两个StorageClient工具类.
- 二维码工具类 - QrcodeUtils.java
二维码工具类,提供多种生成二维码.解析二维码的方法,包括中间logo的二维码等方法. 源码如下:(点击下载 - QrcodeUtils.java.MatrixToImageWriterEx.java. ...
随机推荐
- jQuery判断键盘按下的keyCode
$("div").keydown(function(event) { var keyCode = event.keyCode; //根据keycode判断按下的是哪个键 });
- SpringBoot和数据库连接
就像单机Java应用程序一样,和数据库连接需要DataSource,然后生成到数据库的Connection再进行数据库操作 SpringBoot和原生的JDBC 先看SpringBoot项目源码 从上 ...
- Mybtis框架总结(一)
一:Mybaits下载并搭建核心框架 1:下载mybatis的jar包: 2:创建mybatis框架链接数据库的配置文件Configuration.xml,格式如下 <!DOCTYPE conf ...
- PS Web切图界面设置
界面为移动工具时(快捷键V),选中左上角的图层. 点击视图,选中显示→智能参考线,与标尺. 点击窗口,把"库" "颜色"去掉,把屏幕右上角的"通道&q ...
- C++STL内存管理方法(g++版)
STL作为C++的经典作品,一直备受人们关注.本文主要介绍STL的内存管理策略. 早期的STL内存管理 第一次接触STL源码是看侯捷先生的<STL源码剖析>,此书通俗易懂,剖析透彻,是极佳 ...
- JS中this的指向问题&使用call或apply模拟new
this的指向由调用时决定而不是定义时决定,定义的方式: //直接定义在函数里 var a="window中的a"; var name="window"; fu ...
- Owin SelfHost Asp.net WebApi 遇到 No type was found that matches the controller named 'ControllerName' 异常的解决方案
问题背景:在使用普通的SelfHost时,调用其它工程的dll(其实就是把WebApi写到一个单独的工程方便管理),通过加载其他工程的dll然后再访问webapi是没有问题的. 但是在使用Owin S ...
- 实现android手机来电拦截系统页面弹出自定义页面特效
如何实现android手机来电拦截系统页面弹出自定义页面特效, 首先: 我们需要注册一个监听来电的广播PhoneStateReceiver 类:其次: 在onReceive里面我们获取an ...
- Java for循环的几种用法
J2SE 1.5提供了另一种形式的for循环.借助这种形式的for循环,可以用更简单地方式来遍历数组和Collection等类型的对象.本文介绍使用这种循环的具体方式,说明如何自行定义能被这样遍历的类 ...
- ES 集群
http://www.cnblogs.com/o-andy-o/p/5067184.html