HttpClient httpClient = new DefaultHttpClient();
//httpGet访问网页,为了获取网页的formhash
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<NameValuePair> params=new ArrayList<NameValuePair>();
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){
}
//登陆之后获得网页内容
HttpGet getD=new HttpGet("http://www.hi-pda.com/forum/forumdisplay.php?fid=2");
try {
HttpResponse responseD = httpClient.execute(httpGet);
HttpEntity entityD=responseD.getEntity();
String str=EntityUtils.toString(entityD,"GBK");
System.out.println(str);
}catch (Exception e){
}
登陆之后,应该能够获取登陆之后才能访问的网页内容了,但是我把最后一个get得到的相应打印出来
06-06 04:40:14.115 1068-1081/com.example.LoginTest I/System.out﹕ <p>欢迎您回来,********。现在将转入登录前页面。 <script>setTimeout("window.location.href ='index.php';", 1000);</script></p>