/* init client */ HttpClient http = null; CookieStore httpCookieStore = new BasicCookieStore(); HttpClientBuilder builder = HttpClientBuilder.create().setDefaultCookieStore(httpCookieStore); http = builder.build(); /* do stuff */ HttpGet httpRequest = new HttpGet("http://stackoverflow.com/"); HttpResponse httpResponse = null; try {httpResponse = http.execute(httpRequest);} catch (Throwable error) {throw new RuntimeException(error);} /* check cookies */ httpCookieStore.getCookies();
static final String COOKIES_HEADER = "Set-Cookie"; HttpURLConnection connection = ... ; static java.net.CookieManager msCookieManager = new java.net.CookieManager(); Map<String, List<String>> headerFields = connection.getHeaderFields(); List<String> cookiesHeader = headerFields.get(COOKIES_HEADER); if (cookiesHeader != null) { for (String cookie : cookiesHeader) { msCookieManager.getCookieStore().add(null,HttpCookie.parse(cookie).get(0)); } }
CookieHandler.getDefault()).getCookieStore(); List<HttpCookie> cookies = rawCookieStore.get(baseUri); String url = baseUri.toString(); for (HttpCookie cookie : cookies) { String setCookie = new StringBuilder(cookie.toString()) .append("; domain=").append(cookie.getDomain()) .append("; path=").append(cookie.getPath()) .toString(); webCookieManager.setCookie(url, setCookie);
HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost("http://192.168.178.163:8080/login/"); CookieStore cookieStore = new BasicCookieStore(); HttpContext context = new BasicHttpContext(); context.setAttribute(ClientContext.COOKIE_STORE, cookieStore); ... HttpResponse response = client.execute(post, context); List<Cookie> cookies = cookieStore.getCookies(); CookieMonster.eat(cookies); // :)
HttpClient httpclient = new DefaultHttpClient(); List <Cookie> cookies = cookieStore.getCookies(); for (Cookie cookie: cookies) { if (cookie.getName().equals("XSRF-TOKEN")) { HttpResponse response = httpclient.execute(httppost);
HttpClient httpClient = new DefaultHttpClient(); // execute get/post/put or whatever httpClient.doGetPostPutOrWhatever(); // get cookieStore CookieStore cookieStore = httpClient.getCookieStore(); // get Cookies List<Cookie> cookies = cookieStore.getCookies(); // process...
mStore = new CookieManager().getCookieStore(); String jsonSessionCookie = Prefs.getJsonSessionCookie(mContext); if (!jsonSessionCookie.equals(Prefs.DEFAULT_STRING)) { HttpCookie cookie = mGson.fromJson(jsonSessionCookie, HttpCookie.class); mStore.add(URI.create(cookie.getDomain()), cookie); if (cookie.getName().equals("sessionid")) { remove(URI.create(cookie.getDomain()), cookie); Prefs.saveJsonSessionCookie(mContext, mGson.toJson(cookie)); mStore.add(URI.create(cookie.getDomain()), cookie); return mStore.get(uri); return mStore.getCookies(); return mStore.getURIs(); return mStore.remove(uri, cookie); return mStore.removeAll();
HttpCookie cookie = new HttpCookie("PHPSESSID", getPhpSessionID()); cookie.setPath("/"); cookie.setVersion(0); cookie.setDomain("www.example.com"); ((CookieManager)client.getCookieHandler()).getCookieStore().add(new URI("http://www.example.com"), cookie);
public String cookie(String name) { List<HttpCookie> cookies = cookieManager.getCookieStore().getCookies(); return cookies.stream().filter(cookie -> cookie.getName().equals(name)).findFirst().map(cookie -> cookie.getValue()).orElse(null); } }
CookieManager cookieManager = new CookieManager(); CookieHandler.setDefault(cookieManager); HttpCookie cookie = new HttpCookie("lang", "en"); cookie.setDomain("Your URL"); cookie.setPath("/"); cookie.setVersion(0); cookieManager.getCookieStore().add(new URI("https://Your URL/"), cookie); List<HttpCookie> Cookies = cookieManager.getCookieStore().get(new URI("https://Your URL/")); String s = Cookies.get(0).getValue();
List<HttpCookie> cookies = parseCookie(responseHeaders); for (HttpCookie cookie : cookies) { if (cookie.getDomain() == null) { cookie.setDomain(uri.getHost()); if (cookie.getPath() == null) { cookie.setPath(pathToCookiePath(uri.getPath())); } else if (!HttpCookie.pathMatches(cookie, uri)) { continue; store.add(uri, cookie);
private boolean isSessionCookieExpired(CookieManager manager, long creationTime) { for (URI uri: manager.getCookieStore().getURIs()) { for (HttpCookie cookie: manager.getCookieStore().get(uri)) { if (cookie.getName().equals(JSESSIONID)) { if ((creationTime / 1000 + cookie.getMaxAge()) < System.currentTimeMillis()/1000) { return true; } else return false; } } } return true; }
public static HttpCookie getAuthCookie(CookieStore cookieStore, URI uri) { if (cookieStore == null) { return null; } boolean isSSL = "https".equals(uri.getScheme()); List<HttpCookie> cookies = cookieStore.getCookies(); for (HttpCookie c : cookies) { // If this is a secured cookie and the current connection is non-secured, // then, skip this cookie. We need to skip this cookie because, the cookie // replay will not be transmitted to the server. if (c.getSecure() && !isSSL) { continue; } if (c.getName().equals(AuthenticatedURL.AUTH_COOKIE)) { return c; } } return null; }
// Create a local instance of cookie store public static CookieStore cookieStore = new BasicCookieStore(); // Create local HTTP context static HttpContext localContext = new BasicHttpContext(); // Bind custom cookie store to the local context localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); // Pass local context as a parameter HttpResponse httpResponse = client.execute(request, localContext); List<Cookie> cookies = cookieStore.getCookies(); for (int i = 0; i < cookies.size(); i++) { MLog.d("LOCAL COOKIES: ", ""+ cookies.get(i)); } HttpEntity entity = httpResponse.getEntity();
CookieManager cookies = new CookieManager(); cookies.getCookieStore().add(null, new HttpCookie("app.flickgo.com", "/")); cookies.getCookieStore().add(null, new HttpCookie("has_js", "1")); cookies.getCookieStore().add(null, new HttpCookie(session_name, session_id)); URL url = new URL("http://app.flickgo.com/apistuff/node.json"); HttpURLConnection urlconnection = (HttpURLConnection) url.openConnection(); urlconnection.setRequestMethod("POST"); urlconnection.setDoOutput(true); urlconnection.setRequestProperty("Content-Type", "application/json"); try(OutputStreamWriter writer = new OutputStreamWriter(urlconnection.getOutputStream())) { writer.write("{\"title\":\""+title+"\",\"type\":\"article\",\"body\":{\"und\":[{ \"value\":\""+body+"\"}]}}"); } int rc = urlconnection.getResponseCode();
/** * Retrieves a cookie from the cookie store with a given name * * @param name The name of the cookie * @return A Cookie or null if non found by name */ public HttpCookie getCookie(String name) { return this.cookieManager.getCookieStore() .getCookies() .stream() .filter(cookie -> cookie.getName().equals(name)) .findFirst() .orElse(null); }
CookieManager cookiemanager = LoginUtils.loginFormBasedJ2EE("hostname", "username", "password"); SyncProxy.setBaseURL("baseURI"); StandardDispatchService rpcService = SyncProxy.createProxy(StandardDispatchService.class, new ProxySettings().setCookieManager(cookiemanager)); System.out.println(cookiemanager.getCookieStore().getCookies().get(0)); String JSESSIONID = cookiemanager.getCookieStore().getCookies().get(0).getValue(); rpcService.execute(new XXXAction(XXX,XX));
/** * 获取Cookie * @return Cookie列表 * @since 3.1.1 */ public List<HttpCookie> getCookies(){ return HttpRequest.cookieManager.getCookieStore().getCookies(); }
/** * Stores cookies from request connection in the thread local cookie manager. */ private void pushCookies() { final Map<String, List<String>> headerFields = requestConnection.getHeaderFields(); final List<String> cookiesHeader = headerFields.get(COOKIES_HEADER); if (cookiesHeader != null) { for (String cookie : cookiesHeader) { localCookieManager.get().getCookieStore().add(null, HttpCookie.parse(cookie).get(0)); } } }
CookieStore cookieJar = Main.cookieManager.getCookieStore(); List<HttpCookie> cookies = cookieJar.getCookies(); for (HttpCookie ck: cookies) { if(ck.getName().equals("JSESSIONID")){ System.out.println("JSESSIONID"); }else{ ck.setMaxAge(0); System.out.println("CookieHandler retrieved cookie: " + ck); } }