JAVA利用httpclient登录开心网

httpclient简单应用,登录开心网的例子


需要用到的java包
commons-httpclient-3.1.jar
commons-logging.jar
log4j-1.2.15.jar
commons-codec.jar


很简单,没什么好说的,直接上代码:

Java代码
  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.   
  90. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值