Refine search
@Test // SPR-14066 public void cookieManagerShared() throws Exception { this.mockMvc = MockMvcBuilders.standaloneSetup(new CookieController()).build(); WebClient client = MockMvcWebClientBuilder.mockMvcSetup(this.mockMvc).build(); assertThat(getResponse(client, "http://localhost/").getContentAsString(), equalTo("NA")); client.getCookieManager().addCookie(new Cookie("localhost", "cookie", "cookieManagerShared")); assertThat(getResponse(client, "http://localhost/").getContentAsString(), equalTo("cookieManagerShared")); }
private void storeCookies(WebRequest webRequest, javax.servlet.http.Cookie[] cookies) { Date now = new Date(); CookieManager cookieManager = this.webClient.getCookieManager(); for (javax.servlet.http.Cookie cookie : cookies) { if (cookie.getDomain() == null) { cookie.setDomain(webRequest.getUrl().getHost()); } Cookie toManage = createCookie(cookie); Date expires = toManage.getExpires(); if (expires == null || expires.after(now)) { cookieManager.addCookie(toManage); } else { cookieManager.removeCookie(toManage); } } }
private void assertSingleSessionCookie(String expected) { com.gargoylesoftware.htmlunit.util.Cookie jsessionidCookie = webClient.getCookieManager().getCookie("JSESSIONID"); if (expected == null || expected.contains("Expires=Thu, 01-Jan-1970 00:00:01 GMT")) { assertThat(jsessionidCookie, nullValue()); return; } String actual = jsessionidCookie.getValue(); assertThat("JSESSIONID=" + actual + "; Path=/test; Domain=example.com", equalTo(expected)); }
WebClient webClient = new WebClient(); try { WebRequest webRequest = new WebRequest(new URL(pageRequest.getUrl())); webClient.getOptions().setUseInsecureSSL(true); webClient.getOptions().setJavaScriptEnabled(true); webClient.getOptions().setCssEnabled(false); webClient.getOptions().setThrowExceptionOnScriptError(false); webClient.getCookieManager().setCookiesEnabled(true); for (Map.Entry<String, String> cookieItem : pageRequest.getCookieMap().entrySet()) { webClient.getCookieManager().addCookie(new Cookie("", cookieItem.getKey(), cookieItem.getValue()));
@Override public String call() throws Exception { WebClient client = new WebClient(); client.setThrowExceptionOnFailingStatusCode(false); client.getCookieManager().addCookie(new Cookie(contextPath.getHost(), JSESSIONID, jsessionid)); Page page = client.getPage(contextPath + "introspect?mode=" + mode + "&cid=" + cid); if (!(page instanceof TextPage)) { return "" + page.getWebResponse().getStatusCode(); } TextPage textPage = (TextPage) page; return textPage.getContent(); }
WebClient client = new WebClient(); client.setThrowExceptionOnFailingStatusCode(true); TextPage initPage = client.getPage(contextPath + "introspect?mode=" + IntrospectServlet.MODE_INIT); String cid = extractCid(initPage.getContent()); assertNotNull(cid); assertFalse(cid.isEmpty()); String jsessionid = client.getCookieManager().getCookie(JSESSIONID).getValue(); assertNotNull(jsessionid); assertFalse(jsessionid.isEmpty());
wc.getOptions().setJavaScriptEnabled(jsEnabled); wc.getOptions().setThrowExceptionOnFailingStatusCode(!ignoreFailingStatusCodes); wc.getOptions().setThrowExceptionOnScriptError(throwExceptionOnScriptError); wc.getOptions().setRedirectEnabled(true); wc.getOptions().setUseInsecureSSL(true); wc.getCookieManager().addCookie( new Cookie(c.getDomain() != null ? c.getDomain() : "", c .getName(), c.getValue(), c.getPath() != null ? c
public static void main(String[] args) { String baseUrl = "https://news.ycombinator.com" ; String loginUrl = baseUrl + "/login?goto=news" ; String login = "login"; String password = "password" ; try { System.out.println("Starting autoLogin on " + loginUrl); WebClient client = autoLogin(loginUrl, login, password); HtmlPage page = client.getPage(baseUrl) ; HtmlAnchor logoutLink = page.getFirstByXPath(String.format("//a[@href='user?id=%s']", login)) ; if(logoutLink != null ){ System.out.println("Successfuly logged in !"); // printing the cookies for(Cookie cookie : client.getCookieManager().getCookies()){ System.out.println(cookie.toString()); } }else{ System.err.println("Wrong credentials"); } } catch (Exception e) { e.printStackTrace(); } }
final BrowserVersion browserVersion = getBrowserVersion(); final CookieManager cookieManager = getCookieManager(); if (cookieManager.isCookiesEnabled()) { final CharArrayBuffer buffer = new CharArrayBuffer(cookieString.length() + 22); buffer.append("Set-Cookie: "); cookieSpec.parse(new BufferedHeader(buffer), cookieManager.buildCookieOrigin(pageUrl)); cookieManager.addCookie(htmlUnitCookie); getIncorrectnessListener().notify("set-cookie http-equiv meta tag: invalid cookie '" + cookieString + "'; reason: '" + e.getMessage() + "'.", origin);
public void print(HtmlPage page) { System.out.println("\n\n\n RAW REQUEST BODY \n" + getRawRequestBody()); System.out.println("\n\n\n RAW RESPONSE \n" + getRawResponse()); System.out.println("Response Headers: " + page.getWebResponse().getResponseHeaders()); System.out.println("\n\n\n ********************************* \n" + page.asXml()); System.out.println("\n\n\n **************************** \n" + webClient.getCookieManager().getCookies()); System.out.println("\n\n\n *********************************"); }
/** * Adds a cookie, as long as cookies are enabled. * @see <a href="http://msdn.microsoft.com/en-us/library/ms533693.aspx">MSDN documentation</a> * @param newCookie in the format "name=value[;expires=date][;domain=domainname][;path=path][;secure] */ public void jsxSet_cookie(final String newCookie) { final CookieManager cookieManager = getHtmlPage().getWebClient().getCookieManager(); if (cookieManager.isCookiesEnabled()) { URL url = getHtmlPage().getWebResponse().getRequestSettings().getUrl(); url = replaceForCookieIfNecessary(url); final Cookie cookie = buildCookie(newCookie, url); cookieManager.addCookie(cookie); LOG.debug("Added cookie: " + cookie); } else { LOG.debug("Skipped adding cookie: " + newCookie); } }
final CookieManager cookieManager = getCookieManager(); if (!cookieManager.isCookiesEnabled()) { return Collections.<Cookie>emptySet(); final URL normalizedUrl = cookieManager.replaceForCookieIfNecessary(url); final boolean secure = "https".equals(protocol); final int port = cookieManager.getPort(normalizedUrl); cookieManager.clearExpired(new Date()); final List<org.apache.http.cookie.Cookie> all = Cookie.toHttpClient(cookieManager.getCookies()); final List<org.apache.http.cookie.Cookie> matches = new ArrayList<>(); final CookieSpec cookieSpec = new HtmlUnitBrowserCompatCookieSpec(getBrowserVersion()); for (final org.apache.http.cookie.Cookie cookie : all) { if (cookieSpec.match(cookie, cookieOrigin)) {
@After public void tearDown() { webClient.getCookieManager().clearCookies(); webClient.close(); }
queryString = webClient_.encodeQuery(queryString, true); httpMethod.setQueryString(queryString); queryString = webClient_.encodeQuery(queryString, true); method.setQueryString(queryString); if (webRequestSettings.getRequestBody() != null) { httpMethod.setRequestHeader("User-Agent", webClient_.getBrowserVersion().getUserAgent()); if (webClient_.getCookieManager().isCookiesEnabled()) {
/** * Returns the property "cookieEnabled". * @return the property "cookieEnabled" */ public boolean jsxGet_cookieEnabled() { return getWindow().getWebWindow().getWebClient().getCookieManager().isCookiesEnabled(); }
public WebResponse getResponse( WebRequest request ) throws IOException { Cookie jsCookie = webClient.getCookieManager().getCookie("JSESSIONID"); if (jsCookie != null && (!jsCookie.getValue().startsWith("\"") && !jsCookie.getValue().endsWith("\""))) { Cookie newCookie = new Cookie(jsCookie.getDomain(), jsCookie.getName(), "\"" + jsCookie.getValue() + "\"", jsCookie.getPath(), jsCookie.getExpires(), jsCookie.isSecure()); webClient.getCookieManager().removeCookie(jsCookie); webClient.getCookieManager().addCookie(newCookie); } return super.getResponse(request); } };
@Override public void deleteAllCookies() { CookieManager cookieManager = getWebClient().getCookieManager(); URL url = getRawUrl(); Set<com.gargoylesoftware.htmlunit.util.Cookie> rawCookies = getWebClient().getCookies(url); for (com.gargoylesoftware.htmlunit.util.Cookie cookie : rawCookies) { cookieManager.removeCookie(cookie); } }
private void addSessionCookie(MockHttpServletRequest request, String sessionid) { getCookieManager().addCookie(createCookie(request, sessionid)); }
public void deleteCookieNamed(String name) { CookieManager cookieManager = webClient.getCookieManager(); Set<org.apache.commons.httpclient.Cookie> rawCookies = webClient.getCookieManager().getCookies(getHostName()); for (org.apache.commons.httpclient.Cookie cookie : rawCookies) { if (name.equals(cookie.getName())) { cookieManager.removeCookie(cookie); } } }
LOG.info("Scraper Cookies: " + inspectCookies(cm.getCookies())); LOG.info("Browser Cookies: " + inspectCookies(cookies)); String name = c.getName(); String value = c.getValue(); Cookie existing = cm.getCookie(name); if (existing != null) { found++; LOG.info("Different Value as Existing: " + existing + " value=" + value); Cookie nc = new Cookie(existing.getDomain(), name, value); cm.addCookie(nc); cm.addCookie(c);