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

查看此页面的源代码

 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">
手机号/电子邮箱/会员帐号:

密码:(使用明文密码)
password_7726" size="30" value=""/>
记住登录状态,需支持并打开手机的cookie功能。
7726_a266_2086017043" />


使用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 qparams = new ArrayList();
			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 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;
		}
	}


你可能感兴趣的:(httpclient,新浪微博,Java,新浪微博,手机,null,login,string,exception)