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. windows下git识别大小写配置

    默认情况下windows上的Git客户端,在文件名仅发生大小写改变时不会识别,提交后发现,gitlab上的文件名不会发生变化. 解决方法: 编辑 .git 中的config文件, 将 ignoreca ...

  2. php工作笔记7-摇动手机,发出请求响应

    1.调用

  3. discuz中方法

    discuz中检验是否是邮箱 function isemail($email) { && strlen($email) <= && preg_match(&quo ...

  4. 使用ssh连接远程主机

    在linux系统中,ssh是远程登录的默认工具,因为该工具的协议使用了RSA/DSA的加密算法.该工具做linux系统的远程管理是非常安全的. ssh登录远程主机(服务器)一般有两种方式:无密钥方式  ...

  5. convert return char from sql server 2008 r2 or below version to c#

    C# string.Replace((char)13, ' ') //newline char; string.Replace((char)10, ' ') //return char;

  6. 在Asp.net 4.0 中动态注册HttpModule

    using System; using System.Web; using Microsoft.Web.Infrastructure; namespace MvcApplication1 { publ ...

  7. printAB()

    #include <iostream> void printA() { std::cout << "A" << std::endl; } voi ...

  8. Android菜鸟成长记14 -- AsnyTask

    本篇随笔将讲解一下Android的多线程的知识,以及如何通过AsyncTask机制来实现线程之间的通信. 一.Android当中的多线程 在Android当中,当一个应用程序的组件启动的时候,并且没有 ...

  9. svn命令

    1.检出.更新.提交 svn chectout http://svn_server/xxx_repository/trunk svn update svn commit -m "XXX&qu ...

  10. javaScript 查询字符串参数 获取

    function getQueryStringArgs() { //取得查询字符串并去掉开头的问号 var qs = (location.search.length > 0 ? location ...