HttpClient登录人人网


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();   
}   

}  

 

转自:http://blog.csdn.net/yodlove/archive/2010/10/13/5938022.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值