BlogJava 首頁 新隨筆 聯系 聚合 管理
            37 Posts :: 64 Stories :: 21 Comments :: 0 Trackbacks
          1. package  httpclienttest;  
          2.   
          3. import  org.apache.commons.httpclient .Cookie;  
          4. import  org.apache.commons.httpclient .HttpClient ;  
          5. import  org.apache.commons.httpclient .NameValuePair;  
          6. import  org.apache.commons.httpclient .methods.GetMethod;  
          7. import  org.apache.commons.httpclient .methods.PostMethod;  
          8.   
          9. public   class  LoginKaixin {  
          10.     private   static   final  String LOGON_SITE =  "http://www.kaixin001.com" ;  
          11.     private   static   final   int  LOGON_PORT =  80 ;  
          12.     public   static   void  main(String[] args) throws  Exception {  
          13.         HttpClient  client = new  HttpClient ();  
          14.         client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT);  
          15.           
          16.         //登錄頁面   
          17.         PostMethod post = new  PostMethod( "http://www.kaixin001.com/login/login.php" );  
          18.         NameValuePair ie = new  NameValuePair( "User-Agent" , "Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)" );     
          19.         NameValuePair url = new  NameValuePair( "url" "/home/" );  
          20.         NameValuePair username = new  NameValuePair( "email" , "xxx@163.com" );  
          21.         NameValuePair password = new  NameValuePair( "password" "xxxxxx" );  
          22.     post.setRequestBody(new  NameValuePair[] { ie,url,username, password});  
          23.         client.executeMethod(post);  
          24.         System.out.println("******************************登錄******************************" );  
          25.         Cookie[] cookies = client.getState().getCookies();  
          26.         client.getState().addCookies(cookies);  
          27.         post.releaseConnection();  
          28.         System.out.println("******************************頁面轉向******************************" );  
          29.         String newUrl="http://www.kaixin001.com/home/" ;  
          30.         System.out.println("==========Cookies============" );  
          31.         int  i= 0 ;  
          32.         for (Cookie c:cookies){  
          33.             System.out.println(++i+":   " +c);  
          34.         }  
          35.         client.getState().addCookies(cookies);  
          36.         post.releaseConnection();  
          37.         GetMethod get = new  GetMethod(newUrl);  
          38.         get.setRequestHeader("Cookie" , cookies.toString());  
          39.         client.executeMethod(get);  
          40.         String responseString = get.getResponseBodyAsString();  
          41.         //登錄后首頁的內容   
          42.         System.out.println(responseString);  
          43.         get.releaseConnection();  
          44.         System.out.println("******************************組件功能******************************" );  
          45.         //"http://www.kaixin001.com/!slave/index.php", "朋友買賣"   
          46.         //"http://www.kaixin001.com/!parking/index.php", "爭車位"   
          47.         //"http://www.kaixin001.com/!house/index.php?_lgmode=pri", "買房子"    
          48.         //http://www.kaixin001.com/!house/index.php?_lgmode=pri&t=49   
          49.         //"http://www.kaixin001.com/!house/garden/index.php","花園"   
          50.         //(1)進入朋友買賣****************   
          51.         System.out.println("******************************(1)進入朋友買賣******************************" );  
          52.         String slave="http://www.kaixin001.com/!slave/index.php" ;  
          53.         get = new  GetMethod(slave);  
          54.         get.setRequestHeader("Cookie" , cookies.toString());  
          55.         client.executeMethod(get);  
          56.         responseString = get.getResponseBodyAsString();  
          57.         System.out.println(responseString);  
          58.         get.releaseConnection();  
          59.         //(2)進入爭車位****************   
          60.         System.out.println("******************************(2)進入爭車位******************************" );  
          61.         String parking="http://www.kaixin001.com/!parking/index.php" ;  
          62.         get = new  GetMethod(parking);  
          63.         get.setRequestHeader("Cookie" , cookies.toString());  
          64.         client.executeMethod(get);  
          65.         responseString = get.getResponseBodyAsString();  
          66.         System.out.println(responseString);  
          67.         get.releaseConnection();  
          68.         //(3)進入買房子****************   
          69.         System.out.println("******************************(3)進入買房子*******************************" );  
          70.         String house="http://www.kaixin001.com/!house/index.php?_lgmode=pri&t=49" ;  
          71.         get = new  GetMethod(house);  
          72.         get.setRequestHeader("Cookie" , cookies.toString());  
          73.         client.executeMethod(get);  
          74.         responseString = get.getResponseBodyAsString();  
          75.         System.out.println(responseString);  
          76.         get.releaseConnection();  
          77.         //(4)進入花園****************   
          78.         System.out.println("******************************(4)進入花園*******************************" );  
          79.         String garden="http://www.kaixin001.com/!house/garden/index.php" ;  
          80.         get = new  GetMethod(garden);  
          81.         get.setRequestHeader("Cookie" , cookies.toString());  
          82.         client.executeMethod(get);  
          83.         responseString = get.getResponseBodyAsString();  
          84.         System.out.println(responseString);  
          85.         get.releaseConnection();  
          86.           
          87.           
          88.     }  
          89.   

          posted on 2009-08-21 12:27 xiachang88 閱讀(3716) 評論(20)  編輯  收藏

          Feedback

          # re: httpclient登錄開心網 2009-08-25 20:31 文文
          開心網當然開心喲。嘿嘿!  回復  更多評論
            

          # re: httpclient登錄開心網 2009-08-26 19:52 郭壇
          還不錯  回復  更多評論
            

          # re: httpclient登錄開心網 2009-09-05 15:46 雪飄飄
          123  回復  更多評論
            

          # re: httpclient登錄開心網 2009-09-07 23:36
          sagt erg fdg  回復  更多評論
            

          # re: httpclient登錄開心網 2009-09-08 13:45 帶刺的玫瑰
          我要進去玩  回復  更多評論
            

          # re: httpclient登錄開心網 2009-09-09 22:20 王靖
          請同意  回復  更多評論
            

          # re: httpclient登錄開心網 2009-09-09 22:21 王靖
          你好請同意  回復  更多評論
            

          # re: httpclient登錄開心網 2009-09-15 15:45 李小林
          就是想玩   回復  更多評論
            

          # re: httpclient登錄開心網 2009-09-19 10:46 王賽媛
          恩恩  回復  更多評論
            

          # re: httpclient登錄開心網[未登錄] 2009-09-21 03:47 強子
          怎么進去玩啊  回復  更多評論
            

          # re: httpclient登錄開心網[未登錄] 2009-09-21 19:12 強子
          請同意

            回復  更多評論
            

          # re: httpclient登錄開心網 2009-09-28 20:36 楊新華
          123  回復  更多評論
            

          # re: httpclient登錄開心網 2009-10-07 19:54 李娜
          th/k988  回復  更多評論
            

          # re: httpclient登錄開心網 2009-10-27 22:05 吳燕
          你好請同意,聽說很好耍  回復  更多評論
            

          # re: httpclient登錄開心網 2009-10-27 22:14 吳燕
          聽說很好耍  回復  更多評論
            

          # re: httpclient登錄開心網 2009-11-10 18:43 匿名
          代碼試了一下,登錄好像不行呀。在網上看了好多示例代碼,好像還沒成的  回復  更多評論
            

          # re: httpclient登錄開心網 2009-11-14 23:56 草爽秀
          偶想玩玩  回復  更多評論
            

          # re: httpclient登錄開心網 2009-12-09 12:39 j88888888
          不知帶好玩嗎  回復  更多評論
            

          # re: httpclient登錄開心網 2009-12-30 09:04 這段代碼有錯誤
          這段代碼有錯誤  回復  更多評論
            

          # re: httpclient登錄開心網[未登錄] 2012-02-09 17:29 李莫愁
          代碼不能實現登錄。  回復  更多評論
            


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 深泽县| 商水县| 论坛| 通山县| 顺平县| 松原市| 桐梓县| 敖汉旗| 泗阳县| 炉霍县| 荃湾区| 铜川市| 玛纳斯县| 金乡县| 陆丰市| 班戈县| 璧山县| 紫金县| 马鞍山市| 开平市| 阳东县| 法库县| 抚顺县| 吴堡县| 金溪县| 天祝| 易门县| 通河县| 东源县| 壶关县| 云梦县| 裕民县| 明光市| 津南区| 芜湖县| 许昌市| 新化县| 神农架林区| 冀州市| 阳城县| 大庆市|