編輯:關於Android編程
你登陸論壇的時候,我們先看看浏覽器干了什麼事兒:
用Firefox打開HiPda 的登陸頁面,輸入用戶名和密碼,點登陸。
下面是通過firebug插件獲取的數據:

可以看到浏覽器這個http://www.hi-pda.com/forum/logging.php?action=login&loginsubmit=yes&inajax=1網址發了一個POST請求<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+v7TSu8/Cy/xQT1NUtcSyzsr9ysfKssO0o7o8L3A+CjxwPjxpbWcgc3JjPQ=="/uploadfile/Collfiles/20140607/2014060709065031.jpg" alt="\">
可以看到一共有7個參數:
第一個cookietime=259200,這個是固定的,直接傳這個值過去就行;
第二個formhash是discuz論壇的一個設置,值在當前頁面的源碼裡。
比如我們看一下網頁的源碼,搜一下formhash跟這裡的formhash是不是一樣的:

剛好是一樣的。
第三個值loginfield是固定的,等於username;
第四個是你輸入法密碼;
第五個是安全提問的編號,由於我們沒有選安全提問的問題,所以編號為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版的內容。
android調用系統郵件組件(intent匹配的流程)
直接代碼 package com.example.demoemail; import android.net.Uri; import android.os.Bundl
Android Studio 初探
Android Studio 簡介Android Studio 是Google近年來推薦的Android開發IDE,相對於Eclipse,它針對Android開發做了各種
Android React Native加載圖片資源的正確姿勢
在這篇文章中Android React Native的使用細節問題提到了 圖片使用的問題,也提到了無論用哪種方法都不能加載app內部的圖片資源的問題,當時的代碼是這樣子的
OpenglES2.0 for Android:再談紋理映射
前言上一節我們實現了一個簡單的紋理映射的例子——一個簡單的貼圖,這節我們來做一些稍微復雜一點的例子,最後再給我們前面的立方體做一個紋理。