List of usage examples for java.net CookieManager setCookiePolicy
public void setCookiePolicy(CookiePolicy cookiePolicy)
From source file:Main.java
public static CookieManager cookieManager() { CookieManager cookieManager = new CookieManager(); cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); return cookieManager; }
From source file:Main.java
/** * @see http://blogs.sun.com/CoreJavaTechTips/entry/cookie_handling_in_java_se *///from ww w . j a va 2s . c o m public static void enableCookieMgmt() { CookieManager manager = new CookieManager(); manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); CookieHandler.setDefault(manager); }
From source file:keywhiz.cli.ClientUtils.java
/** * Creates a {@link OkHttpClient} to start a TLS connection. * * @param cookies list of cookies to include in the client. * @return new http client.//from w ww. j a v a 2 s . c o m */ public static OkHttpClient sslOkHttpClient(List<HttpCookie> cookies) { checkNotNull(cookies); SSLContext sslContext; try { sslContext = SSLContext.getInstance("TLSv1.2"); TrustManagerFactory trustManagerFactory = TrustManagerFactory .getInstance(TrustManagerFactory.getDefaultAlgorithm()); trustManagerFactory.init((KeyStore) null); sslContext.init(new KeyManager[0], trustManagerFactory.getTrustManagers(), new SecureRandom()); } catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) { throw Throwables.propagate(e); } SSLSocketFactory socketFactory = sslContext.getSocketFactory(); OkHttpClient client = new OkHttpClient().setSslSocketFactory(socketFactory) .setConnectionSpecs(Arrays.asList(ConnectionSpec.MODERN_TLS)).setFollowSslRedirects(false); client.setRetryOnConnectionFailure(false); client.networkInterceptors().add(new XsrfTokenInterceptor("XSRF-TOKEN", "X-XSRF-TOKEN")); CookieManager cookieManager = new CookieManager(); cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); cookies.forEach(c -> cookieManager.getCookieStore().add(null, c)); client.setCookieHandler(cookieManager); return client; }
From source file:CookieAccessor.java
/** * Get cookies for a url from cookie store *///from www .ja va2 s. c o m public void getCookieUsingCookieHandler() { try { // instantiate CookieManager; make sure to set CookiePolicy CookieManager manager = new CookieManager(); manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); CookieHandler.setDefault(manager); // get content from URLConnection; cookies are set by web site URL url = new URL("http://host.example.com"); URLConnection connection = url.openConnection(); connection.getContent(); // get cookies from underlying CookieStore CookieStore cookieJar = manager.getCookieStore(); List<HttpCookie> cookies = cookieJar.getCookies(); for (HttpCookie cookie : cookies) { System.out.println("CookieHandler retrieved cookie: " + cookie); } } catch (Exception e) { System.out.println("Unable to get cookie using CookieHandler"); e.printStackTrace(); } }
From source file:horriblev3.Cloudflare.java
public List<HttpCookie> scrape() { if (strUrl == null) { System.out.println("URL == NULL"); return null; }// w w w . j a v a 2s. c om try { CookieManager manager = new CookieManager(); manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); CookieHandler.setDefault(manager); URL url = new URL(strUrl); HttpURLConnection con; con = (HttpURLConnection) url.openConnection(); con.setRequestProperty("User-Agent", USERAGENT); InputStream _is; if (con.getResponseCode() == 200) { return retrieveCookies(manager); } else { _is = con.getErrorStream(); StringBuilder result = new StringBuilder(); try (BufferedReader rd = new BufferedReader(new InputStreamReader(_is))) { String line; while ((line = rd.readLine()) != null) { result.append(line); } } String source = result.toString(); //extract challenge String challenge = regex(source, "name=\"jschl_vc\" value=\"(\\w+)\""); String challenge_pass = regex(source, "name=\"pass\" value=\"(.+?)\""); //prepare String builder = regex(source, "setTimeout\\(function\\(\\)\\{\\s+(var s,t,o,p,b,r,e,a,k,i,n,g,f.+?\\r?[\\s\\S]+?a\\.value =.+?)\\r?\\s+';"); builder = builder.replaceFirst("\\s{3,}[a-z](?: = ).+form'\\);\\s+;", "").replaceFirst( "a\\.value = parseInt\\(.+?\\).+", regex(builder, "a\\.value = (parseInt\\(.+?\\)).+")); //Execute&solve System.out.println(builder); long solved = Long.parseLong(solveJS2(builder)); solved += url.getHost().length(); System.out.println("SOLVED: " + solved); try { Thread.sleep(3000); } catch (InterruptedException e) { System.out.println(e.getMessage()); } URI tmp = UrlToUri(url); List<NameValuePair> qparams = new ArrayList<>(); qparams.add(new BasicNameValuePair("jschl_vc", challenge)); qparams.add(new BasicNameValuePair("jschl_answer", String.valueOf(solved))); qparams.add(new BasicNameValuePair("pass", challenge_pass)); URIBuilder uriBuilder = new URIBuilder().setScheme(tmp.getScheme()).setPath("/cdn-cgi/l/chk_jschl") .setHost(tmp.getHost()).setParameters(qparams); HttpURLConnection cookie_req = (HttpURLConnection) new URL(uriBuilder.toString()).openConnection(); cookie_req.setRequestProperty("Referer", url.toString()); cookie_req.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:48.0) Gecko/20100101 Firefox/48.0"); HttpURLConnection.setFollowRedirects(false); cookie_req.connect(); System.out.println("ResponseCode: " + cookie_req.getResponseCode()); if (cookie_req.getResponseCode() == 200) { return retrieveCookies(manager); } else { System.out.println("Something went wrong!"); return null; } } } catch (IOException e1) { System.out.println(e1.getMessage()); return null; } }
From source file:com.hygenics.parser.GetImages.java
private void getImages() { // controls the web process from a removed method log.info("Setting Up Pull"); String[] proxyarr = (proxies == null) ? null : proxies.split(","); // cleanup//from w ww. j ava 2 s .c o m if (cleanup) { cleanupDir(fpath); } // image grab CookieManager cm = new CookieManager(); cm.setCookiePolicy(CookiePolicy.ACCEPT_ALL); CookieHandler.setDefault(cm); int numimages = 0; InputStream is; byte[] bytes; int iter = 0; int found = 0; // set proxy if needed if (proxyuser != null) { proxy(proxyhost, proxyport, https, proxyuser, proxypass); } int i = 0; ArrayList<String> postImages = new ArrayList<String>(); ForkJoinPool fjp = new ForkJoinPool(Runtime.getRuntime().availableProcessors()); Set<Callable<String>> pulls = new HashSet<Callable<String>>(); Set<Callable<ArrayList<String>>> sqls = new HashSet<Callable<ArrayList<String>>>(); List<Future<String>> imageFutures; ArrayList<String> images; int chunksize = (int) Math.ceil(commitsize / numqueries); log.info("Chunksize: " + chunksize); if (baseurl != null || baseurlcolumn != null) { do { log.info("Offset: " + offset); log.info("Getting Images"); images = new ArrayList<String>(commitsize); log.info("Getting Columns"); for (int n = 0; n < numqueries; n++) { String tempsql = sql + " WHERE " + idString + " >= " + offset + " AND " + idString + " < " + (offset + chunksize); if (conditions != null) { tempsql += conditions; } sqls.add(new QueryDatabase( ((extracondition != null) ? tempsql + " " + extracondition : tempsql))); offset += chunksize; } List<Future<ArrayList<String>>> futures = fjp.invokeAll(sqls); int w = 0; while (fjp.isQuiescent() && fjp.getActiveThreadCount() > 0) { w++; } for (Future<ArrayList<String>> f : futures) { try { ArrayList<String> fjson; fjson = f.get(); if (fjson.size() > 0) { images.addAll(fjson); } if (f.isDone() == false) { f.cancel(true); } } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } log.info(Integer.toString(images.size()) + " image links found. Pulling."); ArrayList<String> tempproxies = new ArrayList<String>(); if (proxyarr != null) { for (String proxy : proxyarr) { tempproxies.add(proxy.trim()); } } if (maxproxies > 0) { maxproxies -= 1;// 0 and 1 should be equivalent conditions // --num is not like most 0 based still due // to >= } // get images for (int num = 0; num < images.size(); num++) { String icols = images.get(num); int proxnum = (int) Math.random() * (tempproxies.size() - 1); String proxy = (tempproxies.size() == 0) ? null : tempproxies.get(proxnum); // add grab pulls.add(new ImageGrabber(icols, proxy)); if (proxy != null) { tempproxies.remove(proxy); } // check for execution if (num + 1 == images.size() || pulls.size() >= commitsize || tempproxies.size() == 0) { if (tempproxies.size() == 0 && proxies != null) { tempproxies = new ArrayList<String>(proxyarr.length); for (String p : proxyarr) { tempproxies.add(p.trim()); } } imageFutures = fjp.invokeAll(pulls); w = 0; while (fjp.isQuiescent() == false && fjp.getActiveThreadCount() > 0) { w++; } for (Future<String> f : imageFutures) { String add; try { add = f.get(); if (add != null) { postImages.add(add); } } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } imageFutures = null;// garbage collect elligible pulls = new HashSet<Callable<String>>(commitsize); } if (postImages.size() >= commitsize && addtoDB == true) { if (addtoDB) { log.info("Posting to Database"); log.info("Found " + postImages.size() + " images"); numimages += postImages.size(); int size = (int) Math.floor(postImages.size() / numqueries); for (int n = 0; n < numqueries; n++) { if (((n + 1) * size) < postImages.size() && (n + 1) < numqueries) { fjp.execute(new ImagePost(postImages.subList(n * size, (n + 1) * size))); } else { fjp.execute(new ImagePost(postImages.subList(n * size, postImages.size() - 1))); } } w = 0; while (fjp.isQuiescent() && fjp.getActiveThreadCount() > 0) { w++; } } found += postImages.size(); postImages.clear(); } } if (postImages.size() > 0 && addtoDB == true) { log.info("Posting to Database"); numimages += postImages.size(); int size = (int) Math.floor(postImages.size() / numqueries); for (int n = 0; n < numqueries; n++) { if (((n + 1) * size) < postImages.size()) { fjp.execute(new ImagePost(postImages.subList(n * size, (n + 1) * size))); } else { fjp.execute(new ImagePost(postImages.subList(n * size, postImages.size()))); } } w = 0; while (fjp.isQuiescent() && fjp.getActiveThreadCount() > 0) { w++; } found += postImages.size(); postImages.clear(); } // handle iterations specs iter += 1; log.info("Iteration: " + iter); if ((iter < iterations && found < images.size()) || tillfound == true) { log.info("Not All Images Obtained Trying Iteration " + iter + " of " + iterations); offset -= commitsize; } else if ((iter < iterations && found >= images.size()) && tillfound == false) { log.info("Images Obtained in " + iter + " iterations. Continuing."); iter = 0; } else { // precautionary log.info("Images Obtained in " + iter + " iterations. Continuing"); iter = 0; } } while (images.size() > 0 && iter < iterations); if (fjp.isShutdown()) { fjp.shutdownNow(); } } log.info("Complete. Check for Errors \n " + numimages + " Images Found"); }
From source file:no.eris.applet.AppletViewer.java
private void overrideCookieHandler(CookieManager manager) { manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); final CookieHandler handler = CookieHandler.getDefault(); LOGGER.debug("CookieStore: size {}", manager.getCookieStore().getCookies().size()); if (cookies != null) { for (UriAndCookie uriAndCookie : cookies) { URI uri = uriAndCookie.getUri(); HttpCookie cookie = uriAndCookie.getCookie(); LOGGER.debug("Adding cookies: <{}> value={} secure={}", new Object[] { uri, cookie, cookie.getSecure() }); manager.getCookieStore().add(uri, cookie); }/*from w ww . j av a 2 s .c o m*/ } LOGGER.debug("CookieStore: size {}", manager.getCookieStore().getCookies().size()); LOGGER.debug("Overriding cookie handler: {}", (handler == null ? null : handler.getClass().getName())); // FIXME because we depend on the system-wide cookie manager, we probably cannot run multiple applets at the time // we also maybe have some security issues lurking here... // I could maybe partition the callers based on the ThreadGroup ?? // FIXME theres also some cleanup to do somewhere CookieHandler.setDefault(new LoggingCookieHandler(manager)); }
From source file:org.oscarehr.integration.excelleris.com.colcamex.www.core.Connect.java
private void _init() { CookieManager cookieManager = new CookieManager(); cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); CookieHandler.setDefault(cookieManager); }
From source file:test.be.fedict.eid.applet.ControllerTest.java
@Test public void controllerIdentification() throws Exception { // setup// ww w . j a v a2 s .com Messages messages = new Messages(Locale.getDefault()); Runtime runtime = new TestRuntime(); View view = new TestView(); Controller controller = new Controller(view, runtime, messages); // make sure that the session cookies are passed during conversations CookieManager cookieManager = new CookieManager(); cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); CookieHandler.setDefault(cookieManager); // operate controller.run(); // verify LOG.debug("verify..."); SessionHandler sessionHandler = this.servletTester.getContext().getSessionHandler(); SessionManager sessionManager = sessionHandler.getSessionManager(); LOG.debug("session manager type: " + sessionManager.getClass().getName()); HashSessionManager hashSessionManager = (HashSessionManager) sessionManager; LOG.debug("# sessions: " + hashSessionManager.getSessions()); assertEquals(1, hashSessionManager.getSessions()); Map<String, HttpSession> sessionMap = hashSessionManager.getSessionMap(); LOG.debug("session map: " + sessionMap); Entry<String, HttpSession> sessionEntry = sessionMap.entrySet().iterator().next(); HttpSession httpSession = sessionEntry.getValue(); assertNotNull(httpSession.getAttribute("eid")); Identity identity = (Identity) httpSession.getAttribute("eid.identity"); assertNotNull(identity); assertNotNull(identity.name); LOG.debug("name: " + identity.name); LOG.debug("document type: " + identity.getDocumentType()); LOG.debug("duplicate: " + identity.getDuplicate()); assertNull(httpSession.getAttribute("eid.identifier")); assertNull(httpSession.getAttribute("eid.address")); assertNull(httpSession.getAttribute("eid.photo")); }