4

我的问题与此页面上的问题非常相似,只是我无权访问远程服务器,也不知道它是如何进行身份验证的。

我正在尝试跨网页保持登录状态,我可以使用 webclient.getPage() 请求这些网页。我正在访问的网站使用带有用户名、密码对的标准登录表单。我之前所做的是创建一个小函数来为我做到这一点:

public static HtmlPage logIn(HtmlPage page) {
    HtmlPage nextpage = null;
    final HtmlForm form = page.getFormByName("login_form");
    final HtmlSubmitInput button = form.getInputByValue("Login");
    final HtmlTextInput username = form.getInputByName("username");
    final HtmlPasswordInput password = form.getInputByName("password");
    username.setValueAttribute("user_foo");
    password.setValueAttribute("pwd_bar");      

    // hit submit button and return the requested page
    try {
        nextpage = button.click();
    } catch (IOException e) {
        e.printStackTrace();
    }
        return nextpage;
}

这样做的问题是我必须手动搜索此函数返回的页面才能找到我想要的页面的链接。更麻烦的是,这仅适用于登录后的页面,而不适用于其他页面。

相反,我想在浏览器模拟器“webclient”中保存登录信息,以便我可以无缝访问站点内的任何受保护页面。除了尝试上一个问题(上面链接)中的解决方案之外,我还尝试了以下解决方案但没有成功:

private static void setCredentials(WebClient webClient) {
    String username = "user_foo";
    String password = "pwd_bar";
    DefaultCredentialsProvider creds = (DefaultCredentialsProvider) webClient.getCredentialsProvider();//new DefaultCredentialsProvider();
    try {
        creds.addCredentials(username, password);
    webClient.setCredentialsProvider(creds);
    }
    catch (Exception e){
        System.out.println("!!! Problem login in");
        e.printStackTrace();            
}   

编辑:这是显示我如何使用 webClient 的主要功能:

公共静态 void main(String[] args) 抛出异常 {

    // Create and initialize WebClient object
    WebClient webClient = new WebClient(/*BrowserVersion.CHROME_16*/);
    webClient.setThrowExceptionOnScriptError(false);
    webClient.setJavaScriptEnabled(false);
    webClient.setCssEnabled(false);
    webClient.getCookieManager().setCookiesEnabled(true);
    setCredentials(webClient);

    HtmlPage subj_page = null;
    //visit login page and get it
    String url = "http://www.website.com/index.php";
    HtmlPage page = (HtmlPage) webClient.getPage(url);
    HtmlAnchor anchor = null;
    page = logIn(page);

    // search for content
    page = searchPage(page, "recent articles");     

    // click on the paper link      
    anchor = (HtmlAnchor) page.getAnchorByText("recent articles");
    page = (HtmlPage) anchor.click();

    // loop through found articles
    //{{{page
    int curr_pg = 1;
    int last_pg = 5;        
    page = webClient.getPage(<starting URL of the first article>); // such URLs look like: "www.website.com/view_articles.php?publication_id=17&page=1"
    do {
        // find sections on this page; 
        List <HtmlDivision> sections = new ArrayList<HtmlDivision>();
        List <HtmlDivision> artdivs = new ArrayList<HtmlDivision>();
        List <HtmlDivision> tagdivs = new ArrayList<HtmlDivision>();
        sections = (List<HtmlDivision>) page.getByXPath("//div[@class='article_section']");
        artdivs = (List<HtmlDivision>) page.getByXPath("//div[@class='article_head']");
        tagdivs = (List<HtmlDivision>) page.getByXPath("//div[@class='article_tag']");          

        int num_ques = sections.size();
        HtmlDivision section, artdiv, tagdiv;

        // for every section, get its sub-articles
        for (int i = 0; i < num_ques; i++) {
            section = sections.get(i);
            artdiv = artdivs.get(i);
            tagdiv = tagdivs.get(i);

            // find the sub-article details and print to xml file
            String xml = getXMLArticle(artdiv, section.asText(), tagdiv);
            System.out.println(xml);
            System.out.println("-----------------------------");
        }
        //remove IllegalMonitorStateException *
        synchronized (webClient) { 
            webClient.wait(2000); // wait for 2 seconds             
        }

        String href = "?publication_id=17&page=" + curr_pg;
        anchor = page.getAnchorByHref(href);            
        page = anchor.click();
        System.out.println("anchor val: " + anchor.getHrefAttribute());
        curr_pg++;
    } while (curr_pg < last_pg);
    //}}}page

    webClient.closeAllWindows();
}

其他信息:我没有关于远程站点服务器身份验证机制的信息,因为我无权访问它,但你的帮助会很棒。谢谢!

4

0 回答 0