查看此页面的源代码
<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/>
最后登录代码如下,下面使用到了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;
}
}