httpunit使用示範範例

來源:互聯網
上載者:User

標籤:

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 {/** * 頁面內容測試 * @throws MalformedURLException * @throws IOException * @throws SAXException */public static void testGetHtmlContent() throws MalformedURLException,IOException, SAXException {System.out.println("直接擷取網頁內容:");// 建立一個WebConversation執行個體WebConversation wc = new WebConversation();// 向指定的URL發出請求,擷取響應WebResponse wr = wc.getResponse("http://www.baidu.com");// 用getText方法擷取對應的所有內容// 用System.out.println將擷取的內容列印在控制台上System.out.println(wr.getText());}/** * 用get方法擷取頁面內容 * @throws MalformedURLException * @throws IOException * @throws SAXException */public static void testGetMethod() throws MalformedURLException,IOException, SAXException {System.out.println("向server發送資料,然後擷取網頁內容:");// 建立一個WebConversation執行個體WebConversation wc = new WebConversation();// 向指定的URL發出請求WebRequest req = new GetMethodWebRequest("http://localhost:8080/test.html");// 給請求加上參數req.setParameter("query", "四氯化碳");// 擷取響應對象WebResponse resp = wc.getResponse(req);// 用getText方法擷取對應的所有內容// 用System.out.println將擷取的內容列印在控制台上System.out.println(resp.getText());}/** * 用post方法擷取頁面內容 * @throws MalformedURLException * @throws IOException * @throws SAXException */public static void testPostMethod() throws MalformedURLException,IOException, SAXException {System.out.println("使用Post方式向server發送資料,然後擷取網頁內容:");// 建立一個WebConversation執行個體WebConversation wc = new WebConversation();// 向指定的URL發出請求WebRequest req = new PostMethodWebRequest("http://localhost:8080/test.html");// 給請求加上參數req.setParameter("user_name", "test");req.setParameter("password", "111111");// 擷取響應對象WebResponse resp = wc.getResponse(req);// 用getText方法擷取對應的所有內容// 用System.out.println將擷取的內容列印在控制台上System.out.println(resp.getText());}/** * 擷取頁面連結並類比點擊 * @throws MalformedURLException * @throws IOException * @throws SAXException */public static void testClickLink() throws MalformedURLException,IOException, SAXException {System.out.println("擷取頁面中連結指向頁面的內容:");// 建立一個WebConversation執行個體WebConversation wc = new WebConversation();// 擷取響應對象WebResponse resp = wc.getResponse("http://www.265.com/");// 獲得頁面連結化物件WebLink link = resp.getLinkWith("百度");// 類比使用者單擊事件link.click();// 獲得當前的響應對象WebResponse nextLink = wc.getCurrentPage();// 用getText方法擷取對應的所有內容// 用System.out.println將擷取的內容列印在控制台上System.out.println(nextLink.getText());}/** * 擷取頁面內容的table內容 * @throws MalformedURLException * @throws IOException * @throws SAXException */public static void testTableContent() throws MalformedURLException,IOException, SAXException {System.out.println("擷取頁面中表格的內容:");// 建立一個WebConversation執行個體WebConversation wc = new WebConversation();// 擷取響應對象WebResponse resp = wc.getResponse("http://www.w3school.com.cn/tiy/loadtext.asp?f=html_table_test");System.out.println(resp.getText());// 獲得對應的表格對象WebTable webTable = resp.getTables()[0];// 將表格對象的內容傳遞給字串數組String[][] datas = webTable.asText();// 迴圈顯示表格內容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("表格中第" + (i + 1) + "行第" + (j + 1) + "列的內容是:"+ datas[i][j]);++j;}++i;}}/** * 擷取頁面的表單控制項內容 * @throws MalformedURLException * @throws IOException * @throws SAXException */public static void testHtmlContentForm() throws MalformedURLException,IOException, SAXException {System.out.println("擷取頁面中表單的內容:");// 建立一個WebConversation執行個體WebConversation wc = new WebConversation();// 擷取響應對象WebResponse resp = wc.getResponse("http://www.w3school.com.cn/tiy/t.asp?f=html_table_test");System.out.println(resp.getText());// 獲得對應的表單對象WebForm webForm = resp.getForms()[0];// 獲得表單中所有控制項的名字String[] pNames = webForm.getParameterNames();int i = 0;int m = pNames.length;// 迴圈顯示表單中所有控制項的內容while (i < m) {System.out.println("第" + (i + 1) + "個控制項的名字是" + pNames[i] + ",裡面的內容是"+ (webForm.getParameterValues(pNames[i])));++i;}}public static void main(String[] args) throws MalformedURLException,IOException, SAXException {// testGetHtmlContent();// testGetMethod();// testPostMethod();// testClickLink();// testTableContent();testHtmlContentForm();}}

httpunit使用示範範例

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.