/**
* 获取httpclient的请求url地址
*/
public static String getUrl(){
String url = "http://"+map.get("server")+":"+map.get("server_port")+map.get("heartbeat_path");
System.out.println(url);
StringBuilder params = new StringBuilder(StringUtils.EMPTY);
for (String key : map.keySet()) {
if("timestamp".equals(key)){
try {
params=params.append(key).append("=").append(URLEncoder.encode(map.get(key).toString(), "UTF-8")).append("&");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}else if("heartbeat_path".equals(key)){
try {
params=params.append(key).append("=").append(URLEncoder.encode(map.get(key).toString(), "utf-8")).append("&");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}else if("cmd".equalsIgnoreCase(key)){
boolean isEndError=map.get(key).toString().endsWith("error!!!");
if("mac error!!!".equalsIgnoreCase(map.get(key).toString())||isEndError){
params = params.append(key).append("=").append("")
.append("&");
}else{
params = params.append(key).append("=").append(map.get(key))
.append("&");
}
}else{
params = params.append(key).append("=").append(map.get(key))
.append("&");
}
}
if (!"".equals(params)) {
url = url + "?" + params.toString();
}
url = url.substring(0, url.length() - 1);
return url;
} /**
* 发送httpclient请求,并根据服务器端的返回数据更新设备状态
*/
public static void httpGet(String url) {
//记录当前状态
HashMap<String, Object> temp=new HashMap<String,Object>();
for(Iterator it = map.keySet().iterator() ; it.hasNext();){
String key = it.next().toString();
temp.put(key, map.get(key));
}
Helper.mapSession=temp;
CloseableHttpClient httpClient = HttpClients.createDefault();
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(20000).setConnectTimeout(20000).build();
HttpGet httpget = new HttpGet(url);
httpget.addHeader("Accept", "*/*");
httpget.addHeader("Connection", "keep-alive");
httpget.addHeader("Accept-Encoding", "gzip, deflate");
CloseableHttpResponse response;
try {
response = httpClient.execute(httpget);
HttpEntity entity = response.getEntity();
System.out.println(response.getStatusLine());
if (entity != null) {
String params=EntityUtils.toString(entity);
String[] paramsAry=params.split("\\|");
if(paramsAry.length==4){ map.put("status", paramsAry[3]);
String[] cmdAry=paramsAry[2].split("@@");
System.out.println("response={"+params+"}");
map.put("rt", "");
if(!"".equals(cmdAry[0].trim())&&cmdAry[0]!=null){
if(cmdAry[0].endsWith("error!!!")){
map.put("cmd", "");
map.put("status", 0);
Helper.flag=false;//没有指令的情况
return;
}
reciveTime=new Date().getTime();
//map.put("timestamp", new Date());
if(!("".equals(cmdAry[0])||null==cmdAry[0])){
Helper.flag=true;//有指令的情况
}
map.put("cmd", cmdAry[0]); if(cmdAry.length>1){
for(int i=1;i<cmdAry.length;i++){
String[] param=cmdAry[i].split("=");
if("sensor_indoor_temp".equalsIgnoreCase(param[0])){
double indoorTemp=Double.parseDouble(map.get("indoor_temp").toString());
double value=Double.parseDouble(param[1]);
map.put("indoor_temp", indoorTemp+value);
}else if("sensor_outdoor_temp".equalsIgnoreCase(param[0])){
double indoorTemp=Double.parseDouble(map.get("outdoor_temp").toString());
double value=Double.parseDouble(param[1]);
map.put("outdoor_temp", indoorTemp+value);
}
map.put(param[0], param[1]);
}
return;
}
}else{
map.put("cmd", "");
map.put("status", 0);
Helper.flag=false;//没有指令的情况
}
}else{
System.out.println("response={"+params+"}");
} }
response.close();
httpClient.close();
} catch (IOException e) {
Helper.startProgram=false;
Helper.flag=false;
runDeviceSimulator();
//e.printStackTrace();
}
}

httpclient发送get请求的更多相关文章

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

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

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

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

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

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

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

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

  5. Httpclient发送json请求

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

  6. .net core使用HttpClient发送代理请求_程序内抓包_Fiddler抓包

    前言:  通过Fiddler抓取浏览器请求数据,相信大家已经都会用了,我们知道Fiddler是通过在本机计算器添加一个默认的代理服务器来实现的抓包数据的,端口号为:8888. 其实当我们打开Fiddl ...

  7. java httpclient发送json 请求 ,go服务端接收

    /***java客户端发送http请求*/package com.xx.httptest; /** * Created by yq on 16/6/27. */ import java.io.IOEx ...

  8. 使用HttpClient发送HTTPS请求以及配置Tomcat支持SSL

    这里使用的是HttpComponents-Client-4.1.2 package com.jadyer.util; import java.io.File; import java.io.FileI ...

  9. httpclient发送request请求时设置header和timeout

    package com.xxx.xxx.common; import java.io.BufferedReader; import java.io.InputStreamReader; import ...

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

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

随机推荐

  1. 如何移除浏览器一启动就打开lunchpage.org

    lunchpage.org 就是一个劫持网站.症状就是你打开你电脑上的任何浏览器都会重定向到一个广告页面.这个很烦! 解决方法: 1. 安装 Zemana AntiMalware 便携版. 2. 打开 ...

  2. Visual C++中error spawning cl.exe错误的两种解决方法.

    可能很多人在安装VC 6.0后有过点击“Compile”或者“Build”后被出现的 “Compiling... ,Error spawning cl.exe”错误提示给郁闷过.很多人的 选择是重装, ...

  3. LEFT JOIN结果集可能变大。。。。。

    SELECT A.*,B.* FROM A LEFT JOIN B ON A.ID = B.ID

  4. c++_包子凑数

    标题:包子凑数 小明几乎每天早晨都会在一家包子铺吃早餐.他发现这家包子铺有N种蒸笼,其中第i种蒸笼恰好能放Ai个包子.每种蒸笼都有非常多笼,可以认为是无限笼. 每当有顾客想买X个包子,卖包子的大叔就会 ...

  5. CSRF verification failed. Request aborted. 表单提交方法为POST时的报错

    本人所用Django版本为1.11,在设置请求方法为POST时,遇到标题中的错误,尝试了多种方法,最终通过下面的操作来修复: 在template文件中添加图中红框部分 接着,导入csrf_exempt ...

  6. django1.11 启动错误:Generator expression must be parenthesized

    错误信息: Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0 ...

  7. zoj 2772 Quick Change

    Quick Change Time Limit: 2 Seconds      Memory Limit: 65536 KB J.P. Flathead's Grocery Store hires c ...

  8. ms sql server 大批量导入

    BULK INSERT 文章:BULK INSERT如何将大量数据高效地导入SQL Server 可以首先在数据库建一个表Temp_tb,这个表作为导入数据的表,然后使用bulk insert导入,导 ...

  9. mongodb 报错问题

    系统不支持:Mongo 错误位置 FILE: C:\wamp64\www\frame\a_tp32\ThinkPHP\Library\Think\Db\Driver\Mongo.class.php L ...

  10. ubuntu-12.04下安装postgresql

    2013-10-01 20:42:57|    moniter参考资料:Ubuntu 12.04下PostgreSQL-9.1安装与配置详解(在线安装)一.安装postgresqlbamboo@bam ...