简体   繁体   English

使用htmlUnit登录到学校网站

[英]Logging in to school website using htmlUnit

I'm trying to make an application for my school website (reggienet.illinoisstate.edu), but I'm having trouble getting entering the login and having it stored in the cookies that I am logged in. I've scoured the web and tried everything I could find, but nothing is working I'm totally lost. 我正在尝试为我的学校网站(reggienet.illinoisstate.edu)申请,但我无法输入登录名并将其存储在我登录的Cookie中。我已经在网上搜寻了尝试了我能找到的所有东西,但没有任何工作,我完全迷失了。

Here is the code that I have right now 这是我现在拥有的代码

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;
}

Can anybody offer any help, or can anybody see what's wrong with this code? 有人可以提供任何帮助吗,或者有人可以看到此代码有什么问题吗?

Consider using the Selenium library to interact with websites. 考虑使用Selenium库与网站进行交互。 It has methods for editing browser cookies. 它具有编辑浏览器cookie的方法。 Here is a link to its java documentation. 这是其Java文档的链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM