project development will inevitably encounter external interface calls, niche today first contact with the class, followed by the API method to go again, if there is a wrong place, also please Doraemon, humbly!
1 Packagecom.cplatform.movie.back.test;2 3 ImportJava.io.BufferedReader;4 ImportJava.io.DataOutputStream;5 ImportJava.io.InputStreamReader;6 Importjava.net.HttpURLConnection;7 ImportJava.net.URL;8 ImportJava.net.URLEncoder;9 Ten One A Public classHttpurlconnectiontest { - Public Static FinalString Get_url = "Http://112.4.27.9/mall-back/if_user/store_list?storeId=32"; - Public Static FinalString post_url = "Http://112.4.27.9/mall-back/if_user/store_list"; the - /** - * Interface Call GET - */ + Public Static voidHttpurlconectionget () { - Try { +URL url =NewURL (Get_url);//Convert a string to a URL request address AHttpURLConnection connection = (httpurlconnection) url.openconnection ();//Open Connection atConnection.connect ();//Connection Session - //Get input stream -BufferedReader br =NewBufferedReader (NewInputStreamReader (Connection.getinputstream ())); - String Line; -StringBuilder SB =NewStringBuilder (); - while(line = Br.readline ())! =NULL) {//Loop Read Stream in Sb.append (line); - } toBr.close ();//Close the stream +Connection.disconnect ();//Disconnect Connection - System.out.println (sb.tostring ()); the}Catch(Exception e) { * e.printstacktrace (); $SYSTEM.OUT.PRINTLN ("Failure!"));Panax Notoginseng } - } the + /** A * Interface Call POST the */ + Public Static voidHttpurlconnectionpost () { - Try { $URL url =NewURL (post_url); $ - //strong urlconnection connection with URL returned by the Open method to HttpURLConnection connection (identifies the remote object connection referenced by a URL) -HttpURLConnection connection = (httpurlconnection) url.openconnection ();//at this point the cnnection is just a connection object, pending connection the - //set the connection output stream to True, false by default (the POST request is passed implicitly as a stream parameter)WuyiConnection.setdooutput (true); the - //set the connection input stream to True WuConnection.setdoinput (true); - About //set the request mode to post $Connection.setrequestmethod ("POST"); - - //POST request cache set to False -Connection.setusecaches (false); A + //sets whether the HttpURLConnection instance automatically performs a redirect theConnection.setinstancefollowredirects (true); - $ //set the various properties within the request header (the following is the type of the content set, set to the urlencoded encoded from parameter) the //application/x-javascript text/xml->xml Data Application/x-javascript->json Object application/ x-www-form-urlencoded-> form Data theConnection.setrequestproperty ("Content-type", "application/x-www-form-urlencoded"); the the //Establish a connection (the request does not start until the Connection.getinputstream () method call is initiated, and the above parameters are set before this method) - Connection.connect (); in the //Create an input/output stream for outputting the parameters that are carried in the connection, (the output is what follows) theDataOutputStream dataout =NewDataOutputStream (Connection.getoutputstream ()); AboutString parm = "storeid=" + urlencoder.encode ("+", "utf-8");//The Urlencoder.encode () method encodes a string the the //output parameters to the connection the dataout.writebytes (parm); + - //refresh and close the stream when the output is complete the Dataout.flush ();BayiDataout.close ();//important and easy to ignore steps (close the stream, remember!) the the System.out.println (Connection.getresponsecode ()); - - //Connection initiation request, processing server response (from connection fetch to input stream and wrapped as BufferedReader) theBufferedReader BF =NewBufferedReader (NewInputStreamReader (Connection.getinputstream ())); the String Line; theStringBuilder SB =NewStringBuilder ();//used to store response data the - //Loop read stream, if not at the end the while(line = Bf.readline ())! =NULL) { the Sb.append (Bf.readline ()); the }94Bf.close ();//important and easy to ignore steps (close the stream, remember!) theConnection.disconnect ();//Destroy Connection the System.out.println (sb.tostring ()); the 98}Catch(Exception e) { About e.printstacktrace (); - }101 }102 103 104 Public Static voidMain (string[] args) { the //httpurlconectionget ();106 httpurlconnectionpost ();107 }108}
Java background calls the HttpURLConnection class to simulate browser requests (typically used for interface calls)