4.2 版本

     /**
* url 请求 paramUrl
*
* @time 2015年11月10日下午4:40:22
* @packageName com.rom.utils
* @param url url请求地址
* @param header 请求头信息
* @param params url请求参数
* @param paramstype 参数类型 1:json格式 ; 其他:正常格式;
* @param resulttype 返回值类型 1: 压缩流字符串 2:正常字符串
* @return
*/
public synchronized static String paramUrl(String url, Map<String, String> header, Map<String, Object> params,
String paramstype, String resulttype) { String result = "";
HttpClient httpClient = new DefaultHttpClient();
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000); HttpPost httpPost = new HttpPost(url); if (header != null) {
Set<String> headerkey = header.keySet();
Iterator<String> headerkeyit = headerkey.iterator();
while (headerkeyit.hasNext()) {
String key = (String) headerkeyit.next();
httpPost.setHeader(key, header.get(key).toString());
}
} try {
// 如果参数类型为1 证明参数传递方式为 json格式
if (paramstype != null && paramstype.equals("1")) {
if (params != null) {
StringEntity entity = new StringEntity(JSONObject.fromObject(params).toString(), "utf-8");// 解决中文乱码问题
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
}
} else {
// 参数格式为 键值对形式
if (params != null) {
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
Set<String> keySet = params.keySet();
Iterator<String> keyit = keySet.iterator();
while (keyit.hasNext()) {
String key = (String) keyit.next();
formparams.add(new BasicNameValuePair(key, params.get(key).toString()));
}
UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
httpPost.setEntity(uefEntity);
}
} HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
if (entity != null) {
// 返回类型如果不为空 并且 等于1 证明该返回结果经过zip压缩
if (resulttype != null && resulttype.equals("1")) {
result = EntityUtils.toString(new GzipDecompressingEntity(entity), "utf-8");
} else {
result = EntityUtils.toString(entity, "utf-8");
}
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
log.error(e.getMessage());
} catch (ClientProtocolException e) {
e.printStackTrace();
log.error(e.getMessage());
} catch (IOException e) {
e.printStackTrace();
log.error(e.getMessage());
} finally {
httpClient.getConnectionManager().shutdown();
}
return result;
}

4.4 版本

     /**
* url 请求 paramUrl
*
* @time 2015年11月10日下午4:40:22
* @packageName com.rom.utils
* @param url url请求地址
* @param header 请求头信息
* @param params url请求参数
* @param paramstype 参数类型 1:json格式 ; 其他:正常格式;
* @return
*/
public static JSONObject paramUrl(String url ,Map<String, String> header,
Map<String, Object> params,String paramstype){ RequestConfig config = RequestConfig.custom()
.setConnectionRequestTimeout(10000).setConnectTimeout(10000)
.setSocketTimeout(10000).build(); HttpPost httpPost = new HttpPost(url); httpPost.setConfig(config);
if (header != null) {
Set<String> headerkey = header.keySet();
Iterator<String> headerkeyit = headerkey.iterator();
while (headerkeyit.hasNext()) {
String key = (String) headerkeyit.next();
httpPost.setHeader(key, header.get(key).toString());
}
} if (params != null) {
// 如果参数类型为1 证明参数传递方式为 json格式
if (paramstype != null && paramstype.equals("1")) {
StringEntity entity = new StringEntity(JSONObject.fromObject(params).toString(), "utf-8");// 解决中文乱码问题
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
} else {
// 参数格式为 键值对形式
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
Set<String> keySet = params.keySet();
Iterator<String> keyit = keySet.iterator();
while (keyit.hasNext()) {
String key = (String) keyit.next();
formparams.add(new BasicNameValuePair(key, params.get(key).toString()));
}
UrlEncodedFormEntity uefEntity = null;
try {
uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
httpPost.setEntity(uefEntity);
}
} CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(null).build();//设置进去 CloseableHttpResponse response = null;
StringBuffer out;
try {
response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); InputStream in = entity.getContent(); out = new StringBuffer();
byte[] b = new byte[4096];
for (int n; (n = in.read(b)) != -1;) {
out.append(new String(b, 0, n));
}
JSONObject json = JSONObject.fromObject(out.toString());
in = null;
return json;
} catch (ClientProtocolException e) {
log.error(e.getMessage());
} catch (IllegalStateException e) {
log.error(e.getMessage());
} catch (IOException e) {
log.error(e.getMessage());
} finally{
try {
if (httpClient!=null) {
httpClient.close();
}
} catch (IOException e) {
log.error(e);
}
}
return null;
}

HttpClient 4.4 请求的更多相关文章

  1. HttpClientUtil [使用apache httpclient模拟http请求]

    基于httpclient-4.5.2 模拟http请求 以get/post方式发送json请求,并获取服务器返回的json -------------------------------------- ...

  2. 【JAVA】通过HttpClient发送HTTP请求的方法

    HttpClient介绍 HttpClient 不是一个浏览器.它是一个客户端的 HTTP 通信实现库.HttpClient的目标是发 送和接收HTTP 报文.HttpClient不会去缓存内容,执行 ...

  3. Android系列之网络(三)----使用HttpClient发送HTTP请求(分别通过GET和POST方法发送数据)

    ​[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...

  4. Android系列之网络(一)----使用HttpClient发送HTTP请求(通过get方法获取数据)

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  5. 一步步教你为网站开发Android客户端---HttpWatch抓包,HttpClient模拟POST请求,Jsoup解析HTML代码,动态更新ListView

    本文面向Android初级开发者,有一定的Java和Android知识即可. 文章覆盖知识点:HttpWatch抓包,HttpClient模拟POST请求,Jsoup解析HTML代码,动态更新List ...

  6. 使用httpclient发送post请求与get请求

    最近因为项目的要求,需要使用httpclient来发送请求.但是查阅了许多博客,大家发送请求的方法各不相同.原因是因为httpclient的jar包的不同版本,其内部方法也不相同.因此抛开具体用到的j ...

  7. Java Apcahe的HTTPClient工具Http请求当请求超时重发

    java Apcahe的HTTPClient工具Http请求当请求超时时底层会默认进行重发,默认重发次数为3次,在某些情况下为了防止重复的请求,需要将自动重发覆盖. 设置HTTP参数,设置不进行自动重 ...

  8. (一)----使用HttpClient发送HTTP请求(通过get方法获取数据)

    (一)----使用HttpClient发送HTTP请求(通过get方法获取数据) 一.HTTP协议初探: HTTP(Hypertext Transfer Protocol)中文 “超文本传输协议”,是 ...

  9. SpringMVC,MyBatis项目中兼容Oracle和MySql的解决方案及其项目环境搭建配置、web项目中的单元测试写法、HttpClient调用post请求等案例

     要搭建的项目的项目结构如下(使用的框架为:Spring.SpingMVC.MyBatis): 2.pom.xml中的配置如下(注意,本工程分为几个小的子工程,另外两个工程最终是jar包): 其中 ...

  10. Httpclient发送json请求

    一.Httpclient发送json请求 public String RequestJsonPost(String url){    String strresponse = null;    try ...

随机推荐

  1. Spring Boot 版本支持对应JDK

    转自:http://www.cnblogs.com/oumi/p/9241424.html 一.Spring Boot 版本支持 Spring Boot Spring Framework Java M ...

  2. Mysql慢查询和慢查询日志分析利器&ndash;mysqlsla

    1.安装mysqlsla Source code     wget http://hackmysql.com/scripts/mysqlsla-2.03.tar.gz tar zvxf mysqlsl ...

  3. [ligerUI] grid封装调用方法

    /** * 获取页面参数 */ function getPageSize(){ var xScroll, yScroll; if (window.innerHeight && wind ...

  4. 44.Qt通过子类化qstyle实现自定义外观

    main.cpp #include <QtGui> #include "brozedialog.h" #include "bronzestyle.h" ...

  5. [XJOI]noip44 T3还有这种操作

    还有这种操作 ttt 最近在学习二进制, 他想知道小于等于N的数中有多少个数的二进制表示中有偶数个1. 但是他想了想觉得不够dark,于是他增加了若干次操作,每次操作会将一个区间内的0变1 , 1变0 ...

  6. lua类实现

    _Account = {} --创建一张借记卡 function _Account:new( tb ) local _Tb = tb or {} setmetatable(_Tb, self) sel ...

  7. Vue.js 2 vs Vue.js 3的实现 – 云栖社区

    Vue.js 2 vs Vue.js 3的实现 – 云栖社区 vue.js核心团队已经讨论过将在Vue3实现的变化.虽然API不会改变,但是数据响应机制(译者注:对数据改变的监听和通知)发生了变化.这 ...

  8. CSS 弹性盒

    图片新窗口打开浏览

  9. Caffe: Caffe的Python接口

    官方参考:http://caffe.berkeleyvision.org/installation.html 官方介绍是这样的: Python The main requirements are nu ...

  10. [SOA]REST与SOA两种架构的异同比较

    REST的特性 它基于HTTP协议,是一种明确构建在客户端/服务端体系结构上的一种风格.特征如下: 1.网络上的资源都被抽象为资源,这些资源都具有唯一的统一资源标识符(URI:Uniform Reso ...