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. ... 
随机推荐
- leetcode 217
			217. Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your ... 
- 头像上传,拖拽,裁切(HTML5)版本
			演示地址: http://codeman35.itongyin.com:19002/v1/web_demo.html 功能: 支持滚轴放大缩小,鼠标拖动,裁切可视区域 
- WebView 调试
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { WebView.setWebContentsDeb ... 
- c++ 打印堆栈代码
			namespace google {namespace glog_internal_namespace_ {void DumpStackTraceToString(std::string* stack ... 
- delphi数据类型及占用的字节数 C++ 对应数据类型
			delphi byte:1个字节. int/Integer: long: long long:8字节,64位 shortInt:2字节,16位 LongInt:4字节,32位 Int64:8字节,64 ... 
- xcode调试技巧
			xode报错有时挺无厘头,完全不知道哪里出的问题,最后还得用排除法,记录一些工作中认为有用的调试技巧 1.左侧视图点断点视图,左下角点加号,选择exception breakpoint,类型选c++, ... 
- UICollectionReusableView 使用时的一些问题
			在使用UICollectionView 时, 设置分区头时, - (UICollectionReusableView *)collectionView:(UICollectionView *)coll ... 
- LAMP环境
			LAMP = Linux + Apache + MySQL + PHP [1] [2] [3] [4] [1]Linux是一套免费使用和自由传播的类Unix操作系统, ... 
- windows访问lnmp配置的虚拟域名
			原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://linuxzkq.blog.51cto.com/9379412/1630217 实 ... 
- CentOS6.5中配置Rabbitmq3.6.6集群方案
			一.安装Erlang环境 1.下载erlang源代码 wget http://erlang.org/download/otp_src_19.1.tar.gz 2.安装依赖相关库文件 yum insta ... 
