Use HttpClient to simulate login to the blog site, httpclient blog
Recently, I am working on the blog garden client for Android. I am considering adding the login function to get more information.
Now the test uses HttpClient for simulated login.
Login Page: http://passport.cnblogs.com/login.aspx
Login results page: http://home.cnblogs.com/, http://www.cnblogs.com /,......
The user name and password are required for Logon. The attributes are tbUserName and tbPassword ;:
The logon is successful. When you go to the next page, you need to provide Cookies. You can also set attributes such as UA:
Code:
1 package com.arlen.login; 2 3 import org.apache.commons.httpclient.Cookie; 4 import org.apache.commons.httpclient.HttpClient; 5 import org.apache.commons.httpclient.NameValuePair; 6 import org.apache.commons.httpclient.cookie.CookiePolicy; 7 import org.apache.commons.httpclient.methods.GetMethod; 8 import org.apache.commons.httpclient.methods.PostMethod; 9 import org.apache.commons.httpclient.params.HttpMethodParams;10 11 public class ImitateLogin {12 13 public static void main(String[] args) {14 String userName = "username";15 String password = "password"16 String loginUrl = "http://passport.cnblogs.com/login.aspx";17 String dataUrl = "http://home.cnblogs.com/";18 HttpClientLogin(userName, password, loginUrl, dataUrl);19 }20 21 private static void HttpClientLogin(String userName, String password,22 String loginUrl, String dataUrl) {23 HttpClient httpClient = new HttpClient();24 httpClient.getParams().setParameter(25 HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");26 PostMethod postMethod = new PostMethod(loginUrl);27 28 NameValuePair[] postData = { new NameValuePair("tbUserName", userName),29 new NameValuePair("tbPassword", password) };30 postMethod.setRequestBody(postData);31 32 try {33 34 httpClient.getParams().setCookiePolicy(35 CookiePolicy.BROWSER_COMPATIBILITY);36 httpClient.executeMethod(postMethod);37 Cookie[] cookies = httpClient.getState().getCookies();38 StringBuffer stringBuffer = new StringBuffer();39 for (Cookie c : cookies) {40 stringBuffer.append(c.toString() + ";");41 }42 43 GetMethod getMethod = new GetMethod(dataUrl);44 getMethod.setRequestHeader("Cookie", stringBuffer.toString());45 postMethod.setRequestHeader("Host", "passport.cnblogs.com");46 postMethod.setRequestHeader("Referer", "http://home.cnblogs.com/");47 postMethod.setRequestHeader("User-Agent", "AndroidCnblogs");48 httpClient.executeMethod(getMethod);49 50 String result = getMethod.getResponseBodyAsString();51 System.out.println(result);52 53 } catch (Exception e) {54 e.printStackTrace();55 }56 }57 58 }
Simulate logon using httpclient in java
You need to first determine whether the login is successful. As long as the client object is successfully logged on, you should get the cookie. You can use this client object to directly request the page you need. If the login is successful without a status code, the status code may be useless.
Use java httpclient to simulate logon to get the 163 Email Contact
Use the packet capture tool to capture packets and use httpclient to simulate the whole process. Note that some fields may need to be parsed from html.