When you log on to the Forum, Let's first look at what the browser has done:
Use Firefox to open the HiPda login page, enter the user name and password, and click Login.
The data obtained through the firebug plug-in is as follows:
Can you see the browser http://www.hi-pda.com/forum/logging.php? Action = login & loginsubmit = yes & inajax = 1 the web site sends a POST request. <喎?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 = "\">
We can see that there are a total of 7 parameters:
The first cookietime = 259200, which is fixed and can be passed directly;
The second formhash is a setting of the discuz Forum. The value is in the source code of the current page.
For example, let's take a look at the source code of the web page and find out if formhash is the same as formhash here:
Exactly the same.
The third value of loginfield is fixed, which is equal to username;
The fourth is your input password;
The fifth is the number of the security question. Because we did not select a security question, the number is 0;
The sixth referer can be input directly;
The seventh is your user name.
The following code enables automatic logon.
First, through the above analysis, we first need the value of formhash. We can get the source code of the webpage through HttpGet and parse the formhash.
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.exe cute (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 ){}Now we can log on and use 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){ }
Now that we have successfully logged on, the login status will always be displayed as long as the same HttpClient object is used. For example, let's use httpClient to open version D and try it out:
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){ }
The dashboard that has been logged on is displayed.