httpclient发送get请求

Posted 根须

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了httpclient发送get请求相关的知识,希望对你有一定的参考价值。

  1 /**
  2      * 获取httpclient的请求url地址
  3      */
  4     public static String getUrl(){
  5         String url = "http://"+map.get("server")+":"+map.get("server_port")+map.get("heartbeat_path");
  6         System.out.println(url);
  7         StringBuilder params = new StringBuilder(StringUtils.EMPTY);
  8         for (String key : map.keySet()) {
  9             if("timestamp".equals(key)){
 10                 try {
 11                     params=params.append(key).append("=").append(URLEncoder.encode(map.get(key).toString(), "UTF-8")).append("&");
 12                 } catch (UnsupportedEncodingException e) {
 13                     e.printStackTrace();
 14                 }
 15             }else if("heartbeat_path".equals(key)){
 16                 try {
 17                     params=params.append(key).append("=").append(URLEncoder.encode(map.get(key).toString(), "utf-8")).append("&");
 18                 } catch (UnsupportedEncodingException e) {
 19                     e.printStackTrace();
 20                 }
 21             }else if("cmd".equalsIgnoreCase(key)){
 22                 boolean isEndError=map.get(key).toString().endsWith("error!!!");
 23                 if("mac error!!!".equalsIgnoreCase(map.get(key).toString())||isEndError){
 24                     params = params.append(key).append("=").append("")
 25                             .append("&");
 26                 }else{
 27                     params = params.append(key).append("=").append(map.get(key))
 28                             .append("&");
 29                 }
 30             }else{
 31                 params = params.append(key).append("=").append(map.get(key))
 32                         .append("&");
 33             }
 34         }
 35         if (!"".equals(params)) {
 36             url = url + "?" + params.toString();
 37         }
 38         url = url.substring(0, url.length() - 1);
 39         return url;
 40     }
 41 
 42     /**
 43      * 发送httpclient请求,并根据服务器端的返回数据更新设备状态
 44      */
 45     public static void httpGet(String url) {
 46         //记录当前状态
 47         HashMap<String, Object> temp=new HashMap<String,Object>();
 48         for(Iterator it = map.keySet().iterator() ; it.hasNext();){
 49             String key = it.next().toString();
 50             temp.put(key, map.get(key));
 51         }
 52         Helper.mapSession=temp;
 53         CloseableHttpClient httpClient = HttpClients.createDefault();
 54         RequestConfig requestConfig = RequestConfig.custom()
 55                 .setSocketTimeout(20000).setConnectTimeout(20000).build();
 56         HttpGet httpget = new HttpGet(url);
 57         httpget.addHeader("Accept", "*/*");
 58         httpget.addHeader("Connection", "keep-alive");
 59         httpget.addHeader("Accept-Encoding", "gzip, deflate");
 60         CloseableHttpResponse response;
 61         try {
 62             response = httpClient.execute(httpget);
 63             HttpEntity entity = response.getEntity();
 64             System.out.println(response.getStatusLine());
 65             if (entity != null) {
 66                 String params=EntityUtils.toString(entity);
 67                 String[] paramsAry=params.split("\\|");
 68                 if(paramsAry.length==4){
 69                     
 70                     map.put("status", paramsAry[3]);
 71                     String[] cmdAry=paramsAry[2].split("@@");
 72                     System.out.println("response={"+params+"}");
 73                     map.put("rt", "");
 74                     if(!"".equals(cmdAry[0].trim())&&cmdAry[0]!=null){
 75                         if(cmdAry[0].endsWith("error!!!")){
 76                             map.put("cmd", "");
 77                             map.put("status", 0);
 78                             Helper.flag=false;//没有指令的情况
 79                             return;
 80                         }
 81                         reciveTime=new Date().getTime();
 82                         //map.put("timestamp", new Date());
 83                         if(!("".equals(cmdAry[0])||null==cmdAry[0])){
 84                             Helper.flag=true;//有指令的情况
 85                         }
 86                         map.put("cmd", cmdAry[0]);
 87                         
 88                         if(cmdAry.length>1){
 89                             for(int i=1;i<cmdAry.length;i++){
 90                                 String[] param=cmdAry[i].split("=");
 91                                 if("sensor_indoor_temp".equalsIgnoreCase(param[0])){
 92                                     double indoorTemp=Double.parseDouble(map.get("indoor_temp").toString());
 93                                     double value=Double.parseDouble(param[1]);
 94                                     map.put("indoor_temp", indoorTemp+value);
 95                                 }else if("sensor_outdoor_temp".equalsIgnoreCase(param[0])){
 96                                     double indoorTemp=Double.parseDouble(map.get("outdoor_temp").toString());
 97                                     double value=Double.parseDouble(param[1]);
 98                                     map.put("outdoor_temp", indoorTemp+value);
 99                                 }
100                                 map.put(param[0], param[1]);
101                             }
102                             return;
103                         }
104                     }else{
105                         map.put("cmd", "");
106                         map.put("status", 0);
107                         Helper.flag=false;//没有指令的情况
108                     }
109                 }else{
110                     System.out.println("response={"+params+"}");
111                 }
112                 
113             }
114             response.close();
115             httpClient.close();
116         } catch (IOException e) {
117             Helper.startProgram=false;
118             Helper.flag=false;
119             runDeviceSimulator();
120             //e.printStackTrace();
121         }
122     }

 

以上是关于httpclient发送get请求的主要内容,如果未能解决你的问题,请参考以下文章

httpclient发送get请求

HttpClient发送Post请求,get请求

HttpClient发送get,post接口请求

HttpClient模拟get,post请求并发送请求参数(json等)

HttpClient4.X发送Get请求的url参数拼接

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