HttpComponents之httpclient
HttpComponents源码目录:http://www.boyunjian.com/javasrc/org.apache.httpcomponents/httpclient/4.3.4/_/ package com.bbkj.utils; import com.alibaba.fastjson.JSON;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
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.util.*; public class HttpUtils{ private static PoolingHttpClientConnectionManager connectionManager = null;
private static HttpClientBuilder httpBulder = null;
private static RequestConfig requestConfig = null; private static int MAXCONNECTION = 10; private static int DEFAULTMAXCONNECTION = 5; private static String IP = "abc.com";
private static int PORT = 80; static {
//设置http的状态参数
requestConfig = RequestConfig.custom()
.setSocketTimeout(5000)
.setConnectTimeout(5000)
.setConnectionRequestTimeout(5000)
.build(); HttpHost target = new HttpHost(IP, PORT);
connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.setMaxTotal(MAXCONNECTION);
connectionManager.setDefaultMaxPerRoute(DEFAULTMAXCONNECTION);
connectionManager.setMaxPerRoute(new HttpRoute(target), 20);
httpBulder = HttpClients.custom();
httpBulder.setConnectionManager(connectionManager);
} public static CloseableHttpClient getConnection() {
CloseableHttpClient httpClient = httpBulder.build();
httpClient = httpBulder.build();
return httpClient;
} public static HttpUriRequest getRequestPost(Map<String, Object> map, String url) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
Set<Map.Entry<String, Object>> entrySet = map.entrySet();
HttpUriRequest reqMethod = null;
StringEntity stringEntity = new StringEntity(JSON.toJSONString(map), ContentType.APPLICATION_JSON);
reqMethod = RequestBuilder.post().setUri(url)
.setEntity(stringEntity)
.setConfig(requestConfig).build();
return reqMethod;
} public static HttpUriRequest getRequestGet(Map<String, String> map, String url) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
Set<Map.Entry<String, String>> entrySet = map.entrySet();
for (Map.Entry<String, String> e : entrySet) {
String name = e.getKey();
String value = e.getValue();
NameValuePair pair = new BasicNameValuePair(name, value);
params.add(pair);
}
HttpUriRequest reqMethod = null;
reqMethod = RequestBuilder.get().setUri(url)
.addParameters(params.toArray(new BasicNameValuePair[params.size()]))
.setConfig(requestConfig).build();
return reqMethod;
} public static void main(String args[]) throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
// map.put("startindex", "1");
// map.put("size", "2"); map.put("sn","1");
map.put("index",1);
map.put("heart_rate",1);
map.put("resp_rate",1);
map.put("leave_Bed",true);
map.put("leave_bed_time",1);
map.put("timestamp",1);
map.put("bedNo","1"); /***
* String sn; //设备编号
Integer index; //序号
Integer heart_rate; //心率
Integer resp_rate; //呼吸/分钟
Boolean leave_Bed; //false:未离床,true:离床
Integer leave_bed_time; //离床时间(分钟)
Long timestamp;
String bedNo;
*/ HttpClient client = getConnection();
HttpUriRequest post = getRequestPost(map, "http://abc.com/bed/submit/data");
HttpResponse response = client.execute(post);
System.out.println(response.getStatusLine().getStatusCode() );
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
String message = EntityUtils.toString(entity, "utf-8");
System.out.println(message);
} else {
System.out.println("请求失败");
}
}
}
HttpComponents之httpclient的更多相关文章
- HttpComponents组件探究 - HttpClient篇
在Java领域,谈到网络编程,可能大家脑海里第一反应就是MINA,NETTY,GRIZZLY等优秀的开源框架.没错,不过在深入探究这些框架之前,我们需要先从最original的技术探究开始(当然,需要 ...
- apache.http.client.HttpClient
前言 HTTP 协议可能是现在 Internet 上使用得最多.最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源.虽然在 JDK 的 java net包中已经提 ...
- java httpclient post xml demo
jar archive: http://archive.apache.org/dist/httpcomponents/ 基于httpclient 2.0 final的demo(for jdk1.5/1 ...
- HttpClient详细解释
Http协议的重要性相信不用我多说了,HttpClient相比传统JDK自带的URLConnection,增加了易用性和灵活性(具体区别,日后我们再讨论),它不仅是客户端发送Http请求变得容易,而且 ...
- Apache HttpComponents 学习
基本上,用户常用的就是HttpClient:它基于Http Core部分,但 Core部分太过于 low level,不建议使用,除非有特殊需要. Apache HttpComponentsTM 项目 ...
- HttpClient使用详细教程
Http协议的重要性相信不用我多说了,HttpClient相比传统JDK自带的URLConnection,增加了易用性和灵活性(具体区别,日后我们再讨论),它不仅是客户端发送Http请求变得容易,而且 ...
- Java中的http(网络处理)相关的库:HttpClient,HttpCore(转载)
[背景] 最近和之前,折腾了这个: [教程]模拟登陆百度之Java代码版 然后,对于Java的HttpClient,有了点了解. 现在整理如下: Java本身没有Http相关的库 Java本身,没有内 ...
- Http和Https网络同步请求httpclient和异步请求async-http-client
原文:https://blog.csdn.net/fengshizty/article/details/53100694 Http和https网络请求 主要总结一下使用到的网络请求框架,一种是同步网络 ...
- httpclient解析
1.HttpClient简介 HttpClient相比传统JDK自带的URLConnection,增加了易用性和灵活性,它不仅使客户端发送Http请求变得容易,而且也方便开发人员测试接口(基于Http ...
随机推荐
- 43、android:screenOrientation
android:screenOrientationThe orientation of the activity's display on the device. The value can be a ...
- UVALive 5873 (几何+思维)
唉 被秀了... 还是太弱,说好的数形结合呢,列个式子出来后就被吓到了,然后就懵逼了. 题意: 有一条狗,从原点出发,沿n个向量走,每个向量只走一次,沿着一个向量(x,y)走时,既可以往(x,y)方向 ...
- SteinerTree模板
#define N 55//所有点的个数 #define K 10//SteinerTree 最大顶点数,必须精确 #define INF 10000000 //SteinerTree 邻接矩阵模板. ...
- 爬虫入门【8】Python连接MongoDB的用法简介
MongoDB的连接和数据存取 MongoDB是一种跨平台,面向文档的NoSQL数据库,提供高性能,高可用性并且易于扩展. 包含数据库,集合,文档等几个重要概念. 我们在这里不介绍MongoDB的特点 ...
- JS HTML DOM---Document对象
Document 对象 当浏览器载入 HTML 文档, 它就会成为 document 对象. document 对象是HTML文档的根节点与所有其他节点(元素节点,文本节点,属性节点, 注释节点). ...
- IOS开发复习笔记(4)-TableView
总结几个TableView常用的代码 1.初始化方面 static string CellIndetifier=@"cellIndetifier"; -(NSInteger)num ...
- VMware虚拟机安装(二)
我们一般用户使用的是windows ,那如何装一个Centos呢,我们一般学习的话,是搞一个VM虚拟机, 在虚拟机里安装一个Centos操作系统来学习,当然正式部署的话 我们一般是从阿里云或者西部数码 ...
- java限流(一): Semaphore
Before obtaining an item each thread must acquire a permit from the semaphore, guaranteeing that an ...
- 《Python 机器学习》笔记(四)
数据预处理--构建好的训练数据集 机器学习算法最终学习结果的优劣取决于两个主要因素:数据的质量和数据中蕴含的有用信息的数量. 缺失数据的处理 在实际应用过程中,样本由于各种原因缺少一个或多个值得情况并 ...
- 【转】python面向对象中的元类
type() 动态语言和静态语言最大的不同,就是函数和类的定义,不是编译时定义的,而是运行时动态创建的. 比方说我们要定义一个Hello的class,就写一个hello.py模块: class Hel ...