代码如下:
import com.gargoylesoftware.htmlunit.*; import com.gargoylesoftware.htmlunit.html.*; public class Sohu { public static void main(String[] args) throws Exception{ WebClient webClient = new WebClient(BrowserVersion.FIREFOX_24); HtmlPage page = webClient.getPage("http://i.auto.sohu.com/user/login/toLogin.at"); HtmlTextInput user = (HtmlTextInput) page.getElementById("pp_login"); user.type("heiheiabcd@sohu.com"); HtmlPasswordInput password = (HtmlPasswordInput) page.getElementById("pw_login"); password.click(); password.type("Xx20111111"); HtmlAnchor submit = page.getFirstByXPath("//form[@id='user-login']/a[1]"); page = (HtmlPage) submit.click(); Thread.sleep(3000); System.out.println(page.asText()); webClient.closeAllWindows(); } }
得到的结果如下:
登录 - 我的搜狐汽车 首页 | 新闻 | 购车 | 用车 | 论坛 heiheiabcd@sohu.com Xx20111111 **登录中... //为什么是登录中,而得不到登录后的界面,有什么办法?** checked 下次自动登录 忘记密码? 使用其它账号登录: 意见反馈 - 搜狗输入法 - 支付中心 - 搜狐招聘 - 广告服务 - 客服中心 - 联系方式 - 保护隐私权 - About SOHU - 公司介绍 Copyright © 2011 Sohu.com Inc. All Rights Reserved. 搜狐公司 版权所有 搜狐不良信息举报电话:010-62728061 举报邮箱:jubao@contact.sohu.com 京ICP备060382号
问题:怎么才能得到登录后的页面而不是登录中的页面?
(page.getWebResponse().getContentAsString()得到页面源码。page.getUrl()得到登录中页面的url)
线程休眠的时间再长点行不行?
登录中这个页面header有set-cookie吧,有的话,记录下来,用Cookie访问首页就好了