Android HttpClient自動登陸discuz論壇!

來源:互聯網
上載者:User

你登陸論壇的時候,我們先看看瀏覽器幹了什麼事兒:

用Firefox開啟HiPda 的登陸頁面,輸入使用者名稱和密碼,點登陸。

下面是通過firebug外掛程式擷取的資料:


可以看到瀏覽器這個http://www.hi-pda.com/forum/logging.php?action=login&loginsubmit=yes&inajax=1網址發了一個POST請求<喎?http://www.bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+v7TSu8/Cy/xQT1NUtcSyzsr9ysfKssO0o7o8L3A+CjxwPjxpbWcgc3JjPQ=="http://www.2cto.com/uploadfile/Collfiles/20140607/2014060709065031.jpg" alt="\">

可以看到一共有7個參數:

第一個cookietime=259200,這個是固定的,直接傳這個值過去就行;

第二個formhash是discuz論壇的一個設定,值在當前頁面的源碼裡。

比如我們看一下網頁的源碼,搜一下formhash跟這裡的formhash是不是一樣的:


剛好是一樣的。

第三個值loginfield是固定的,等於username;

第四個是你IME密碼;

第五個是安全提問的編號,由於我們沒有選安全提問的問題,所以編號為0;

第六個referer,直接輸進去這個就行;

第七個是你的使用者名稱。


下面我們用代碼實現自動登入。

首先通過上面的分析,首先需要formhash的值,這個我們可以通過HttpGet得到網頁的源碼,把formhash解析出來。

                HttpClient httpClient = new DefaultHttpClient();                //得到網頁的formhash值,用Jsoup解析出來                HttpGet httpGet = new HttpGet("http://www.hi-pda.com/forum/logging.php?action=login");                try{                    HttpResponse httpResponse = httpClient.execute(httpGet);                    HttpEntity httpEntity = httpResponse.getEntity();                    String s = EntityUtils.toString(httpEntity,"GBK");                    Element formhash_Element = Jsoup.parse(s).select("input[name=formhash]").first();                    formhash = formhash_Element.attr("value");                    System.out.println(formhash);                }                catch(Exception e ){                }
下面我們就可以登陸了,用HttpPost:

                HttpPost httpPost=new HttpPost("http://www.hi-pda.com/forum/logging.php?action=login&loginsubmit=yes&inajax=1");                List params=new ArrayList();                params.add(new BasicNameValuePair("formhash",formhash));                params.add(new BasicNameValuePair("loginfield","username"));                params.add(new BasicNameValuePair("password","******"));                params.add(new BasicNameValuePair("questionid","0"));                params.add(new BasicNameValuePair("referer","http://www.hi-pda.com/forum/index.php"));                params.add(new BasicNameValuePair("username","******"));                try {                    httpPost.setEntity(new UrlEncodedFormEntity(params, "GBK"));                    HttpResponse response=httpClient.execute(httpPost);                    HttpEntity entity=response.getEntity();                    String ans=EntityUtils.toString(entity);                }catch (Exception e){                }
現在我們已經登陸成功了,只要用同一個HttpClient對象,就會一直顯示登入狀態。比如我們用這個httpClient開啟一下D版試一下:

                HttpGet getHome = new HttpGet("http://www.hi-pda.com/forum/index.php");                try{                    httpClient.execute(getHome);                }catch (Exception e){                }                HttpGet getD=new HttpGet("http://www.hi-pda.com/forum/forumdisplay.php?fid=2");                try {                    HttpResponse responseD = httpClient.execute(getD);                    HttpEntity entityD=responseD.getEntity();                    String str=EntityUtils.toString(entityD,"GBK");                    System.out.println(str);                }catch (Exception e){                }

可以看到顯示的是已登陸的D版的內容。



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.