From: Dr. B. L. <b.l...@fz...> - 2015-02-17 12:18:10
|
Hi, I will login to a https page which needs cookies enabled. I set it in the CookieManager, but I get always the awnser that cookies aren't enabled :( I'm using htmlunit 2.15. My code: private final WebClient webClient; public LoginPage() { this.webClient = new WebClient(BrowserVersion.FIREFOX_24); webClient.getOptions().setJavaScriptEnabled(true); webClient.getCookieManager().setCookiesEnabled(true); } public void loginAs(String username, String password, String domain) throws FailingHttpStatusCodeException, MalformedURLException, IOException { System.out.println(webClient.getCookieManager().isCookiesEnabled()); // Get the login page HtmlPage loginPage = (HtmlPage) webClient .getPage("https://users.euro-fusion.org/cfjec-authenticate"); String htmlBody = loginPage.getWebResponse().getContentAsString(); System.out.println(htmlBody); // Get the form that we are dealing with and within that form, // find the submit button and the field that we want to change. HtmlForm form = loginPage.getForms().get(0); // Enter login and passwd form.getInputByName("user").setValueAttribute(username); form.getInputByName("password").setValueAttribute(password); HtmlSelect select = (HtmlSelect) loginPage.getElementByName("domain"); HtmlOption option = select.getOptionByValue(domain); select.setSelectedAttribute(option, true); // Click "Sign In" button HtmlPage page1 = (HtmlPage) form.getInputByValue("Log In").click(); // Get page as Html htmlBody = page1.getWebResponse().getContentAsString(); System.out.println(htmlBody); } public static void main(String args[]) { LoginPage pb = new LoginPage(); // make sure cookies is turn on CookieHandler.setDefault(new CookieManager()); try { pb.loginAs("xxx", "yyy", "EXTERNAL"); } catch (FailingHttpStatusCodeException | IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } ------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------ Forschungszentrum Juelich GmbH 52425 Juelich Sitz der Gesellschaft: Juelich Eingetragen im Handelsregister des Amtsgerichts Dueren Nr. HR B 3498 Vorsitzender des Aufsichtsrats: MinDir Dr. Karl Eugen Huthmacher Geschaeftsfuehrung: Prof. Dr.-Ing. Wolfgang Marquardt (Vorsitzender), Karsten Beneke (stellv. Vorsitzender), Prof. Dr.-Ing. Harald Bolt, Prof. Dr. Sebastian M. Schmidt ------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------ |