Import Java.io.ioexception;import Java.net.malformedurlexception;import Org.xml.sax.saxexception;import Com.meterware.httpunit.getmethodwebrequest;import Com.meterware.httpunit.postmethodwebrequest;import Com.meterware.httpunit.webconversation;import Com.meterware.httpunit.webform;import Com.meterware.httpunit.weblink;import Com.meterware.httpunit.webrequest;import Com.meterware.httpunit.webresponse;import Com.meterware.httpunit.webtable;public class HttpUnitTestSample {/** * Page Content Test * @throws malformedurlexception * @throws ioexception * @throws saxexception */public static void Testgethtmlconten T () throws Malformedurlexception,ioexception, saxexception {System.out.println ("Direct access to Web content:");// Establish a webconversation instance webconversation WC = new Webconversation ();//Make a request to the specified URL, get the response WebResponse WR = Wc.getresponse (" Http://www.baidu.com ")////////////////Use GetText method to obtain the corresponding Contents//Use the SYSTEM.OUT.PRINTLN to print the acquired content on the console System.out.println (Wr.gettext ()) ;} /** * Get page content with Get method * @throws malformedurlexception * @throws IOException * @throws saxexception */public static void Testgetmethod () throws Malformedurlexception,ioexception, saxexception {Syst EM.OUT.PRINTLN ("Send data to server and get Web content:");//Establish a webconversation instance webconversation WC = new Webconversation ();// Make a request to the specified URL webrequest req = new Getmethodwebrequest ("http://localhost:8080/test.html");//Add a reference to the request Req.setparameter (" Query "," "carbon tetrachloride");//Gets the response object WebResponse resp = wc.getresponse (req);//Use GetText method to get all the corresponding Contents// Print the acquired content on the console using System.out.println System.out.println (Resp.gettext ());} /** * Get page content with POST method * @throws malformedurlexception * @throws ioexception * @throws saxexception */public static void test Postmethod () throws Malformedurlexception,ioexception, saxexception {System.out.println ("Send data to server using post, Then get the Web content: ");//Create a webconversation instance webconversation WC = new Webconversation ();//Make a request to the specified URL webrequest req = new Postmethodwebrequest ("http://localhost:8080/test.html");//Add the number of references to the request Req.setparameter ("user_name", "test"); Req.setparameter ("Password", "111111");//Get Response objectWebResponse resp = wc.getresponse (req);//Use the GetText method to obtain the corresponding Contents//use SYSTEM.OUT.PRINTLN to print the content obtained on the console System.out.println ( Resp.gettext ());} /** * Get page link and simulate click * @throws malformedurlexception * @throws ioexception * @throws saxexception */public static void Testcl Icklink () throws Malformedurlexception,ioexception, saxexception {System.out.println ("Get page link to page content:");// Establish a webconversation instance webconversation WC = new Webconversation ();//Get Response object WebResponse resp = Wc.getresponse ("http:// www.265.com/");//Get page link object weblink link = resp.getlinkwith (" Baidu ");//Simulate user Click event Link.click ();//Get current Response object WebResponse NextLink = Wc.getcurrentpage ();//Use the GetText method to obtain the corresponding Contents//Use the SYSTEM.OUT.PRINTLN to print the content obtained on the console System.out.println ( Nextlink.gettext ());} /** * Get table contents of page Contents * @throws malformedurlexception * @throws ioexception * @throws saxexception */public static void tes Ttablecontent () throws Malformedurlexception,ioexception, saxexception {System.out.println ("Get the contents of the table on the page:");// Create a webconversation instance webconversation WC = new WebconvErsation ();//Gets the response object WebResponse resp = Wc.getresponse ("Http://www.w3school.com.cn/tiy/loadtext.asp?f=html_table_ Test "); System.out.println (Resp.gettext ());//get the corresponding Table object webtable webtable = Resp.gettables () [0];//Pass the contents of the Table object to the string array string[][ ] Datas = Webtable.astext ();//cyclic display of table contents int i = 0, j = 0;int m = datas[0].length;int n = datas.length;while (i < n) {j = 0;while (J < m) {System.out.println ("table" + (i + 1) + "line" + (j + 1) + "The contents of the column are:" + datas[i][j]); ++j;} ++i;}} /** * Get form Control contents of page * @throws malformedurlexception * @throws ioexception * @throws saxexception */public static void testht Mlcontentform () throws Malformedurlexception,ioexception, saxexception {System.out.println ("Get the contents of the form on the page:");// Establish a webconversation instance webconversation WC = new Webconversation ();//Get Response object WebResponse resp = Wc.getresponse ("http:// Www.w3school.com.cn/tiy/t.asp?f=html_table_test "); System.out.println (Resp.gettext ());//Get the corresponding form object WebForm WebForm = Resp.getforms () [0];//get the name of all controls in the form string[] Pnames = Webform.getparameternAmes (); int i = 0;int m = pnames.length;//Loop Displays the contents of all controls in the form while (I < m) {System.out.println ("+" + (i + 1) + "control name is" + P Names[i] + ", inside content is" + (Webform.getparametervalues (pnames[i))); ++i;}} public static void Main (string[] args) throws Malformedurlexception,ioexception, saxexception {//Testgethtmlcontent () ;//Testgetmethod ();//Testpostmethod ();//Testclicklink ();//Testtablecontent (); Testhtmlcontentform ();}}
HttpUnit using Demo sample