List of usage examples for org.apache.http.impl.client BasicCookieStore BasicCookieStore
public BasicCookieStore()
From source file:org.alfresco.selenium.FetchHttpClient.java
public static CloseableHttpClient getHttpClient(final WebDriver driver) { BasicCookieStore cookieStore = new BasicCookieStore(); cookieStore.addCookie(generateSessionCookie(driver)); //Create http client to retrieve the file. return HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build(); }
From source file:com.ibm.xsp.xflow.activiti.util.HttpClientUtil.java
public static String post(String url, Cookie[] cookies, String content) { String body = null;/* w w w . ja v a 2 s . c om*/ try { StringBuffer buffer = new StringBuffer(); DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost postRequest = new HttpPost(url); StringEntity input = new StringEntity(content); input.setContentType("application/json"); postRequest.setEntity(input); httpClient.setCookieStore(new BasicCookieStore()); String hostName = new URL(url).getHost(); String domain = hostName.substring(hostName.indexOf(".")); for (int i = 0; i < cookies.length; i++) { if (logger.isLoggable(Level.FINEST)) { logger.finest("Cookie:" + cookies[i].getName() + ":" + cookies[i].getValue() + ":" + cookies[i].getDomain() + ":" + cookies[i].getPath()); } BasicClientCookie cookie = new BasicClientCookie(cookies[i].getName(), cookies[i].getValue()); cookie.setVersion(0); cookie.setDomain(domain); cookie.setPath("/"); httpClient.getCookieStore().addCookie(cookie); } HttpResponse response = httpClient.execute(postRequest); BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent()))); String output; if (logger.isLoggable(Level.FINEST)) { logger.finest("Output from Server .... \n"); } while ((output = br.readLine()) != null) { if (logger.isLoggable(Level.FINEST)) { logger.finest(output); } buffer.append(output); } httpClient.getConnectionManager().shutdown(); body = buffer.toString(); } catch (Exception e) { e.printStackTrace(); } return body; }
From source file:com.nebkat.plugin.youtube.YoutubeRetriever.java
public static String getLocation(String videoId) { CookieStore cookieStore = new BasicCookieStore(); HttpContext localContext = new BasicHttpContext(); localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); HttpGet get = new HttpGet(String.format(YOUTUBE_API_URL, videoId)); get.setHeader("User-Agent", USER_AGENT); HttpResponse response;/*w w w . ja va2 s .co m*/ try { response = ConnectionManager.getHttpClient().execute(get, localContext); } catch (IOException ex) { get.abort(); return ""; } if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { get.abort(); return ""; } String videoInfoData; try (BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()))) { StringBuilder builder = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { builder.append(line); } videoInfoData = builder.toString(); } catch (IOException ex) { return ""; } Map<String, String> videoInfo = getNameValuePairMap(videoInfoData); String[] formats = videoInfo.get("url_encoded_fmt_stream_map").split(","); for (String format : formats) { Map<String, String> formatInfo = getNameValuePairMap(format); String itag = formatInfo.get("itag"); if (itag.equals(Integer.toString(PREFERRED_FORMAT))) { String url = formatInfo.get("url"); String sig = formatInfo.get("sig"); return url + "&signature=" + sig; } } return ""; }
From source file:com.gooddata.http.client.CookieUtilsTest.java
@Before public void setUp() { context = new BasicHttpContext(); cookieStore = new BasicCookieStore(); context.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore); }
From source file:com.logicoy.pdmp.pmpi.http.SessionObject.java
public SessionObject(String interconnectId) { this.interconnectId = interconnectId; this.cookieStore = new BasicCookieStore(); }
From source file:zz.pseas.ghost.utils.DownloadUtil.java
public static CookieStore convertToCookieStore(Set<Cookie> cookies) { BasicCookieStore store = new BasicCookieStore(); for (Cookie c : cookies) { BasicClientCookie c1 = new BasicClientCookie(c.getName(), c.getValue()); c1.setDomain(c.getDomain());/* w ww .ja v a 2s . c o m*/ c1.setPath(c.getPath()); c1.setExpiryDate(c.getExpiry()); store.addCookie(c1); } return store; }
From source file:simple.crawler.ext.vanilla.VanillaForumUtil.java
public static HttpContext login(String loginURL, String username, String password) throws Exception { DefaultHttpClient httpclient = HttpClientFactory.getInstance(); CookieStore cookieStore = new BasicCookieStore(); HttpContext httpContext = new BasicHttpContext(); httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); HttpResponse res = httpclient.execute(new HttpGet(loginURL), httpContext); String html = HttpClientUtil.getContentBodyAsString(res); HtmlParser parser = new HtmlParser(); Document doc = parser.parseNonWellForm(html); ///*from ww w . j a va 2s . c om*/ Node loginTransientKey = (Node) XPathUtil.read(doc, "//*[@id=\"Form_TransientKey\"]", XPathConstants.NODE); Node hpt = (Node) XPathUtil.read(doc, "//*[@id=\"Form_hpt\"]", XPathConstants.NODE); Node target = (Node) XPathUtil.read(doc, "//*[@id=\"Form_Target\"]", XPathConstants.NODE); Node clientHour = (Node) XPathUtil.read(doc, "//*[@id=\"Form_ClientHour\"]", XPathConstants.NODE); // List<NameValuePair> list = new ArrayList<NameValuePair>(); list.add(new BasicNameValuePair("Form/TransientKey", ((Element) loginTransientKey).getAttribute("value"))); list.add(new BasicNameValuePair("Form/hpt", ((Element) hpt).getAttribute("value"))); list.add(new BasicNameValuePair("Form/Target", ((Element) target).getAttribute("value"))); list.add(new BasicNameValuePair("Form/ClientHour", ((Element) clientHour).getAttribute("value"))); list.add(new BasicNameValuePair("Form/Email", "admin")); list.add(new BasicNameValuePair("Form/Password", "admin")); list.add(new BasicNameValuePair("Form/Sign_In", "Sign In")); list.add(new BasicNameValuePair("Form/RememberMe", "1")); list.add(new BasicNameValuePair("Checkboxes[]", "RememberMe")); HttpPost post = new HttpPost(loginURL); post.setEntity(new UrlEncodedFormEntity(list)); res = httpclient.execute(post, httpContext); return httpContext; }
From source file:com.ibm.watson.app.common.util.http.HttpClientBuilder.java
public static CloseableHttpClient buildDefaultHttpClient(Credentials cred) { // Use custom cookie store if necessary. CookieStore cookieStore = new BasicCookieStore(); // Use custom credentials provider if necessary. CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(); RequestConfig defaultRequestConfig;/*from w w w . j av a 2s. com*/ //private DefaultHttpClient client; connManager.setMaxTotal(200); connManager.setDefaultMaxPerRoute(50); try { SSLContextBuilder builder = new SSLContextBuilder(); builder.loadTrustMaterial(null, new TrustSelfSignedStrategy()); // Create a registry of custom connection socket factories for supported // protocol schemes. Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder .<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.INSTANCE) .register("https", new SSLConnectionSocketFactory(builder.build())).build(); } catch (Exception e) { logger.warn(MessageKey.AQWEGA02000W_unable_init_ssl_context.getMessage(), e); } // Create global request configuration defaultRequestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.BEST_MATCH) .setExpectContinueEnabled(true) .setTargetPreferredAuthSchemes( Arrays.asList(AuthSchemes.BASIC, AuthSchemes.NTLM, AuthSchemes.DIGEST)) .setAuthenticationEnabled(true).build(); if (cred != null) credentialsProvider.setCredentials(AuthScope.ANY, cred); return HttpClients.custom().setConnectionManager(connManager).setDefaultCookieStore(cookieStore) .setDefaultCredentialsProvider(credentialsProvider).setDefaultRequestConfig(defaultRequestConfig) .build(); }
From source file:com.cloud.utils.rest.HttpClientHelper.java
public static CloseableHttpClient createHttpClient(final int maxRedirects) throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException { final Registry<ConnectionSocketFactory> socketFactoryRegistry = createSocketFactoryConfigration(); final BasicCookieStore cookieStore = new BasicCookieStore(); return HttpClientBuilder.create() .setConnectionManager(new PoolingHttpClientConnectionManager(socketFactoryRegistry)) .setRedirectStrategy(new LaxRedirectStrategy()) .setDefaultRequestConfig(RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT) .setMaxRedirects(maxRedirects).build()) .setDefaultCookieStore(cookieStore).setRetryHandler(new StandardHttpRequestRetryHandler()).build(); }
From source file:com.electronicpanopticon.spring.web.rest.StatefullRestTemplate.java
public StatefullRestTemplate() { super();/*from w ww .java 2 s. com*/ httpClient = new DefaultHttpClient(); cookieStore = new BasicCookieStore(); httpContext = new BasicHttpContext(); httpContext.setAttribute(ClientContext.COOKIE_STORE, getCookieStore()); statefullHttpComponentsClientHttpRequestFactory = new StatefullHttpComponentsClientHttpRequestFactory( httpClient, httpContext); super.setRequestFactory(statefullHttpComponentsClientHttpRequestFactory); }