當(dāng)柳上原的風(fēng)吹向天際的時(shí)候...

          真正的快樂來源于創(chuàng)造

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            368 Posts :: 1 Stories :: 201 Comments :: 0 Trackbacks
          HttpUnit網(wǎng)址:http://httpunit.sourceforge.net

          要在工程中使用HttpUnit,需要下載httpunit1.7.0.zip包,并把其中l(wèi)ib及jars目錄中的包加載到工程中。

          連接網(wǎng)頁并取得網(wǎng)頁文本示例:
          // 打開一個(gè)Web會(huì)話,WebConversation的實(shí)例模擬一個(gè)Web瀏覽器,用來請(qǐng)求一個(gè)Web資源
                  WebConversation wc=new WebConversation();
                  
                  
          // 向網(wǎng)址http://www.baidu.com發(fā)出請(qǐng)求
                  WebRequest req=new GetMethodWebRequest("http://www.baidu.com");
                  
                  
          try {
                      
          // 獲得http://www.baidu.com的響應(yīng)
                      WebResponse resp=wc.getResponse(req);
                      
                      
          // 打印表示整個(gè)Web頁面的字符串
                      System.out.println(resp.getText());
                      
                      
          // resp.getDOM()方法返回一個(gè)表示整個(gè)Web頁面的XML文檔
                      
          // resp.getForms()方法返回Web頁面的中的表單數(shù)組
                      
          // resp.getTables()方法返回Web頁面的中的表格數(shù)組
                      
          // resp.getLinks()方法返回Web頁面的中的鏈接數(shù)組
                      
          // resp.getImages()方法返回Web頁面的中的圖片數(shù)組
                  }
           catch (IOException e) {
                      e.printStackTrace();
                  }
           catch (SAXException e) {
                      e.printStackTrace();
                  }

          模擬在百度輸入“金融危機(jī)”并取得查詢結(jié)果的例子:
          WebConversation wc=new WebConversation();
                  
                  WebRequest request
          =new GetMethodWebRequest("http://www.baidu.com");
                  
          try {
                      WebResponse response
          =wc.getResponse(request);
                      
                      
          // 取得Web頁面中第一個(gè)Form
                      WebForm form=response.getForms()[0];
                      request
          =form.getRequest();
                      
                      
          // 設(shè)置查詢主題為"金融危機(jī)“,wd是查詢文本框的name
                      request.setParameter("wd""金融危機(jī)");
                      
                      
          // 取得并在控制臺(tái)打印響應(yīng)
                      response=wc.getResponse(request);
                      System.out.println(response.getText());
                  }
           catch (IOException e) {
                      e.printStackTrace();
                  }
           catch (SAXException e) {
                      e.printStackTrace();
                  }


          posted on 2009-02-17 15:08 何楊 閱讀(405) 評(píng)論(0)  編輯  收藏

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 霍林郭勒市| 梧州市| 治多县| 德兴市| 六盘水市| 新安县| 平定县| 汤原县| 天水市| 黄浦区| 南丹县| 黑河市| 昌邑市| 洛扎县| 灵丘县| 永泰县| 沂南县| 天峻县| 舟曲县| 怀柔区| 沧源| 宁城县| 新巴尔虎左旗| 修文县| 庆城县| 嘉黎县| 六枝特区| 潍坊市| 石屏县| 鲁甸县| 南城县| 江西省| 共和县| 大宁县| 绵阳市| 安平县| 聂荣县| 蓝田县| 砀山县| 陇西县| 观塘区|