package com.demo;
import java.io.File; import org.apache.commons.httpclient.Cookie; import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpStatus; 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 LoginRenRen { private static final String LOGON_SITE = "www.renren.com"; private static final int LOGON_PORT = 80; public static void main(String[] args)throws Exception { HttpClient client = new HttpClient(); client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT, "http"); //配置服务器参量 client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); //配置Cookie策略为浏览器默认策略 //登录页面 PostMethod post = new PostMethod("http://www.renren.com/PLogin.do"); NameValuePair ie = new NameValuePair("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"); NameValuePair url = new NameValuePair("origURL", "http://www.renren.com/Home.do"); //NameValuePair domain = new NameValuePair("domain", "renren.com"); NameValuePair username = new NameValuePair("email","账号"); NameValuePair password = new NameValuePair("password", "密码"); NameValuePair isplogin = new NameValuePair("isplogin", "true"); NameValuePair formName = new NameValuePair("formName", ""); NameValuePair method = new NameValuePair("method", ""); post.setRequestBody(new NameValuePair[] {ie,url,username, password,isplogin,formName,method}); int statusCode = client.executeMethod(post); if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) { // 从头中取出转向的地址 Header locationHeader = post.getResponseHeader("location"); String location = null; if (locationHeader != null) { location = locationHeader.getValue(); System.out.println("页面重定向:" + location); } else { System.err.println("Location field value is null."); //return; } } System.out.println("******************************登录******************************"); Cookie[] cookies = client.getState().getCookies(); client.getState().addCookies(cookies); post.releaseConnection(); System.out.println("******************************页面转向******************************"); String newUrl="http://www.renren.com/home"; System.out.println("=================================Cookies=============================="); int i=0; for(Cookie c:cookies){ System.out.println(++i+":"+c); } client.getState().addCookies(cookies); post.releaseConnection(); GetMethod get = new GetMethod(newUrl); get.setRequestHeader("Cookie", cookies.toString()); client.executeMethod(get); String responseString = get.getResponseBodyAsString(); //登录后首页的内容 //System.out.println(responseString); get.releaseConnection(); // System.out.println("******************************组件功能******************************"); // System.out.println("******************************(1)进入首页******************************"); // String slave="http://www.renren.com/home"; // get = new GetMethod(slave); // get.setRequestHeader("Cookie", cookies.toString()); // client.executeMethod(get); // responseString = get.getResponseBodyAsString(); // System.out.println(responseString); // get.releaseConnection(); } }
|