
正文
httpclient发送get请求
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
/**
* 获取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();
}
}







