Package CN. GIS; import Java. io. bufferedreader; import Java. io. inputstreamreader; import java.net. httpurlconnection; import java.net. URL; import java.net. urlconnection;/*** Path Analysis * @ author Liu renkui */public class gisdata {/*** GET Request Method ** @ Param urladdress request address * @ Param encodedtype encoding method, UTF-8, GBK... */public static string getdata (string urladdress, string encodedtype) {try {URL url = new URL (urladdress); http Urlconnection conn = (httpurlconnection) URL. openconnection (); // open the connection with the URL // obtain the returned data and use UTF-8 to transcode the stream data, otherwise, garbled bufferedreader in = new bufferedreader (New inputstreamreader (Conn. getinputstream (), encodedtype); string line = NULL; stringbuffer sb = new stringbuffer (); While (line = in. readline ())! = NULL) {sb. append (line);} In. close (); // close the stream return sb. tostring ();} catch (exception e) {e. printstacktrace ();} return "";}/*** POST Request Method ** @ Param urladdress request address * @ Param encodedtype encoding method, UTF-8, GBK... */public static string postdata (string urladdress, string encodedtype) {try {URL url = new URL (urladdress); urlconnection conn = URL. openconnection (); // open the connection // use the POST request, whether to output/input to the connection, because this is a POST request, The parameter must be placed in the HTTP body, so it must be set to trueconn. setdooutput (true); Conn. setdoinput (true); Conn. setusecaches (false); // No need to set cache for post requests // send Domain Information // outputstreamwriter out = new // outputstreamwriter (Conn. getoutputstream (), "UTF-8"); // out. flush (); // out. close (); // close the stream // obtain the returned data bufferedreader in = new bufferedreader (New inputstreamreader (Conn. getinputstream (), "UTF-8"); string line = NULL; stringbuffer sb = new stringbuffer (); While (line = in. Readline ())! = NULL) {sb. append (line);} In. close (); // close the stream return sb. tostring ();} catch (exception e) {e. printstacktrace ();} return "";} public static void main (string [] ARGs) {string urladdress = "http://gis.bjxch.gov.cn: 5002/navigationservice. do? Request = navigating & SX = 498802.9959 & Sy = 307318.7775 & Ex = 500844.8565 & ey = 304921.25269999 & flag = 0 & type = 0 & coordtype = 0 "; // The returned data, such as XML or JSON data system. out. println ("-------- get --------"); system. out. println (getdata (urladdress, "UTF-8"); // get method system. out. println ("-------- post --------"); system. out. println (postdata (urladdress, "UTF-8"); // POST method }}
Get and post requests an address and returns the request data