httpclient登录新浪微博手机版Weibo.cn

1 篇文章 0 订阅
1 篇文章 0 订阅

查看此页面的源代码

<form action="login_submit.php?rand=368437524&backURL=http%3A%2F%2Fweibo.cn%2Fdpool%2Fttt%2Fhome.php%3Fs2w%3Dlogin&backTitle=%D0%C2%C0%CB%CE%A2%B2%A9&vt=4&revalid=2&ns=1" method="post">
<div class="mg">
手机号/电子邮箱/会员帐号:<br/><input type="text" name="mobile" size="30" value=""/>
<br/>
密码:(<a href="login.php?backURL=http%3A%2F%2Fweibo.cn%2Fdpool%2Fttt%2Fhome.php%3Fs2w%3Dlogin&backTitle=%D0%C2%C0%CB%CE%A2%B2%A9&vt=4&revalid=2&ns=1&pt=1">使用明文密码</a>)<br/>

<input type="password" name="password_7726" size="30" value=""/><br/>
<input type="checkbox" name="remember" checked="checked" />记住登录状态,需支持并打开手机的cookie功能。<br/>
<input type="hidden" name="backURL" value="http://weibo.cn/dpool/ttt/home.php?s2w=login" />
<input type="hidden" name="backTitle" value="新浪微博" />
<input type="hidden" name="backURL" value="http://weibo.cn/dpool/ttt/home.php?s2w=login" />
<input type="hidden" name="vk" value="7726_a266_2086017043" />
<input type="submit" name="submit" value="登录" /><br/>


使用httpfox(firefox的一个插件)查看登录时浏览器发送的表单数据


最后登录代码如下,下面使用到了Jsoup提取html的标签信息

	public boolean Login(String username , String password)
	{	
		String getgsid = null;
		
		String content = Get(LoginUrl);
		if(content==null) return false;
		Document doc = Jsoup.parse(content); 
			
		Element form=null , pwnd=null , vk=null;
		String rand=null , spwnd=null , svk=null;
		form  = doc.select("form[method=post]").first();
		pwnd  = doc.select("input[type=password]").first();
		vk = doc.select("input[name=vk]").first();
		if(form==null || pwnd==null || vk==null) return false;
			
		rand = form.attr("action");
		spwnd = pwnd.attr("name");
		svk = vk.attr("value");
		if(rand==null || spwnd==null || svk==null) return false;
		
		try {
			String url = "http://3g.sina.com.cn/prog/wapsite/sso/"+rand;
			HttpPost post = new HttpPost(url);
			post.setHeader("User-Agent","Mozilla/5.0 (X11; Linux i686; rv:5.0) Gecko/20100101 Firefox/5.0");
			post.setHeader("Referer", "http://weibo.com/");
			post.setHeader("Content-Type", "application/x-www-form-urlencoded");
			
			List<NameValuePair> qparams = new ArrayList<NameValuePair>();
			qparams.add(new BasicNameValuePair("mobile", username));
			qparams.add(new BasicNameValuePair(spwnd, password));
			qparams.add(new BasicNameValuePair("remember", "on"));
			qparams.add(new BasicNameValuePair("backURL", "http://weibo.cn/dpool/ttt/home.php"));
			qparams.add(new BasicNameValuePair("backTitle", "新浪微博"));
			qparams.add(new BasicNameValuePair("vk", svk));
			qparams.add(new BasicNameValuePair("submit", "登录"));
			UrlEncodedFormEntity params = new UrlEncodedFormEntity(qparams, "UTF-8");
			post.setEntity(params);		
			httpclient.execute(post);
			post.abort();
			
			List<Cookie> cookies = httpclient.getCookieStore().getCookies();
			if(cookies.size()!=0) getgsid=cookies.get(0).getValue();
			else getgsid=null;
			gsid = getgsid;
			
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch(Exception e){
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			if(gsid==null) return false;
			else return true;
		}
	}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值