When you go to the forum, let's look at what the browser did:
Use Firefox to open HIPDA landing page, enter the user name and password, click Login.
Here is the data obtained through the Firebug plugin:
You can see a POST request from the browser for this http://www.hi-pda.com/forum/logging.php?action=login&loginsubmit=yes&inajax=1 URL
Take a look at what its post parameters are:
You can see a total of 7 parameters:
The first cookietime=259200, this is fixed, the direct transfer of the value of the past on the line;
The second formhash is a setting for the Discuz forum, and the value is in the source code of the current page.
For example, we look at the source of the Web page, search Formhash with the formhash here is not the same:
It's just the same.
The third value Loginfield is fixed, equal to username;
The fourth one is your input method password;
The fifth one is the security question number, because we did not choose the question of safety question, so the number is 0;
The sixth Referer, the direct input this is OK;
The seventh one is your user name.
Below we use code to implement automatic login.
First of all through the above analysis, the first need Formhash value, this we can get the source of the Web page through HttpGet, the Formhash parse out.
HttpClient HttpClient = new Defaulthttpclient (); Get the Formhash value of the webpage, parse it with 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) { }
Below we can log in, with HttpPost:
HttpPost httppost=new httppost ("HTTP://WWW.HI-PDA.COM/FORUM/LOGGING.PHP?ACTION=LOGIN&LOGINSUBMIT=YES&A Mp;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) {} Now that we have landed successfully, as long as the same HttpClient object, the login status will always be displayed. For example, let's use this httpclient to open the D version and try it:
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) { }
You can see the contents of the "D" version that is displayed.