Package Util;import Java.io.bufferedreader;import Java.io.dataoutputstream;import java.io.inputstreamreader;import Java.net.httpurlconnection;import Java.net.url;import Java.net.urlencoder;public class Interfacetest {//Public Static final String post_url = "http://112.4.27.9/mall-back/if_user/store_list";//Coup test interface//public static final string Post_url = "Http://121.40.204.191:8180/mdserver/service/installLock"; /** * Interface Call GET */public static void Httpurlconectionget (String get_url) {try {URL url = ne W URL (Get_url); Convert string to URL request address httpurlconnection connection = (httpurlconnection) url.openconnection ();//Open Connection Co Nnection.connect ();//Connect session//Get input stream//GBK to encoded format, you can modify BufferedReader br = new Bufferedreade R (New InputStreamReader (Connection.getinputstream (), "GBK")); String Line; StringBuilder sb = new StringBuilder (); while (line = Br.readline ()) = null) {// Loop read Stream Sb.append (line); } br.close ();//Close stream connection.disconnect ();//Disconnect System.out.println (sb.tostring ()); System.out.println (New String (Sb.tostring (). GetBytes ())); } catch (Exception e) {e.printstacktrace (); SYSTEM.OUT.PRINTLN ("Failed!"); }}/** * Interface call POST */public static void Httpurlconnectionpost (String post_url) {try { URL url = new URL (post_url); The URLConnection connection returned by the URL in the open method is strongly converted to a httpurlconnection connection (identifying the remote object connection referenced by a URL) httpurlconnection connection = (httpurlconnection) url.openconnection ();//At this time cnnection just for a connection object, to connect in//set the connection output stream to True, default false (post The request is implicitly passed as a stream of parameters) Connection.setdooutput (true); Set the connection input stream to True connection.setdoinput (true); Set the request mode to post Connection.setrequestmethod ("POST"); The POST request cache is set to False connection.setusecaches (false); Sets whether the HttpURLConnection instance automatically performs redirection connection.setinstancefollowredirects (true); Set the various properties within the request header (the following is the type of content set, set to the urlencoded encoded from parameter)//application/x-javascript Text/xml->xml data Applicati On/x-javascript->json object application/x-www-form-urlencoded-> form data//; Charset=utf-8 has to be, otherwise there will be garbled "★★★★★" Connection.setrequestproperty ("Content-type", "application/x-www-form-urlencoded;charset=utf-8"); Establish a connection (the request does not start until the Connection.getinputstream () method call is initiated, the above parameter settings need to be made before this method) Connection.connect (); Create an input/output stream for outputting the parameters that are carried in the connection, (the output is what follows) DataOutputStream dataout = new DataOutputStream (conne Ction.getoutputstream ()); String App_key = "app_key=" + urlencoder.encode ("4f7bf8c8260124e6e9c6bf094951a111", "utf-8");/modified "changed to error data to avoid information disclosure" String agt_num = "&agt_num=" + urlencoder.encode ("10111", "utf-8");//Modified "changed to error data to avoid information disclosure" string pid = "&A Mp;pid= "+ urlencoder.encode (" BLZXA150401111 "," utf-8 ");//Modified" changed to error data to avoid information disclosure "String Departid =" &departid= " + Urlencoder.encode ("10007111", "utf-8");//Modified "changed to error data to avoid information disclosure" String install_lock_name = "&install_lock_na Me= "+ Urlencoder.encode (" South Day Gate "," Utf-8 "); String install_address = "&install_address=" + urlencoder.encode ("Beijing Yu Xin", "utf-8"); String Install_gps = "&install_gps=" + urlencoder.encode ("116.350888,40.011001", "utf-8"); String install_work = "&install_work=" + Urlencoder.encode ("Xiao Li", "Utf-8"); String install_telete = "&install_telete=" + urlencoder.encode ("13000000000", "utf-8"); String Intall_comm = "&intall_comm=" + urlencoder.encode ("All normal", "utf-8"); Format parm = aaa=111&bbb=222&ccc=333&ddd=444 String parm = App_key+ agt_num+ pid+ departid+ install_lock_name+ install_address+ install_gps+ install_work+ install_telete+ Intall_comm; Output the parameters to the connection dataout.writebytes (parm); Refresh and close the stream Dataout.flush () after the output is complete; Dataout.close (); Important and easy to ignore steps (close the stream, remember!) System.out.println (Connection.getresponsecode ()); The connection initiates the request, processing the server response (from the connection gets to the input stream and wraps it as BufferedReader) bufferedreader bf = new BufferedReader (New InputStreamReader (c Onnection.getinputstream (), "UTF-8")); String Line; StringBuilder sb = new StringBuilder (); Used to store the response data//loop read stream, if not to the end (line = Bf.readline ()) = null) {//SB . Append (Bf.readline ()); Sb.append (line) Append (System.getproperty ("Line.separator")); } bf.close (); Important and easy to ignore steps (close the stream, remember!) Connection.disconnect (); Destroy Connection System.out.println (sB.tostring ()); } catch (Exception e) {e.printstacktrace (); }} public static void Main (string[] args) {//Httpurlconectionpost ("http://121.40.204.191:8180/mdserver/ Service/installlock "); Httpurlconectionget ("https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=123&json=1&p=3&sid= 1438_21098_17001_22159&req=2&pbs=12306&csor=3&pwd=12&cb=jquery110208408865132022476_ 1508200165647&_=1508200165650 "); }}
Java Tuning Interface