HttpClient 4.4 请求
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 请求的更多相关文章
- HttpClientUtil [使用apache httpclient模拟http请求]
基于httpclient-4.5.2 模拟http请求 以get/post方式发送json请求,并获取服务器返回的json -------------------------------------- ...
- 【JAVA】通过HttpClient发送HTTP请求的方法
HttpClient介绍 HttpClient 不是一个浏览器.它是一个客户端的 HTTP 通信实现库.HttpClient的目标是发 送和接收HTTP 报文.HttpClient不会去缓存内容,执行 ...
- Android系列之网络(三)----使用HttpClient发送HTTP请求(分别通过GET和POST方法发送数据)
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...
- Android系列之网络(一)----使用HttpClient发送HTTP请求(通过get方法获取数据)
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...
- 一步步教你为网站开发Android客户端---HttpWatch抓包,HttpClient模拟POST请求,Jsoup解析HTML代码,动态更新ListView
本文面向Android初级开发者,有一定的Java和Android知识即可. 文章覆盖知识点:HttpWatch抓包,HttpClient模拟POST请求,Jsoup解析HTML代码,动态更新List ...
- 使用httpclient发送post请求与get请求
最近因为项目的要求,需要使用httpclient来发送请求.但是查阅了许多博客,大家发送请求的方法各不相同.原因是因为httpclient的jar包的不同版本,其内部方法也不相同.因此抛开具体用到的j ...
- Java Apcahe的HTTPClient工具Http请求当请求超时重发
java Apcahe的HTTPClient工具Http请求当请求超时时底层会默认进行重发,默认重发次数为3次,在某些情况下为了防止重复的请求,需要将自动重发覆盖. 设置HTTP参数,设置不进行自动重 ...
- (一)----使用HttpClient发送HTTP请求(通过get方法获取数据)
(一)----使用HttpClient发送HTTP请求(通过get方法获取数据) 一.HTTP协议初探: HTTP(Hypertext Transfer Protocol)中文 “超文本传输协议”,是 ...
- SpringMVC,MyBatis项目中兼容Oracle和MySql的解决方案及其项目环境搭建配置、web项目中的单元测试写法、HttpClient调用post请求等案例
要搭建的项目的项目结构如下(使用的框架为:Spring.SpingMVC.MyBatis): 2.pom.xml中的配置如下(注意,本工程分为几个小的子工程,另外两个工程最终是jar包): 其中 ...
- Httpclient发送json请求
一.Httpclient发送json请求 public String RequestJsonPost(String url){ String strresponse = null; try ...
随机推荐
- Reorder List 最典型的linkedlist题目
https://oj.leetcode.com/problems/reorder-list/ Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder ...
- Excel 查找某列中的数据在另一列是否存在并输出其他列的数据
最近在操作Excel文件数据导入数据库时,经常需要检查Excel中哪些数据数据库中已经存在,哪些不存在,然后再将不存在数据库中的Excel数据导入:在此过程中,经常需要操作Excel中的数据,所以.也 ...
- Windows显示我的电脑到桌面以及给一些程序设置快捷键
Windows显示我的电脑到桌面,我测试的是windows server 2012和windows10 1.按Win(键盘上的微软徽标键)+R,输入: rundll32.exe shell32.dl ...
- php对文件/目录操作的基础知识(图解)
具体的如下图所示:
- B - Calculating Function
Problem description For a positive integer n let's define a function f: f(n) = - 1 + 2 - 3 + .. + ( ...
- JS 经验总结
1.IE中div的高度是content+padding+border之和,其它的是content的高度 2.一个标签里面只有一个属性,class='cls1 cls2' 3.同一页面不能出现相同的id ...
- Java 实时论坛 - Sym 1.3.0 发布
简介 Sym 是一个用 Java 写的实时论坛,欢迎来体验! 初衷 Sym 的诞生是有如下几点原因: 我们想实现一种新的网络社区体验,独立博客+社区互动 大多数论坛用户体验不够现代化,想做一个和聊 Q ...
- 在APP开发中,如何优雅的设计APP页面
1.明确页面设计在整个产品设计中的位置 互联网产品设计的流程大致是:产品定位——需求分析——信息架构设计——流程设计——页面框架设计——设计说明——输出设计文档.可以看到页面设计是处于整个流程的后期, ...
- MySQL 5.6 Reference Manual-14.6 InnoDB Table Management
14.6 InnoDB Table Management 14.6.1 Creating InnoDB Tables 14.6.2 Moving or Copying InnoDB Tables to ...
- I/O多路复用技术
典型应用于以下场合 1.处理多个描述字时,比如同时处理套接字和磁盘IO.终端IO 2.一个客户同时处理多个套接字 3.服务器既要处理监听套接字,又要处理已连接套接字 4.既要处理TCP.也要处理UDP ...