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工具类的更多相关文章

  1. Go/Python/Erlang编程语言对比分析及示例 基于RabbitMQ.Client组件实现RabbitMQ可复用的 ConnectionPool(连接池) 封装一个基于NLog+NLog.Mongo的日志记录工具类LogUtil 分享基于MemoryCache(内存缓存)的缓存工具类,C# B/S 、C/S项目均可以使用!

    Go/Python/Erlang编程语言对比分析及示例   本文主要是介绍Go,从语言对比分析的角度切入.之所以选择与Python.Erlang对比,是因为做为高级语言,它们语言特性上有较大的相似性, ...

  2. C#工具类OracleHelper,基于Oracle.ManagedDataAccess.Client封装

    基于Oracle.ManagedDataAccess.Client封装的Oracle工具类OracleHelper,代码如下: using System; using System.Data; usi ...

  3. MongoDBDao 工具类(包含分页取数据)

    mongdb工具类 package e16wifi.statistic.com.mongodb; import java.util.ArrayList; import java.util.List; ...

  4. Rhino+envjs-1.2.js 在java运行网站js 工具类

    java爬虫遇到个页面加密的东西,找了些资料学习学习 做了个java运行js的工具类,希望对大家有用,其中用到client(获取js)可以自行换成自己的client.主要是用了 Rhino就是Java ...

  5. HttpClient4.5 SSL访问工具类

    要从网上找一个HttpClient SSL访问工具类太难了,原因是HttpClient版本太多了,稍有差别就不能用,最后笔者干脆自己封装了一个访问HTTPS并绕过证书工具类. 主要是基于新版本Http ...

  6. HttpClient_httpclient 4.3.1 post get的工具类

    package com.ryx.util; import java.util.ArrayList; import java.util.List; import java.util.Map; impor ...

  7. Mina工具类v1.5

    package com.cucpay.fundswap.util; import java.net.InetSocketAddress; import java.nio.charset.Charset ...

  8. fastdfs-client-java工具类封装

    FastDFS是通过StorageClient来执行上传操作的 通过看源码我们知道,FastDFS有两个StorageClient工具类.

  9. 二维码工具类 - QrcodeUtils.java

    二维码工具类,提供多种生成二维码.解析二维码的方法,包括中间logo的二维码等方法. 源码如下:(点击下载 - QrcodeUtils.java.MatrixToImageWriterEx.java. ...

随机推荐

  1. Linux:下载方式安装lrzsz

    若机器服务使用yum源安装,可先下载好lrzsz文件后再上传安装 步骤一: 先下载lrzsz的tar包:wget https://ohse.de/uwe/releases/lrzsz-0.12.20. ...

  2. for 小曦

    安装GO 当前版本1.6.2 http://blog.163.com/hehaifeng1984@126/blog/static/690011362013101044011568/ 教程 https: ...

  3. cnblogs,我回来了

    之前是在Github上搭了个博客,原因只有一个:可以弄个比较个性的域名,逼格高. 不过用起来倒是麻烦,一是经常纠结自己的主页是不是不够逼格?二就是身在墙内,访问速度不理想. 所以,还是安心的在这里,写 ...

  4. 采用TCP协议的PIC32MZ ethernet bootloader

    了解更多关于bootloader 的C语言实现,请加我QQ: 1273623966 (验证信息请填 bootloader),欢迎咨询或定制bootloader(在线升级程序). 趁热打铁,在上一PIC ...

  5. Javascript数组函数库

    其实平时用的比较多的应该是push和pop,不过还是都记下来,以便后面使用. shift :删除原数组第一项,并返回删除元素的值:如果数组为空则返回undefined var a = [1,2,3,4 ...

  6. 无线路由!RTS DTIM阈值、Beacon 周期如何设置多少可以加快路由

    无线路由!RTS DTIM阈值.Beacon 周期如何设置多少可以加快路由 DTIM阈值是使用无线路由器时无线发送数据包的频率.1间隔最低,255最高[1]  但是想要使连接的设备达到最高运行速度,调 ...

  7. 《CoffeeScript应用开发》学习:第五章 CoffeeScript中的类

    在CoffeeScript中定义类 在CoffeeScript中,使用class定义类,使用关键字new实例化对象. 给类绑定方法 class Airplane takeOff: -> cons ...

  8. chroot directory

    给 /A/B/C的C目录做chroot,要对C能读写,所以C目录不能做ROOT目录,对B做chroot. 设置C目录所有者为sftp 账户a,组也改为sftp组(这里a和sftp组都是之前建立好的sf ...

  9. Expression Tree Basics 表达式树原理

    variable point to code variable expression tree data structure lamda expression anonymous function 原 ...

  10. Chrome浏览器官方下载地址

    Chrome浏览器离线安装包官方下载地址,和在线安装一样能自动更新. 正式版 http://www.google.com/chrome/eula.html?hl=zh-CN&standalon ...