`
遛遛遛
  • 浏览: 52475 次
  • 性别: Icon_minigender_1
  • 来自: 宁波
社区版块
存档分类
最新评论

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

    博客分类:
  • web
 
阅读更多

 

最近在研究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跟踪了一下,也跟踪不出来,求指导~

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics