HttpClient sending a GET request

Source: Internet
Author: User

1 /**2 * Get HttpClient's request URL address3      */4      Public StaticString GetUrl () {5String url = "http://" +map.get ("server") + "+map.get" ("Server_port") +map.get ("Heartbeat_path");6 System.out.println (URL);7StringBuilder params =NewStringBuilder (stringutils.empty);8          for(String key:map.keySet ()) {9             if("Timestamp". Equals (Key) {Ten                 Try { OneParams=params.append (Key). Append ("="). Append (Urlencoder.encode (Map.get (key). ToString (), "UTF-8"). Append ("& "); A}Catch(unsupportedencodingexception e) { - e.printstacktrace (); -                 } the}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)) { A                 BooleanIsenderror=map.get (Key). ToString (). EndsWith ("Error!!!"); at                 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 ("&"); in                 } -}Else{ toparams = Params.append (key). Append ("="). Append (Map.get (key)) +. Append ("&"); -             } the         } *         if(!"". Equals (params)) { $url = url + "?" +params.tostring ();Panax Notoginseng         } -url = url.substring (0, Url.length ()-1); the         returnURL; +     } A  the     /** + * Send httpclient requests and update device status based on server-side return data -      */ $      Public Static voidhttpget (String url) { $         //record the current status -Hashmap<string, object> temp=NewHashmap<string,object>(); -          for(Iterator it =Map.keyset (). iterator (); It.hasnext ();) { theString key =It.next (). toString (); - temp.put (Key, Map.get (key));Wuyi         } thehelper.mapsession=temp; -Closeablehttpclient httpClient =Httpclients.createdefault (); WuRequestconfig Requestconfig =Requestconfig.custom () -. SetSocketTimeout (20000). Setconnecttimeout (20000). Build (); AboutHttpGet HttpGet =Newhttpget (URL); $Httpget.addheader ("Accept", "*/*"); -Httpget.addheader ("Connection", "keep-alive"); -Httpget.addheader ("accept-encoding", "gzip, deflate"); - closeablehttpresponse response; A         Try { +Response =Httpclient.execute (httpget); thehttpentity entity =response.getentity (); - System.out.println (Response.getstatusline ()); $             if(Entity! =NULL) { theString params=entityutils.tostring (entity); theString[] Paramsary=params.split ("\\|"); the                 if(paramsary.length==4){ the                      -Map.put ("Status", Paramsary[3]); inString[] Cmdary=paramsary[2].split ("@@"); theSystem.out.println ("response={" +params+ "}"); theMap.put ("RT", "" "); About                     if(!"". Equals (Cmdary[0].trim ()) &&cmdary[0]!=NULL){ the                         if(Cmdary[0].endswith ("Error!!!")){ theMap.put ("cmd", "" "); theMap.put ("status", 0); +helper.flag=false;//a situation without instructions -                             return; the                         }BayiRecivetime=NewDate (). GetTime (); the                         //map.put ("timestamp", new Date ()); the                         if(! ("". Equals (Cmdary[0]) | |NULL==cmdary[0])){ -helper.flag=true;//a situation with instructions -                         } theMap.put ("cmd", cmdary[0]); the                          the                         if(cmdary.length>1){ the                              for(inti=1;i<cmdary.length;i++){ -String[] Param=cmdary[i].split ("="); the                                 if("Sensor_indoor_temp". Equalsignorecase (param[0])){ the                                     DoubleIndoortemp=double.parsedouble (Map.get ("Indoor_temp"). toString ()); the                                     DoubleValue=double.parsedouble (param[1]);94Map.put ("Indoor_temp", indoortemp+value); the}Else if("Sensor_outdoor_temp". Equalsignorecase (param[0])){ the                                     DoubleIndoortemp=double.parsedouble (Map.get ("Outdoor_temp"). toString ()); the                                     DoubleValue=double.parsedouble (param[1]);98Map.put ("Outdoor_temp", indoortemp+value); About                                 } -Map.put (Param[0], param[1]);101                             }102                             return;103                         }104}Else{ theMap.put ("cmd", "" ");106Map.put ("status", 0);107helper.flag=false;//a situation without instructions108                     }109}Else{ theSystem.out.println ("response={" +params+ "}");111                 } the                 113             } the response.close (); the httpclient.close (); the}Catch(IOException e) {117helper.startprogram=false;118helper.flag=false;119 rundevicesimulator (); -             //e.printstacktrace ();121         }122}

HttpClient sending a GET request

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.