Get and post requests an address and returns the request data

Source: Internet
Author: User

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

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.