久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

JAVA利用httpclient登錄開(kāi)心網(wǎng)

 BlazerOfIT 2011-10-26
JAVA利用httpclient登錄開(kāi)心網(wǎng)
2011-07-19 11:55

httpclient簡(jiǎn)單應(yīng)用,,登錄開(kāi)心網(wǎng)的例子


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


很簡(jiǎn)單,,沒(méi)什么好說(shuō)的,,直接上代碼:

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.         //登錄頁(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","[email protected]");  
  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.   
  90. }

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,,所有內(nèi)容均由用戶發(fā)布,,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式,、誘導(dǎo)購(gòu)買等信息,,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,,請(qǐng)點(diǎn)擊一鍵舉報(bào),。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多