首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用htmlUnit登录学校网站

使用htmlUnit登录学校网站
EN

Stack Overflow用户
提问于 2016-03-31 10:19:22
回答 1查看 77关注 0票数 0

我正在尝试为我的学校网站(reggienet.illinoisstate.edu)申请,但我在输入登录信息并将其存储在我登录的cookies中时遇到了问题。我已经在网上找遍了,也试过所有我能找到的东西,但是什么都没有用,我完全迷路了。

这是我现在拥有的代码

代码语言:javascript
复制
public void login()
{
    HtmlPage currentPage = null;
    Scanner keyboard = new Scanner(System.in);
    WebClient webClient = new WebClient();
    try
    {
        webClient = reggieLogin(webClient, keyboard);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    try
    {
        currentPage = webClient.getPage(""https://reggienet.illinoisstate.edu/portal"");
    }
    catch (FailingHttpStatusCodeException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    catch (MalformedURLException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    catch (IOException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    String pageSource = currentPage.asXml();

    System.out.print(pageSource);
}


private WebClient reggieLogin(WebClient webClient, Scanner keyboard) throws Exception
{
    webClient.getOptions().setJavaScriptEnabled(false);
    HtmlPage currentPage = webClient.getPage("https://account.illinoisstate.edu/centrallogin/"); //Load page at the STRING address.
    HtmlInput username = currentPage.getElementByName("username"); //Find element called loginuser for username
    System.out.print("ULID:  ");
    username.setValueAttribute(keyboard.nextLine()); //Set value for username
    HtmlInput password = currentPage.getElementByName("password"); //Find element called loginpassword for password
    System.out.print("Password:  ");
    password.setValueAttribute(keyboard.nextLine()); //Set value for password
    HtmlButtonInput submitBtn = currentPage.getElementByName("submit"); //Find element called Submit to submit form.
    currentPage = submitBtn.click(); //Click on the button.

    return webClient;
}

有人能提供任何帮助吗,或者有人能看到这段代码的问题所在吗?

EN

回答 1

Stack Overflow用户

发布于 2016-03-31 11:01:57

考虑使用Selenium库与网站进行交互。它具有编辑浏览器cookie的方法。Here is a link to its java documentation.

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36323023

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档