vickzhu

            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            151 隨筆 :: 0 文章 :: 34 評(píng)論 :: 0 Trackbacks
          <2008年11月>
          2627282930311
          2345678
          9101112131415
          16171819202122
          23242526272829
          30123456

          常用鏈接

          留言簿(6)

          隨筆檔案

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          以下都是實(shí)戰(zhàn)經(jīng)驗(yàn):
          1、Socket讀取
                String strServer=http://www.google.cn;//這里同樣可以用ip來(lái)訪問(wèn):203.208.35.100
                String strPage="/language_tools?hl=zh-CN";
                try { 
                     String hostname = strServer; 
                     int port = 80; 
                     InetAddress addr = InetAddress.getByName(hostname); 
                     Socket socket = new Socket(addr, port);
                     BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8")); 
                     wr.write("GET " + strPage + " HTTP/1.0\r\n"); 
                     wr.write("HOST:" + strServer + "\r\n"); 
                     wr.write("\r\n"); 
                     wr.flush();
                     BufferedReader rd = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                     String line; 
                     while ((line = rd.readLine()) != null) { 
                          System.out.println(line); 
                     }
                     wr.close(); 
                     rd.close(); 
                } catch (Exception e) { 
                     System.out.println(e.toString()); 
                }
          2、HttpClient方式
                HttpClient client=new HttpClient();
                GetMethod method=new GetMethod("       int status=client.executeMethod(method);
                if(status==HttpStatus.SC_OK){
                 //讀取內(nèi)容
                 byte[] responseBody = method.getResponseBody();
                 //處理內(nèi)容
                 System.out.println(new String(responseBody));
                 System.out.println("文件名稱:"+method.getPath());
                  }
          3、HttpURLConnection方式
                URL url = new URL("這里是你要連接的地址");
                HttpURLConnection conn = (HttpURLConnection)url.openConnection();
                conn.setDoOutput(true);//是否可用于輸出(輸出參數(shù)),默認(rèn)為fasle。另:setDoInput()為是否可用于輸入,默認(rèn)為true
                String parameters = "name=admin&password=123456";//這里是要傳遞的參數(shù)
                OutputStream os = conn.getOutputStream();
                os.write(parameters.getBytes("utf-8"));
                os.flush();
                os.close();
                System.out.println("返回狀態(tài)碼:"+conn.getResponseCode());
                System.out.println("返回消息:"+conn.getResponseMessage());
                InputStream is = conn.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(is,"utf-8"));
                String line = null;
                while((line=br.readLine())!=null){
                 System.out.println(line);
                }
              //  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();;
              //  DocumentBuilder db = dbf.newDocumentBuilder();
              //  Document doc = db.parse(is);
          如果誰(shuí)還有更多的方式分享,請(qǐng)留言!

          評(píng)論

          # re: java 讀取頁(yè)面源碼 的多種方式[未登錄] 2008-11-24 20:39 fisher
          GetMethod method=new GetMethod("http://www.baidu.com/");

          GetMethod ?

          這個(gè)東西哪來(lái)的?  回復(fù)  更多評(píng)論
            

          # re: java 讀取頁(yè)面源碼 的多種方式 2008-11-25 09:03 vickzhu
          你好HttpClient和GetMethod都是來(lái)自apache的包  回復(fù)  更多評(píng)論
            


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 满洲里市| 修文县| 巴马| 商水县| 武鸣县| 弋阳县| 新干县| 松潘县| 抚远县| 府谷县| 米泉市| 铜陵市| 资阳市| 杭锦后旗| 恩平市| 通海县| 延安市| 宣化县| 马关县| 瑞昌市| 新田县| 大余县| 五家渠市| 峨眉山市| 通州市| 松原市| 称多县| 丹凤县| 五大连池市| 育儿| 武威市| 义马市| 上饶县| 长白| 舒城县| 香河县| 东方市| 札达县| 山阴县| 保靖县| 珠海市|