原理很简单,就是向登录页面发起一个http的request,然后获得cookies,再向想要访问的博客园页面发起request请求,在请求中携带cookie。
使用了commons-codec-1.4.jar , commons-httpclient-3.1.jar ,commons-logging-1.0.3.jar 三个外部包。下面是全部代码
import org.apache.commons.httpclient.Cookie; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.cookie.CookiePolicy; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; public class HttpClientLogin { public static void main(String[] args){ //登陆 Url String loginUrl = "http://passport.cnblogs.com/login.aspx"; //需登陆后访问的 Url String dataUrl = "http://home.cnblogs.com/"; HttpClient httpClient = new HttpClient(); //模拟登陆,按实际服务器端要求选用 Post 或 Get 请求方式 PostMethod postMethod = new PostMethod(loginUrl); //设置登陆时要求的信息,一般就用户名和密码,验证码自己处理了 NameValuePair[] data = { new NameValuePair("__VIEWSTATE", "/wEPDwULLTE1MzYzODg2NzZkGAEFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYBBQtjaGtSZW1lbWJlcm1QYDyKKI9af4b67Mzq2xFaL9Bt"), new NameValuePair("__EVENTVALIDATION", "/wEdAAUyDI6H/s9f+ZALqNAA4PyUhI6Xi65hwcQ8/QoQCF8JIahXufbhIqPmwKf992GTkd0wq1PKp6+/1yNGng6H71Uxop4oRunf14dz2Zt2+QKDEIYpifFQj3yQiLk3eeHVQqcjiaAP"), new NameValuePair("tbUserName", "yourusername"), new NameValuePair("tbPassword", "yourpassword"), new NameValuePair("txtReturnUrl", "http://home.cnblogs.com/"), new NameValuePair("btnLogin", "登 录") }; postMethod.setRequestBody(data); try { //设置 HttpClient 接收 Cookie,用与浏览器一样的策略 httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); httpClient.executeMethod(postMethod); //获得登陆后的 Cookie Cookie[] cookies=httpClient.getState().getCookies(); System.out.println(cookies); System.out.println("============================================================================"); String tmpcookies= ""; for(Cookie c:cookies){ tmpcookies += c.toString()+";"; } System.out.println(tmpcookies); //进行登陆后的操作 GetMethod getMethod = new GetMethod(dataUrl); //每次访问需授权的网址时需带上前面的 cookie 作为通行证 getMethod.setRequestHeader("cookie",tmpcookies); //标明来自哪里的链接,以及UA postMethod.setRequestHeader("Referer", "http://unmi.cc"); postMethod.setRequestHeader("User-Agent","Unmi Spot"); httpClient.executeMethod(getMethod); //打印出返回数据,检验一下是否成功 String text = getMethod.getResponseBodyAsString(); System.out.println(text); } catch (Exception e) { e.printStackTrace(); } } }