1 PackageCom.htpt.superviseServices.dm.util;2 3 ImportJava.io.BufferedReader;4 Importjava.io.IOException;5 ImportJava.io.InputStreamReader;6 ImportJava.io.PrintWriter;7 ImportJava.net.URL;8 Importjava.net.URLConnection;9 Importjava.util.List;Ten ImportJava.util.Map; One A ImportNet.sf.json.JSONArray; - - Public classHttpRequest { the /** - * Request to send a GET method to the specified URL - * - * @paramURL + * Send the requested URL - * @paramparam + * Request parameters, request parameters should be in the form of name1=value1&name2=value2. A * @returnThe response result of the remote resource represented by the URL at */ - Public StaticBoolean Sendget (String lon, String lat) { -String result = ""; -BufferedReader in =NULL; -String City = ""; - Try { inString urlnamestring = "Http://api.map.baidu.com/geocoder/v2/?ak=56E117E2740978010ece5593e91b00e3&output=json &coordtype=wgs84ll&location= "+lat+", "+Lon; -URL Realurl =NewURL (urlnamestring); to //opening and linking between URLs +URLConnection connection =realurl.openconnection (); - //to set common request properties theConnection.setrequestproperty ("Accept", "*/*"); *Connection.setrequestproperty ("Connection", "keep-alive"); $Connection.setrequestproperty ("User-agent",Panax Notoginseng"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) "); - //establish an actual connection the Connection.connect (); + //Get all response header fields Amap<string, list<string>> map =connection.getheaderfields (); the //iterate through all the response header fields + for(String key:map.keySet ()) { -SYSTEM.OUT.PRINTLN (key + "--->" +Map.get (key)); $ } $ //defines the response of the BufferedReader input stream to read the URL -in =NewBufferedReader (NewInputStreamReader ( - Connection.getinputstream ())); the String Line; - while(line = In.readline ())! =NULL) {WuyiResult + =Line ; the } -Jsonarray data = Jsonarray.fromobject ("[" +result+ "]"); WuMap DataMap = (map) data.get (0); -Map Dataresult = (map) datamap.get ("Result"); AboutMap addresscomponent = (map) dataresult.get ("Addresscomponent"); $City = (String) addresscomponent.get ("City"); -}Catch(Exception e) { -SYSTEM.OUT.PRINTLN ("Send GET request exception!") " +e); - e.printstacktrace (); A } + //Use the finally block to close the input stream the finally { - Try { $ if(In! =NULL) { the in.close (); the } the}Catch(Exception E2) { the e2.printstacktrace (); - } in } the System.out.println (city); the return"Changsha". Equals (city); About } the the /** the * Request to send the Post method to the specified URL + * - * @paramURL the * Send the requested URLBayi * @paramparam the * Request parameters, request parameters should be in the form of name1=value1&name2=value2. the * @returnThe response result of the remote resource represented - */ - Public Staticstring sendpost (string url, string param) { thePrintWriter out =NULL; theBufferedReader in =NULL; theString result = ""; the Try { -URL Realurl =Newurl (url); the //opening and linking between URLs theURLConnection conn =realurl.openconnection (); the //to set common request properties94Conn.setrequestproperty ("Accept", "*/*"); theConn.setrequestproperty ("Connection", "keep-alive"); theConn.setrequestproperty ("User-agent", the"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ");98 //to send a POST request, you must set the following two lines AboutConn.setdooutput (true); -Conn.setdoinput (true);101 //gets the output stream corresponding to the URLConnection object102out =NewPrintWriter (Conn.getoutputstream ());103 //Send Request Parameters104 out.print (param); the //buffer for flush output stream106 Out.flush ();107 //defines the response of the BufferedReader input stream to read the URL108in =NewBufferedReader (109 NewInputStreamReader (Conn.getinputstream ())); the String Line;111 while(line = In.readline ())! =NULL) { theResult + =Line ;113 } the}Catch(Exception e) { theSYSTEM.OUT.PRINTLN ("Send POST request exception!") "+e); the e.printstacktrace ();117 }118 //Use the finally block to close the output stream, input stream119 finally{ - Try{121 if(out!=NULL){122 out.close ();123 }124 if(in!=NULL){ the in.close ();126 }127 } - Catch(IOException ex) {129 ex.printstacktrace (); the }131 } the returnresult;133 }134}
Java sends get and POST requests