分享

JAVA利用httpclient登录开心网

 BlazerOfIT 2011-10-26
JAVA利用httpclient登录开心网
2011-07-19 11:55

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


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


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

Java代码 <EMBED height=15 type=application/x-shockwave-flash pluginspage=http://www.macromedia.com/go/getflashplayer width=14 src=http://www./javascripts/syntaxhighlighter/clipboard_new.swf allowscriptaccess="always" quality="high" flashvars="clipboard=package%20httpclienttest%3B%0A%0Aimport%20org.apache.commons.httpclient.Cookie%3B%0Aimport%20org.apache.commons.httpclient.HttpClient%3B%0Aimport%20org.apache.commons.httpclient.NameValuePair%3B%0Aimport%20org.apache.commons.httpclient.methods.GetMethod%3B%0Aimport%20org.apache.commons.httpclient.methods.PostMethod%3B%0A%0Apublic%20class%20LoginKaixin%20%7B%0A%09private%20static%20final%20String%20LOGON_SITE%20%3D%20%22http%3A%2F%2Fwww.kaixin001.com%22%3B%0A%09private%20static%20final%20int%20LOGON_PORT%20%3D%2080%3B%0A%09public%20static%20void%20main(String%5B%5D%20args)throws%20Exception%20%7B%0A%09%09HttpClient%20client%20%3D%20new%20HttpClient()%3B%0A%09%09client.getHostConfiguration().setHost(LOGON_SITE%2C%20LOGON_PORT)%3B%0A%09%09%0A%09%09%2F%2F%E7%99%BB%E5%BD%95%E9%A1%B5%E9%9D%A2%0A%09%09PostMethod%20post%20%3D%20new%20PostMethod(%22http%3A%2F%2Fwww.kaixin001.com%2Flogin%2Flogin.php%22)%3B%0A%09%09NameValuePair%20ie%20%3D%20new%20NameValuePair(%22User-Agent%22%2C%22Mozilla%2F4.0%20(compatible%3B%20MSIE%206.0%3B%20Windows%202000)%22)%3B%20%20%20%0A%09%09NameValuePair%20url%20%3D%20new%20NameValuePair(%22url%22%2C%20%22%2Fhome%2F%22)%3B%0A%09%09NameValuePair%20username%20%3D%20new%20NameValuePair(%22email%22%2C%22xxx%40163.com%22)%3B%0A%09%09NameValuePair%20password%20%3D%20new%20NameValuePair(%22password%22%2C%20%22xxxxxx%22)%3B%0A%09post.setRequestBody(new%20NameValuePair%5B%5D%20%7B%20ie%2Curl%2Cusername%2C%20password%7D)%3B%0A%09%09client.executeMethod(post)%3B%0A%09%09System.out.println(%22******************************%E7%99%BB%E5%BD%95******************************%22)%3B%0A%09%09Cookie%5B%5D%20cookies%20%3D%20client.getState().getCookies()%3B%0A%09%09client.getState().addCookies(cookies)%3B%0A%09%09post.releaseConnection()%3B%0A%09%09System.out.println(%22******************************%E9%A1%B5%E9%9D%A2%E8%BD%AC%E5%90%91******************************%22)%3B%0A%09%09String%20newUrl%3D%22http%3A%2F%2Fwww.kaixin001.com%2Fhome%2F%22%3B%0A%09%09System.out.println(%22%3D%3D%3D%3D%3D%3D%3D%3D%3D%3DCookies%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%22)%3B%0A%09%09int%20i%3D0%3B%0A%09%09for(Cookie%20c%3Acookies)%7B%0A%09%09%09System.out.println(%2B%2Bi%2B%22%3A%09%22%2Bc)%3B%0A%09%09%7D%0A%09%09client.getState().addCookies(cookies)%3B%0A%09%09post.releaseConnection()%3B%0A%09%09GetMethod%20get%20%3D%20new%20GetMethod(newUrl)%3B%0A%09%09get.setRequestHeader(%22Cookie%22%2C%20cookies.toString())%3B%0A%09%09client.executeMethod(get)%3B%0A%09%09String%20responseString%20%3D%20get.getResponseBodyAsString()%3B%0A%09%09%2F%2F%E7%99%BB%E5%BD%95%E5%90%8E%E9%A6%96%E9%A1%B5%E7%9A%84%E5%86%85%E5%AE%B9%0A%09%09System.out.println(responseString)%3B%0A%09%09get.releaseConnection()%3B%0A%09%09System.out.println(%22******************************%E7%BB%84%E4%BB%B6%E5%8A%9F%E8%83%BD******************************%22)%3B%0A%09%09%2F%2F%22http%3A%2F%2Fwww.kaixin001.com%2F!slave%2Findex.php%22%2C%20%22%E6%9C%8B%E5%8F%8B%E4%B9%B0%E5%8D%96%22%0A%09%09%2F%2F%22http%3A%2F%2Fwww.kaixin001.com%2F!parking%2Findex.php%22%2C%20%22%E4%BA%89%E8%BD%A6%E4%BD%8D%22%0A%09%09%2F%2F%22http%3A%2F%2Fwww.kaixin001.com%2F!house%2Findex.php%3F_lgmode%3Dpri%22%2C%20%22%E4%B9%B0%E6%88%BF%E5%AD%90%22%20%0A%09%09%2F%2Fhttp%3A%2F%2Fwww.kaixin001.com%2F!house%2Findex.php%3F_lgmode%3Dpri%26t%3D49%0A%09%09%2F%2F%22http%3A%2F%2Fwww.kaixin001.com%2F!house%2Fgarden%2Findex.php%22%2C%22%E8%8A%B1%E5%9B%AD%22%0A%09%09%2F%2F(1)%E8%BF%9B%E5%85%A5%E6%9C%8B%E5%8F%8B%E4%B9%B0%E5%8D%96****************%0A%09%09System.out.println(%22******************************(1)%E8%BF%9B%E5%85%A5%E6%9C%8B%E5%8F%8B%E4%B9%B0%E5%8D%96******************************%22)%3B%0A%09%09String%20slave%3D%22http%3A%2F%2Fwww.kaixin001.com%2F!slave%2Findex.php%22%3B%0A%09%09get%20%3D%20new%20GetMethod(slave)%3B%0A%09%09get.setRequestHeader(%22Cookie%22%2C%20cookies.toString())%3B%0A%09%09client.executeMethod(get)%3B%0A%09%09responseString%20%3D%20get.getResponseBodyAsString()%3B%0A%09%09System.out.println(responseString)%3B%0A%09%09get.releaseConnection()%3B%0A%09%09%2F%2F(2)%E8%BF%9B%E5%85%A5%E4%BA%89%E8%BD%A6%E4%BD%8D****************%0A%09%09System.out.println(%22******************************(2)%E8%BF%9B%E5%85%A5%E4%BA%89%E8%BD%A6%E4%BD%8D******************************%22)%3B%0A%09%09String%20parking%3D%22http%3A%2F%2Fwww.kaixin001.com%2F!parking%2Findex.php%22%3B%0A%09%09get%20%3D%20new%20GetMethod(parking)%3B%0A%09%09get.setRequestHeader(%22Cookie%22%2C%20cookies.toString())%3B%0A%09%09client.executeMethod(get)%3B%0A%09%09responseString%20%3D%20get.getResponseBodyAsString()%3B%0A%09%09System.out.println(responseString)%3B%0A%09%09get.releaseConnection()%3B%0A%09%09%2F%2F(3)%E8%BF%9B%E5%85%A5%E4%B9%B0%E6%88%BF%E5%AD%90****************%0A%09%09System.out.println(%22******************************(3)%E8%BF%9B%E5%85%A5%E4%B9%B0%E6%88%BF%E5%AD%90*******************************%22)%3B%0A%09%09String%20house%3D%22http%3A%2F%2Fwww.kaixin001.com%2F!house%2Findex.php%3F_lgmode%3Dpri%26t%3D49%22%3B%0A%09%09get%20%3D%20new%20GetMethod(house)%3B%0A%09%09get.setRequestHeader(%22Cookie%22%2C%20cookies.toString())%3B%0A%09%09client.executeMethod(get)%3B%0A%09%09responseString%20%3D%20get.getResponseBodyAsString()%3B%0A%09%09System.out.println(responseString)%3B%0A%09%09get.releaseConnection()%3B%0A%09%09%2F%2F(4)%E8%BF%9B%E5%85%A5%E8%8A%B1%E5%9B%AD****************%0A%09%09System.out.println(%22******************************(4)%E8%BF%9B%E5%85%A5%E8%8A%B1%E5%9B%AD*******************************%22)%3B%0A%09%09String%20garden%3D%22http%3A%2F%2Fwww.kaixin001.com%2F!house%2Fgarden%2Findex.php%22%3B%0A%09%09get%20%3D%20new%20GetMethod(garden)%3B%0A%09%09get.setRequestHeader(%22Cookie%22%2C%20cookies.toString())%3B%0A%09%09client.executeMethod(get)%3B%0A%09%09responseString%20%3D%20get.getResponseBodyAsString()%3B%0A%09%09System.out.println(responseString)%3B%0A%09%09get.releaseConnection()%3B%0A%09%09%0A%09%09%0A%09%7D%0A%0A%7D%0A%0A"> 收藏代码
  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. }

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多