From: Ahmed A. <asa...@ya...> - 2016-03-18 18:41:14
|
Hi Marvin, You can enable the logger of "org.apache.http.headers" in log4j.properties [1] so see what HtmlUnit is sending. Otherwise you need to provide more details, including the URL and credentials, for others to look reproduce the issue. Ahmed [1] https://sourceforge.net/p/htmlunit/code/HEAD/tree/trunk/htmlunit/src/test/resources/log4j.properties From: Marvin Desrosiers <mar...@gm...> To: htm...@li... Sent: Friday, March 18, 2016 6:43 PM Subject: [Htmlunit-user] Sending Cookies in HTTP header I'm trying to understand why the cookie are not being sent back to the server. htmlunit is set with setUseInsecureSSL by default to false for the secure tag. try { WebClient webClient = newWebClient(BrowserVersion.CHROME); /*webClient.getOptions().setUseInsecureSSL(false);*/ /*webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);*/ webClient.getOptions().setCssEnabled(false); webClient.getOptions().setJavaScriptEnabled(false); CookieManager cookieManager= webClient.getCookieManager(); out.println(cookieManager.getCookies().toString()); out.println("start"); final HtmlPage loginPage = webClient.getPage(AUTHORIZE_URL); String requestToken = loginPage.getElementByName("request_token").getAttribute("value"); out.println("request token :" + requestToken); out.println("after loginPage"); out.println(cookieManager.getCookies().toString()); Set<Cookie> boxCookies = newHashSet<Cookie>(); boxCookies.addAll(webClient.getCookieManager().getCookies()); StringBuilder cookieHeader = newStringBuilder(); /*Iterator<Cookie> ite = boxCookies.iterator(); while (ite.hasNext()){ Cookie cookie = ite.next(); cookie.getDomain().substring(1); String name = cookie.getName(); String value = cookie.getValue(); System.out.println("Cookie:" + name + "=" +value); if ()){ cookieHeader.append(name + "=" +value); } cookieHeader.append("; "+ name + "=" +value); } */ webClient.addRequestHeader("Accept-Encoding", "gzip, deflate, sdch"); webClient.addRequestHeader("Accept-Language", "en-US,en;q=0.8"); webClient.addRequestHeader("Cache-Control","no-cache"); webClient.addRequestHeader("Upgrade-Insecure-Requests", "1"); webClient.addRequestHeader("DNT", "1"); webClient.addRequestHeader("Host","app.company.com"); for (int index = 0; index < boxCookies.size(); index++){ String cookie = boxCookies.toArray()[index].toString(); String cookieNameValue=cookie.substring(0, cookie.indexOf(";")); String name = cookieNameValue.substring(0,cookieNameValue.indexOf("=")); String value = cookieNameValue.substring(cookieNameValue.indexOf("=") + 1); if (index == 0){ cookieHeader.append(name + "=" +value); } else { cookieHeader.append("; "+ name + "="+value); } } WebRequest secondLoginPage = newWebRequest(AUTHORIZE_URL); secondLoginPage.setAdditionalHeader("Cookie", cookieHeader.toString()); HtmlPage loginPage2 = webClient.getPage(secondLoginPage); out.println(loginPage2.getWebResponse().getWebRequest().getRequestParameters().toString()); out.println("\n"); Map<?, ?> additionalRequest1 = loginPage2.getWebResponse().getWebRequest().getAdditionalHeaders(); Iterator<?> ite0 = additionalRequest1.entrySet().iterator(); while(ite0.hasNext()){ out.println(ite0.next()); } out.println("\n"); final HtmlTextInput login = (HtmlTextInput) loginPage2.getElementById("login"); login.setValueAttribute(USER_EMAIL); final HtmlPasswordInput password= (HtmlPasswordInput) loginPage2.getElementById("password"); password.setValueAttribute(USER_PASS); final HtmlSubmitInput button_submit = loginPage2.getElementByName("login_submit"); final HtmlPage accessGrantingPage = button_submit.click(); final HtmlForm requestForm = (HtmlForm)accessGrantingPage.getElementById("consent_form"); Map<?, ?> additionalRequest = accessGrantingPage.getWebResponse().getWebRequest().getAdditionalHeaders(); out.println("\n"); Iterator<?> ite2 = additionalRequest.entrySet().iterator(); while(ite2.hasNext()){ out.println(ite2.next()); } out.println("\n"); out.println("after accessGrantingPage"); out.println(cookieManager.getCookies().toString()); final HtmlButton consent_accept_button = accessGrantingPage.getElementByName("consent_accept"); try { final HtmlPage authorizationPage = consent_accept_button.click(); out.println("after authorizationPage"); out.println(authorizationPage.getUrl().toString()); out.println(authorizationPage.getWebResponse().getStatusMessage()); out.println(authorizationPage.getWebResponse().getResponseHeaders()); } catch (RuntimeException re){ re.printStackTrace(); } webClient.closeAllWindows(); } catch (IOException ioe){ ioe.printStackTrace(); } finally { } |