`
kop
  • 浏览: 20922 次
  • 性别: Icon_minigender_1
  • 来自: 春秋战国
最近访客 更多访客>>
社区版块
存档分类
最新评论

JAVA利用httpclient登录开心网

阅读更多
httpclient简单应用,登录开心网的例子


需要用到的java包
commons-httpclient-3.1.jar
commons-logging.jar
log4j-1.2.15.jar
commons-codec.jar


很简单,没什么好说的,直接上代码:
package httpclienttest;

import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

public class LoginKaixin {
	private static final String LOGON_SITE = "http://www.kaixin001.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);
		
		//登录页面
		PostMethod post = new PostMethod("http://www.kaixin001.com/login/login.php");
		NameValuePair ie = new NameValuePair("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");   
		NameValuePair url = new NameValuePair("url", "/home/");
		NameValuePair username = new NameValuePair("email","xxx@163.com");
		NameValuePair password = new NameValuePair("password", "xxxxxx");
	post.setRequestBody(new NameValuePair[] { ie,url,username, password});
		client.executeMethod(post);
		System.out.println("******************************登录******************************");
		Cookie[] cookies = client.getState().getCookies();
		client.getState().addCookies(cookies);
		post.releaseConnection();
		System.out.println("******************************页面转向******************************");
		String newUrl="http://www.kaixin001.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("******************************组件功能******************************");
		//"http://www.kaixin001.com/!slave/index.php", "朋友买卖"
		//"http://www.kaixin001.com/!parking/index.php", "争车位"
		//"http://www.kaixin001.com/!house/index.php?_lgmode=pri", "买房子" 
		//http://www.kaixin001.com/!house/index.php?_lgmode=pri&t=49
		//"http://www.kaixin001.com/!house/garden/index.php","花园"
		//(1)进入朋友买卖****************
		System.out.println("******************************(1)进入朋友买卖******************************");
		String slave="http://www.kaixin001.com/!slave/index.php";
		get = new GetMethod(slave);
		get.setRequestHeader("Cookie", cookies.toString());
		client.executeMethod(get);
		responseString = get.getResponseBodyAsString();
		System.out.println(responseString);
		get.releaseConnection();
		//(2)进入争车位****************
		System.out.println("******************************(2)进入争车位******************************");
		String parking="http://www.kaixin001.com/!parking/index.php";
		get = new GetMethod(parking);
		get.setRequestHeader("Cookie", cookies.toString());
		client.executeMethod(get);
		responseString = get.getResponseBodyAsString();
		System.out.println(responseString);
		get.releaseConnection();
		//(3)进入买房子****************
		System.out.println("******************************(3)进入买房子*******************************");
		String house="http://www.kaixin001.com/!house/index.php?_lgmode=pri&t=49";
		get = new GetMethod(house);
		get.setRequestHeader("Cookie", cookies.toString());
		client.executeMethod(get);
		responseString = get.getResponseBodyAsString();
		System.out.println(responseString);
		get.releaseConnection();
		//(4)进入花园****************
		System.out.println("******************************(4)进入花园*******************************");
		String garden="http://www.kaixin001.com/!house/garden/index.php";
		get = new GetMethod(garden);
		get.setRequestHeader("Cookie", cookies.toString());
		client.executeMethod(get);
		responseString = get.getResponseBodyAsString();
		System.out.println(responseString);
		get.releaseConnection();
		
		
	}

}

分享到:
评论
7 楼 chenzubin 2010-04-17  
kaixin001.com是什么概念?
6 楼 highill 2010-03-30  
提取开心网的登录form,
在 html里面做一个隐藏的form,用JS就可以直接提交了
安全性方面,好像不太好 呵呵
5 楼 perfect 2010-03-30  
所有的操作 都是 基于 返回的html
成功返回的html source 肯定有 失败 没有的 tag
非常要命的方法,也是唯一的方法
4 楼 javacode23 2010-03-24  
刚刚接触这个httpclient,代码看的不是很懂,我想问一下你是怎么验证的账号密码的正确性的啊 ?  例如我有一个开心网账号是zhengque@163.com 密码是:zhengque;还有一个错误的账号:cuowu@163.com密码:cuowu,我应该如何实现判断账号的正确与否呢 ? 如果用zhengque@163.com账号登陆成功了 就输“成功登陆!”反之则输出“登录失败!”  这个该如何实现啊  望大侠能不吝赐教啊!
3 楼 sysmaid 2009-12-30  
想问一下楼主代码是否经过验证?(是否原创就不过问了~)
cookie.toString()这样明显是不对的
我用楼主的代码运行,也许是kaixin001的登陆改动过了,运行结果是time out
找了很多用httpClient登陆的例子,都大同小异,也许有什么细节没发现吧
反正我还没登陆成功过
2 楼 younggun 2009-07-19  
这些玩意用curl不就搞定了嘛
1 楼 ferreousbox 2009-07-17  
你的代码:
get.setRequestHeader("Cookie", cookies.toString()); 

唉,不说了.

而且这个步骤也没有必要,因为HttpClient会自动管理COOKIE

相关推荐

Global site tag (gtag.js) - Google Analytics