use of cn.edu.hfut.dmic.webcollector.net.HttpRequest in project wechat by dllwh.
the class SinaWeiboCN method getSinaCookie.
/**
* ----------------------------------------------- [私有方法]
*/
/**
* @方法描述: 获取新浪微博的cookie,这个方法针对weibo.cn有效,对weibo.com无效
* weibo.cn以明文形式传输数据,请使用小号
* @param username
* 新浪微博用户名
* @param password
* 新浪微博密码
* @return
*/
public static String getSinaCookie(String username, String password) throws Exception {
HtmlUnitDriver driver = new HtmlUnitDriver();
driver.setJavascriptEnabled(true);
driver.get("http://login.weibo.cn/login/");
/**
* 手机号/电子邮箱/会员帐号:
*/
WebElement mobile = driver.findElementByCssSelector("input[name=mobile]");
mobile.sendKeys(new CharSequence[] { username });
/**
* 密码
*/
WebElement pass = driver.findElementByCssSelector("input[name^=password]");
pass.sendKeys(new CharSequence[] { password });
/**
* 图片验证码
*/
WebElement ele = driver.findElementByCssSelector("img");
String src = ele.getAttribute("src");
String cookie = concatCookie(driver);
HttpRequest request = new HttpRequest(src);
request.setCookie(cookie);
String imageCode = "";
WebElement code = driver.findElementByCssSelector("input[name^=code]");
code.sendKeys(new CharSequence[] { imageCode });
/**
* 记住登录状态,需支持并打开手机的cookie功能。
*/
WebElement rem = driver.findElementByCssSelector("input[name=remember]");
rem.click();
/**
* 登录按钮
*/
WebElement submit = driver.findElementByCssSelector("input[name=submit]");
submit.click();
String result = concatCookie(driver);
if (result.contains("gsid_CTandWM")) {
return result;
}
throw new Exception("weibo login failed");
}
Aggregations