HtmlUnit会话管理 [英] HtmlUnit Session Management

查看:123
本文介绍了HtmlUnit会话管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用HtmlUnit登录到Facebook页面并查看其HTML内容.我正在尝试通过HtmlUnit填写登录凭据,但是单击提交按钮时,我看不到正在进行的会话.

I'm trying to login to Facebook page using HtmlUnit and view its HTML content. I'm trying to fill up the login credentials through HtmlUnit but I don't see the session being carried when the submit button is clicked.

在htmlunit会话管理类上找不到太多内容.我还附加了我当前用来尝试解决此问题的代码.任何帮助表示赞赏!

Couldnt find much content on htmlunit session management classes. I have also attached the code that I'm currently using to attempt this problem. Any help appreciated!

WebClient webClient = new WebClient();
HtmlPage page1 = webClient.getPage("https://www.facebook.com");
List<HtmlForm> listF = page1.getForms();
HtmlForm form = null;
for(int i=0; i<listF.size(); i++)
{
    if(listF.get(i).getId().equals("login_form"))
    {
        form = listF.get(i);
        }
}
HtmlTextInput uName = form.getInputByName("email");
HtmlPasswordInput passWord = form.getInputByName("pass");
HtmlSubmitInput button = form.getInputByValue("Log In");
uName.setValueAttribute(FACEBOOK_UNAME);
passWord.setValueAttribute(FACEBOOK_PASS);
HtmlPage page2 = button.click();

推荐答案

找到了答案.在开始获取网页之前,请先启用Cookie.有用. 添加了以下代码

Found the answer. Just enabled cookies before starting to get webpages. It works. Added the below piece of code

WebClient webClient = new WebClient();
CookieManager cookieMan = new CookieManager();
cookieMan = webClient.getCookieManager();
cookieMan.setCookiesEnabled(true);

这篇关于HtmlUnit会话管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆