BlogJava 首頁(yè) 新隨筆 聯(lián)系 聚合 管理
            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.         //登錄頁(yè)面   
          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("******************************頁(yè)面轉(zhuǎn)向******************************" );  
          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.         //登錄后首頁(yè)的內(nèi)容   
          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", "爭(zhēng)車位"   
          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)進(jìn)入朋友買賣****************   
          51.         System.out.println("******************************(1)進(jìn)入朋友買賣******************************" );  
          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)進(jìn)入爭(zhēng)車位****************   
          60.         System.out.println("******************************(2)進(jìn)入爭(zhēng)車位******************************" );  
          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)進(jìn)入買房子****************   
          69.         System.out.println("******************************(3)進(jìn)入買房子*******************************" );  
          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)進(jìn)入花園****************   
          78.         System.out.println("******************************(4)進(jìn)入花園*******************************" );  
          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 閱讀(3718) 評(píng)論(20)  編輯  收藏

          Feedback

          # re: httpclient登錄開心網(wǎng) 2009-08-25 20:31 文文
          開心網(wǎng)當(dāng)然開心喲。嘿嘿!  回復(fù)  更多評(píng)論
            

          # re: httpclient登錄開心網(wǎng) 2009-08-26 19:52 郭壇
          還不錯(cuò)  回復(fù)  更多評(píng)論
            

          # re: httpclient登錄開心網(wǎng) 2009-09-05 15:46 雪飄飄
          123  回復(fù)  更多評(píng)論
            

          # re: httpclient登錄開心網(wǎng) 2009-09-07 23:36
          sagt erg fdg  回復(fù)  更多評(píng)論
            

          # re: httpclient登錄開心網(wǎng) 2009-09-08 13:45 帶刺的玫瑰
          我要進(jìn)去玩  回復(fù)  更多評(píng)論
            

          # re: httpclient登錄開心網(wǎng) 2009-09-09 22:20 王靖
          請(qǐng)同意  回復(fù)  更多評(píng)論
            

          # re: httpclient登錄開心網(wǎng) 2009-09-09 22:21 王靖
          你好請(qǐng)同意  回復(fù)  更多評(píng)論
            

          # re: httpclient登錄開心網(wǎng) 2009-09-15 15:45 李小林
          就是想玩   回復(fù)  更多評(píng)論
            

          # re: httpclient登錄開心網(wǎng) 2009-09-19 10:46 王賽媛
          恩恩  回復(fù)  更多評(píng)論
            

          # re: httpclient登錄開心網(wǎng)[未登錄] 2009-09-21 03:47 強(qiáng)子
          怎么進(jìn)去玩啊  回復(fù)  更多評(píng)論
            

          # re: httpclient登錄開心網(wǎng)[未登錄] 2009-09-21 19:12 強(qiáng)子
          請(qǐng)同意

            回復(fù)  更多評(píng)論
            

          # re: httpclient登錄開心網(wǎng) 2009-09-28 20:36 楊新華
          123  回復(fù)  更多評(píng)論
            

          # re: httpclient登錄開心網(wǎng) 2009-10-07 19:54 李娜
          th/k988  回復(fù)  更多評(píng)論
            

          # re: httpclient登錄開心網(wǎng) 2009-10-27 22:05 吳燕
          你好請(qǐng)同意,聽說很好耍  回復(fù)  更多評(píng)論
            

          # re: httpclient登錄開心網(wǎng) 2009-10-27 22:14 吳燕
          聽說很好耍  回復(fù)  更多評(píng)論
            

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

          # re: httpclient登錄開心網(wǎng) 2009-11-14 23:56 草爽秀
          偶想玩玩  回復(fù)  更多評(píng)論
            

          # re: httpclient登錄開心網(wǎng) 2009-12-09 12:39 j88888888
          不知帶好玩嗎  回復(fù)  更多評(píng)論
            

          # re: httpclient登錄開心網(wǎng) 2009-12-30 09:04 這段代碼有錯(cuò)誤
          這段代碼有錯(cuò)誤  回復(fù)  更多評(píng)論
            

          # re: httpclient登錄開心網(wǎng)[未登錄] 2012-02-09 17:29 李莫愁
          代碼不能實(shí)現(xiàn)登錄。  回復(fù)  更多評(píng)論
            


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 临潭县| 本溪| 龙陵县| 大悟县| 博兴县| 四平市| 宁南县| 安图县| 正阳县| 巴青县| 称多县| 栾城县| 桃江县| 富宁县| 万宁市| 舟曲县| 衡东县| 古田县| 望城县| 本溪市| 介休市| 庆城县| 申扎县| 平武县| 万载县| 洞头县| 凯里市| 建德市| 玉田县| 陆良县| 塔河县| 大新县| 宽甸| 鸡泽县| 蒙城县| 宁远县| 广水市| 渭源县| 名山县| 建水县| 宜昌市|