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("请求失败");
}
}
}

最新文章

  1. 学习使用html与css,并尝试写php
  2. Monyer&#39;s game Google Hack关的BT玩法
  3. 神经网络:多层网络与C++实现
  4. 二模 (12) day1
  5. C++多态性与C#的比较
  6. hdoj 1241 Oil Deposits
  7. C++的函数重载 转
  8. JAVA Calendar具体解释
  9. 当MVC4无法跳转时
  10. 只启动一个zookeeper配置 server1只需要配置一个
  11. 谈谈Session会话和Cookie
  12. 学点Groovy来理解build.gradle代码
  13. 使用FastJson进行对象和JSON转换属性命名规则为下划线和驼峰的问题
  14. Manjaro使用笔记-使用中国源的方法
  15. Android下查看共享库依赖项
  16. 小程序 login
  17. centos6.5安装无线网卡驱动并配置wifi
  18. 20144303石宇森《网络对抗》Web安全基础实践
  19. java 多线程之 interrupt()和线程终止方式
  20. rails 部署 nginx + passenger

热门文章

  1. windown安装bloomFilter
  2. node.js 关于跨域和传递给前台参数
  3. iOS CGAffineTransform你了解多少?
  4. Velocity模板引擎介绍
  5. Django models ORM基础操作--白话聊Django系列
  6. LinuxCentos系统安装Nginx过程记录
  7. Android怎样在http头信息里设置參数
  8. Linux用户相关文件之密码文件
  9. HTTP1.1中CHUNKED编码解析
  10. [转] CentOS---网络配置详解