Personal Learning use: careful reference
1 Client class
Import Com.thoughtworks.gauge.step;import Com.thoughtworks.gauge.table;import Com.thoughtworks.gauge.TableRow; Import Java.io.bufferedreader;import Java.io.ioexception;import Java.io.inputstreamreader;import Java.io.printwriter;import Java.net.httpurlconnection;import Java.net.url;import Java.net.URLEncoder;import Java.util.hashmap;import java.util.list;import java.util.map;/** * Created by KSC on 2/7/2017. */public class TestClient {//send GET request/** * * @param url Destination address * @param parameter STable Request Parameters * @return Remote response result */@Step ("The Get method URL is <url> and para Meters is <parametersTable> "public static string Sendget (String url,table parameterstable) {Map<stri ng,string> parameters = new hashmap<> (); For (TableRow row:parametersTable.getTableRows ()) {String key = Row.getcell ("name"); String value = Row.getcell ("Age"); Parameters.puT (Key,value); } for (String S:parameters.keyset ()) {System.out.println (s+ "\ T" +parameters.get (s)); } String result = "";//Results returned BufferedReader in = null;//Read response input stream stringbuffer SF = new StringBuffer () ;//Storage parameter String params = "";//The encoded parameter try{if (parameters.size () = = 1) {for (string n Ame:parameters.keySet ()) {sf.append (name). Append ("="). Append (Java.net.URL Encoder.encode (Parameters.get (name), "UTF-8")); } params = sf.tostring (); }else{for (String Name:parameters.keySet ()) {sf.append (name). Append ("="). Append ( Java.net.URLEncoder.encode (Parameters.get (name), "UTF-8")). Append ("&"); } String Tempparams = Sf.tostring (); params = tempparams.substring (0,tempparams.lengtH ()-1); SYSTEM.OUT.PRINTLN (params); } String fullurl = URL + "?" + params; System.out.println (FullUrl); Create URL object url connurl = new URL (fullurl); Open URL link httpurlconnection httpconn = (httpurlconnection) connurl.openconnection (); Set Common Properties Httpconn.setrequestproperty ("Accept", "*/*"); Httpconn.setrequestproperty ("Connection", "keep-alive"); Httpconn.setrequestproperty ("User-agent", "mozilla/4.0" (compatible; MSIE 8.0; Windows NT 6.1) "); Establish the actual link httpconn.connect (); The response head gets map<string,list<string>> headers = Httpconn.getheaderfields (); System.out.println ("=============================="); Traverse header all fields for (String Key:headers.keySet ()) {System.out.println (key+ "\ t: \ t" +headers.get (key)) ; }//define the BufferedReader input stream to read the response of the URL and set the encoding method in = new BufferedReader (New InputStreamReader (Httpconn.getinputstream (), "UTF-8")); String line = "";//reads the contents of the returned while (line = In.readline ()) = null) {result = Result+line; }}catch (Exception e) {e.printstacktrace (); }finally{try{if (in = null) {in.close (); }}catch (IOException ex) {ex.printstacktrace (); }} System.out.println ("*****************************"); SYSTEM.OUT.PRINTLN (result); System.out.println ("=============================="); return result; }//send Post Request@step ("The Post method URL is <url> and parameters be <parametersTable>") public Stati C string Sendpost (String url,table parameterstable) {map<string,string> parameters = new hashmap<> (); For (TableRow row:parametersTable.getTableRows ()) {String key = Row.getcell ("Name"); String value = Row.getcell ("Age"); Parameters.put (Key,value); } for (String S:parameters.keyset ()) {System.out.println (s+ "\t\t" +parameters.get (s)); } System.out.println ("---------------------"); String result = ""; BufferedReader in = null; PrintWriter out = null;//stringbuffer SF = new StringBuffer (); String params = ""; try{if (parameters.size () = = 1) {for (String Name:parameters.keySet ()) {sf. Append (name). Append ("="). Append (Java.net.URLEncoder.encode (parameters.get (name), "UTF-8") ); } params = sf.tostring (); }else{for (String Name:parameters.keySet ()) {sf.append (name). Append ("="). Append ( Java.net.URLEncoder.encode (Parameters.get (name), "UTF-8")). Append ("&"); } String tempparams = sf.tostring (); params = tempparams.substring (0,tempparams.length ()-1); } URL connurl = new URL (URL); HttpURLConnection httpconn = (httpurlconnection) connurl.openconnection (); Httpconn.setrequestproperty ("Accept", "*/*"); Httpconn.setrequestproperty ("Connection", "keep-alive"); Httpconn.setrequestproperty ("User-agent", "mozilla/4.0" (compatible; MSIE 8.0; Windows NT 6.1) "); Set Post mode Httpconn.setdoinput (TRUE); Httpconn.setdooutput (TRUE); Gets the output stream corresponding to the HttpURLConnection object out = new PrintWriter (Httpconn.getoutputstream ()); Send request parameter out.write (params); Flush (flush) The buffered Out.flush () in the output stream; in = new BufferedReader (New InputStreamReader (Httpconn.getinputstream (), "UTF-8")); String line = ""; while (line = In.readline ()) = null) {result = Result+line; }}catch (Exception e) {e.printstacktrace (); }finally{try{if (out! = null) {out.close (); } if (in = null) {in.close (); }}catch (IOException e) {e.printstacktrace (); }} System.out.println ("*****************************"); SYSTEM.OUT.PRINTLN (result); System.out.println ("============================"); return result; }}
2. Test class
Test with an automated test tool gauge
Use HTTP request for creating user=====================created by KSC on 2/7/2017this are an executable specification file which follows Markdown syntax. Every heading in the this file denotes a scenario. Every bulleted point denotes a step.call in URL with method get----------------tags:get*the Get method URL is "Http://loc alhost:8080 "and Parameters are|name|age| | ----|---|| tom|20| | Maike|22|call on URL with Method post---------------------------tags:post*the Post method URL is "https://www.baidu.com/ "and Parameters are|name|age| | ----|---|| marry|20| | rose|22|
The Java background simulates sending HTTP GET and POST requests and testing