當前位置:成語大全網 - 書法字典 - 如何用Java實現模擬登錄Discuz!論壇返回的Html代碼?

如何用Java實現模擬登錄Discuz!論壇返回的Html代碼?

包org.shaw

導入org . Apache . commons . mons . mons . mons . mons . ";

靜態最終int LOGON _ PORT = 80

公共Cookie[]登錄(字符串名稱,字符串密碼){

嘗試{

http client client = new http client();

client.getHostConfiguration()。setHost(LOGON_SITE,LOGON _ PORT);

post method post = new post method("/logging . PHP?動作=登錄”);

client . execute method(post);

String response String = new String(post . getresponse body(),“gbk”);

post . release connection();

string form hash = getFormHash(response string);

system . out . println(form hash);

post = new post method("/logging . PHP?action =登錄& amp);

name value pair[]params = new name value pair[11];

params[0]= new name value pair(" log in field "," username ");

params[1]= new name value pair("用戶名",名稱);

params[2] =新的名稱值對(" password ",pas);

params[3]= new name value pair(" referer "," index . PHP ");

params[4]= new name value pair(" questionid "," 0 ");

params[5]= new name value pair(" answer "," ");

params[6]= new name value pair(" cookietime "," 2592000 ");

params[7]= new name value pair(" form hash ",form hash);

params[8]= new name value pair(" log in mode "," ");

params[9]= new name value pair(" log in submit "," true ");

params[10]= new name value pair(" styleid "," ");

post . setrequestbody(params);

client . execute method(post);

//response String = new String(post . getresponse body(),“gbk”);

post . release connection();

get method get = new get method("/index . PHP ");

client . execute method(get);

response String = new String(get . getresponse body(),“gbk”);

get . release connection();

system . out . println(response string);

} catch(異常e) {

e . printstacktrace();

}

返回null

}

公共字符串getFormHash(字符串htmlContent) {

嘗試{

int start = html content . index of(" name = \ " form hash \ " ");

start = html content . index of(" value = \ ",start)+7;

int end = htmlContent.indexOf("\ ",start);

string form hash = html content . substring(start,end);

返回formhash

} catch (RuntimeException e) {

扔e;

}

}

公共靜態void main(String[] args) {

新BaiyouBBS()。登錄(" zzzxxxzzz "," zzz xxxzzz ");

}

}