List of usage examples for org.apache.http.client CookieStore getCookies
List<Cookie> getCookies();
From source file:com.vanda.beivandalibnetwork.utils.HttpUtils.java
/** * ?Cookie?/*from w ww .j a v a2 s . c om*/ * * @param context * * @param name * Cookie?? * @return */ public static Cookie getCookie(final HttpContext context, String name) { HttpContext ctx = context == null ? CURRENT_CONTEXT : context; if (ctx == null) return null; CookieStore store = (CookieStore) ctx.getAttribute(ClientContext.COOKIE_STORE); if (store == null) return null; for (Cookie cookie : store.getCookies()) { if (cookie.getName().equals(name)) return cookie; } return null; }
From source file:com.hp.mercury.ci.jenkins.plugins.oo.core.OOAccessibilityLayer.java
private static CookieStore handleCsrfCookies(CookieStore cookieStore) { List<Cookie> cookies = new ArrayList<Cookie>(); for (Cookie cookie : cookieStore.getCookies()) { if (!cookie.getName().contains("CSRF")) { cookies.add(cookie);// ww w.j av a2s . c o m } } cookieStore.clear(); for (Cookie cookie : cookies) { cookieStore.addCookie(cookie); } return cookieStore; }
From source file:com.domuslink.communication.ApiHandler.java
/** * Pull the raw text content of the given URL. This call blocks until the * operation has completed, and is synchronized because it uses a shared * buffer {@link #sBuffer}.//from w w w. j a v a 2 s . c o m * * @param type The type of either a GET or POST for the request * @param commandURI The constructed URI for the path * @return The raw content returned by the server. * @throws ApiException If any connection or server error occurs. */ protected static synchronized String urlContent(int type, URI commandURI, ApiCookieHandler cookieHandler) throws ApiException { HttpResponse response; HttpRequestBase request; if (sUserAgent == null) { throw new ApiException("User-Agent string must be prepared"); } // Create client and set our specific user-agent string DefaultHttpClient client = new DefaultHttpClient(); UsernamePasswordCredentials creds = new UsernamePasswordCredentials("", sPassword); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), creds); client.setCredentialsProvider(credsProvider); CookieStore cookieStore = cookieHandler.getCookieStore(); if (cookieStore != null) { boolean expiredCookies = false; Date nowTime = new Date(); for (Cookie theCookie : cookieStore.getCookies()) { if (theCookie.isExpired(nowTime)) expiredCookies = true; } if (!expiredCookies) client.setCookieStore(cookieStore); else { cookieHandler.setCookieStore(null); cookieStore = null; } } try { if (type == POST_TYPE) request = new HttpPost(commandURI); else request = new HttpGet(commandURI); request.setHeader("User-Agent", sUserAgent); response = client.execute(request); // Check if server response is valid StatusLine status = response.getStatusLine(); if (status.getStatusCode() != HTTP_STATUS_OK) { Log.e(TAG, "urlContent: Url issue: " + commandURI.toString() + " with status: " + status.toString()); throw new ApiException("Invalid response from server: " + status.toString()); } // Pull content stream from response HttpEntity entity = response.getEntity(); InputStream inputStream = entity.getContent(); ByteArrayOutputStream content = new ByteArrayOutputStream(); // Read response into a buffered stream int readBytes = 0; while ((readBytes = inputStream.read(sBuffer)) != -1) { content.write(sBuffer, 0, readBytes); } if (cookieStore == null) { List<Cookie> realCookies = client.getCookieStore().getCookies(); if (!realCookies.isEmpty()) { BasicCookieStore newCookies = new BasicCookieStore(); for (int i = 0; i < realCookies.size(); i++) { newCookies.addCookie(realCookies.get(i)); // Log.d(TAG, "aCookie - " + realCookies.get(i).toString()); } cookieHandler.setCookieStore(newCookies); } } // Return result from buffered stream return content.toString(); } catch (IOException e) { Log.e(TAG, "urlContent: client execute: " + commandURI.toString()); throw new ApiException("Problem communicating with API", e); } catch (IllegalArgumentException e) { Log.e(TAG, "urlContent: client execute: " + commandURI.toString()); throw new ApiException("Problem communicating with API", e); } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources client.getConnectionManager().shutdown(); } }
From source file:org.apache.hive.jdbc.Utils.java
/** * The function iterates through the list of cookies in the cookiestore and tries to * match them with the cookieName. If there is a match, the cookieStore already * has a valid cookie and the client need not send Credentials for validation purpose. * @param cookieStore The cookie Store//from www.java 2 s. c o m * @param cookieName Name of the cookie which needs to be validated * @param isSSL Whether this is a http/https connection * @return true or false based on whether the client needs to send the credentials or * not to the server. */ static boolean needToSendCredentials(CookieStore cookieStore, String cookieName, boolean isSSL) { if (cookieName == null || cookieStore == null) { return true; } List<Cookie> cookies = cookieStore.getCookies(); for (Cookie 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.isSecure() && !isSSL) { continue; } if (c.getName().equals(cookieName)) { return false; } } return true; }
From source file:com.ibm.team.build.internal.hjplugin.util.HttpUtils.java
@SuppressWarnings("unused") private static void printCookies(HttpClientContext httpContext) { if (LOGGER.isLoggable(Level.FINER)) { LOGGER.finer("Cookies:"); //$NON-NLS-1$ CookieStore cookieStore = (CookieStore) httpContext.getAttribute(HttpClientContext.COOKIE_STORE); List<Cookie> cookies = cookieStore.getCookies(); if (cookies.isEmpty()) { System.out.println("\tNone"); //$NON-NLS-1$ } else {/* w w w. ja va 2s. com*/ for (int i = 0; i < cookies.size(); i++) { LOGGER.finer("\t- " + cookies.get(i).toString()); //$NON-NLS-1$ } } } }
From source file:functional.CookieTest.java
/** * Call a handler that will return no cookie * * @throws Exception/*from www. j ava 2 s. c o m*/ */ @Test public void testNoCookies() throws Exception { HttpGet httpget = new HttpGet("http://localhost:8080/noCookie"); ResponseHandler<String> responseHandler = new BasicResponseHandler(); httpclient.execute(httpget, responseHandler); CookieStore cookies = httpclient.getCookieStore(); assertEquals(0, cookies.getCookies().size()); }
From source file:functional.CookieTest.java
/** * Now call one that should return a Cookie * * @throws Exception/*from ww w .j a v a 2 s . c om*/ */ @Test public void testReceiveCookie() throws Exception { HttpGet httpget = new HttpGet("http://localhost:8080/sendCookie"); ResponseHandler<String> responseHandler = new BasicResponseHandler(); httpclient.execute(httpget, responseHandler); CookieStore cookies = httpclient.getCookieStore(); assertEquals(1, cookies.getCookies().size()); assertEquals("test", cookies.getCookies().get(0).getName()); assertEquals("test", cookies.getCookies().get(0).getValue()); }
From source file:org.tellervo.desktop.wsi.util.WSCookieStore.java
/** * Populate this cookie store from an Apache cookie store * @param cs/*from w w w. j ava 2 s. com*/ */ public void fromCookieStore(CookieStore cs) { boolean storeChanged = false; for (Cookie c : cs.getCookies()) { String hv = hashCookie(c); WSCookieWrapper cw = cookies.get(hv); // cookie already exists and is unchanged if (cw != null && cw.equals(c)) continue; // (re-)wrap a cookie cookies.put(hv, new WSCookieWrapper(c)); storeChanged = true; log.debug("New Cookie: " + hv); } if (storeChanged) { WSCookieStoreHandler.save(this); } }
From source file:com.ibm.sbt.services.client.CookieStoreClientService.java
@Override protected Response execRequest(HttpRequestBase httpRequestBase, Args args, Object content) throws ClientServicesException { Response response = super.execRequest(httpRequestBase, args, content); CookieStore cookieStore = httpClient.getCookieStore(); List<Cookie> cookies = cookieStore.getCookies(); //System.out.println(hashCode() +" < "+cookies.size()); for (Cookie cookie : cookies) { //System.out.println(cookie.getName()+"="+cookie.getValue()); CookieStoreClientService.cookieStore.addCookie(cookie); }//w ww. j a v a2 s . c o m return response; }
From source file:ilarkesto.integration.max.internet.RequestExecutor.java
private String getCookieValue(String name) { CookieStore cookieStore = httpClient.getCookieStore(); for (Cookie cookie : cookieStore.getCookies()) { if (name.equals(cookie.getName())) return cookie.getValue(); }// w w w .j a va 2s . c om return null; }