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 阅读(3691) 评论(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 李莫愁
代码不能实现登录。  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: