使用httpClient实现模拟登录人人网

 

最近在研究httpClient包,也只是初步了解,发一段模拟登录人人网的程序

 

 

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

public class Test2 {
	public static void main(String[] args) throws Exception {

		HttpClient httpClient = new HttpClient();
		
		//这里的url地址,实现使用Get方法登入www.renren.com之后从中取出form表带中的action 
		String url = "http://www.renren.com/PLogin.do";
		
		PostMethod post = new PostMethod(url);

		post.addParameter("email", "you never know");
		post.addParameter("password", "you never know");
		

		int statuscode = httpClient.executeMethod(post);
		post.releaseConnection();
		
		if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY)
				|| (statuscode == HttpStatus.SC_MOVED_PERMANENTLY)
				|| (statuscode == HttpStatus.SC_SEE_OTHER)
				|| (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
			
			// 读取新的URL地址
			Header header = post.getResponseHeader("location");
			System.out.println(header);
			if (header != null) {
				//新的URL地址,需要在此发送请求,这里就用上了GetMethod方法
				String newuri = header.getValue();
				System.out.println("newuri:"+ newuri);
				if ((newuri == null) || (newuri.equals("")))
					newuri = "/";
				
				GetMethod redirect = new GetMethod(newuri);
				
				httpClient.executeMethod(redirect);
				
				System.out.println(redirect.getResponseBodyAsString());
				redirect.releaseConnection();
			} else
				System.out.println("Invalid redirect");
		}
	}
}
 

运行程序后,控制台上会打印出登录成功后的网页代码,通过这个步骤,以后还可以通过程序进一步实现其他一些功能,比如写日志,发状态之类的。  写一个while循环,每一分钟发一条状态吧。。。。。。(封号)

 

这里POST上去的参数 只有一个用户名和密码,可是用同样的方式去登入QQ空间,发现无法模拟,估计是POST的参数缺少一些吧,安装了firebug跟踪了一下,也跟踪不出来,求指导~

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值