Maintain credentials on all pages in HTMLunit WebClient

My question is very similar to the one on this page , except that I do not have access to the remote server, and I do not know how it performs its authentication.

I am trying to maintain status in all web pages that I can request using webclient.getPage (). The website I am accessing uses the standard login form with a username and password. What I did before is to create a small function to do this for me:

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

The problem is that I need to manually search for the page returned by this function to find the link to the page I need. What is more worrying is that this only works for the page immediately after logging in, but not for other pages.

Instead, I would like to save the information for entering the webclient browser simulator so that I can freely access any secure page on the site. In addition to trying to solve in the previous question (see above), I tried to execute the following solution without success:

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

Edited: here is the main function showing how I use webClient:

public static void main (String [] args) throws Exception {

  // 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(); } 

Additional Information: I do not have information about the authentication mechanism of the remote site server, since I do not have access to it, but your help will be excellent. Thanks!

+4
source share

All Articles