private Answer<HttpResponse> createOkResponseWithCookie() { return new Answer<HttpResponse>() { @Override public HttpResponse answer(InvocationOnMock invocation) throws Throwable { HttpContext context = (HttpContext) invocation.getArguments()[1]; if (context.getAttribute(ClientContext.COOKIE_STORE) != null) { BasicCookieStore cookieStore = (BasicCookieStore) context.getAttribute(ClientContext.COOKIE_STORE); BasicClientCookie cookie = new BasicClientCookie("cookie", "meLikeCookie"); cookieStore.addCookie(cookie); } return OK_200_RESPONSE; } }; }
public Map<String, String> connect(String url, Map<String, Object> parameters) throws ManagerResponseException { Map<String, String> response = new HashMap<String, String>(); CloseableHttpClient httpclient = HttpClients.createDefault(); List<NameValuePair> nvps = new ArrayList<>(); nvps.add(new BasicNameValuePair("j_username", (String) parameters.get("login"))); nvps.add(new BasicNameValuePair("j_password", (String) parameters.get("password"))); localContext = HttpClientContext.create(); localContext.setCookieStore(new BasicCookieStore()); HttpPost httpPost = new HttpPost(url); try { httpPost.setEntity(new UrlEncodedFormEntity(nvps)); CloseableHttpResponse httpResponse = httpclient.execute(httpPost, localContext); ResponseHandler<String> handler = new CustomResponseErrorHandler(); String body = handler.handleResponse(httpResponse); response.put(BODY, body); httpResponse.close(); } catch (Exception e) { authentificationUtils.getMap().clear(); throw new ManagerResponseException(e.getMessage(), e); } return response; }
public QMailHttpClient getHttpClient() throws MessagingException { if (httpClient == null) { httpClient = httpClientFactory.create(); // Disable automatic redirects on the http client. httpClient.getParams().setBooleanParameter("http.protocol.handle-redirects", false); // Setup a cookie store for forms-based authentication. httpContext = new BasicHttpContext(); authCookies = new BasicCookieStore(); httpContext.setAttribute(ClientContext.COOKIE_STORE, authCookies); SchemeRegistry reg = httpClient.getConnectionManager().getSchemeRegistry(); try { Scheme s = new Scheme("https", new WebDavSocketFactory(hostname, 443), 443); reg.register(s); } catch (NoSuchAlgorithmException nsa) { Timber.e(nsa, "NoSuchAlgorithmException in getHttpClient"); throw new MessagingException("NoSuchAlgorithmException in getHttpClient: ", nsa); } catch (KeyManagementException kme) { Timber.e(kme, "KeyManagementException in getHttpClient"); throw new MessagingException("KeyManagementException in getHttpClient: ", kme); } } return httpClient; }
/** * Remove a Cookie by name and path * * @param name cookie name * @param path cookie path */ @PublicAtsApi public void removeCookie( String name, String path ) { BasicCookieStore cookieStore = (BasicCookieStore) httpContext.getAttribute(HttpClientContext.COOKIE_STORE); if (cookieStore != null) { List<Cookie> cookies = cookieStore.getCookies(); cookieStore.clear(); for (Cookie cookie : cookies) { if (!cookie.getName().equals(name) || !cookie.getPath().equals(path)) { cookieStore.addCookie(cookie); } } } }
public static CookieStore getuCookie() { CookieStore uCookie = new BasicCookieStore(); try { String COOKIE_S_LINKDATA = LemallPlatform.getInstance().getCookieLinkdata(); if (!TextUtils.isEmpty(COOKIE_S_LINKDATA)) { String[] cookies = COOKIE_S_LINKDATA.split("&"); for (String item : cookies) { String[] keyValue = item.split(SearchCriteria.EQ); if (keyValue.length == 2) { if (OtherUtil.isContainsChinese(keyValue[1])) { keyValue[1] = URLEncoder.encode(keyValue[1], "UTF-8"); } BasicClientCookie cookie = new BasicClientCookie(keyValue[0], keyValue[1]); cookie.setVersion(0); cookie.setDomain(".lemall.com"); cookie.setPath("/"); uCookie.addCookie(cookie); } } } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return uCookie; }
public void process(HttpRequest httpRequest, HttpContext httpContext) throws HttpException, IOException { final HttpClientContext clientContext = HttpClientContext.adapt(httpContext); List<Cookie> cookies = clientContext.getCookieStore().getCookies(); boolean set = (null != StickyCookieHolder.getTestStickySessionCookie()); boolean found = false; ListIterator<Cookie> it = cookies.listIterator(); while (it.hasNext()) { Cookie cookie = it.next(); if (cookie.getName().equals(StickyCookieHolder.COOKIE_NAME)) { found = true; if (set) { // set the cookie with the value saved for each thread using the rule it.set(StickyCookieHolder.getTestStickySessionCookie()); } else { // if the cookie is not set in TestStickySessionRule, remove it from here it.remove(); } } } // if the cookie needs to be set from TestStickySessionRule but did not exist in the client cookie list, add it here. if (!found && set) { cookies.add(StickyCookieHolder.getTestStickySessionCookie()); } BasicCookieStore cs = new BasicCookieStore(); cs.addCookies(cookies.toArray(new Cookie[cookies.size()])); httpContext.setAttribute(HttpClientContext.COOKIE_STORE, cs); }
@Before public void setUp() throws IOException { when(mockHubConnection.createHubProxy("CoreHub")).thenReturn(mockHubProxy); HttpResponse mockResponse = createResponse(HttpStatus.SC_OK,STATUS_OK_TEXT,"Bittrex"); when(mockHttpClientContext.getCookieStore()).thenReturn(new BasicCookieStore()); when(mockHttpClient.execute(argThat(UrlMatcher.matchesUrl("https://bittrex.com")),eq(mockHttpClientContext))) .thenReturn(mockResponse); when(mockHttpFactory.createClient()).thenReturn(mockHttpClient); when(mockHttpFactory.createClientContext()).thenReturn(mockHttpClientContext); when(mockHttpFactory.createHubConnection(any(),any(),anyBoolean(),any())).thenReturn(mockHubConnection); bittrexExchange = new BittrexExchange(0,"apikey","secret", mockHttpFactory); lambdaCalled = false; }
private HttpUtils(HttpRequestBase request) { this.request = request; this.clientBuilder = HttpClientBuilder.create(); this.isHttps = request.getURI().getScheme().equalsIgnoreCase("https"); this.config = RequestConfig.custom().setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY); this.cookieStore = new BasicCookieStore(); if (request instanceof HttpPost) { this.type = 1; this.builder = EntityBuilder.create().setParameters(new ArrayList<NameValuePair>()); } else if (request instanceof HttpGet) { this.type = 2; this.uriBuilder = new URIBuilder(); } else if (request instanceof HttpPut) { this.type = 3; this.builder = EntityBuilder.create().setParameters(new ArrayList<NameValuePair>()); } else if (request instanceof HttpDelete) { this.type = 4; this.uriBuilder = new URIBuilder(); } }
public static void setCookieStore(HttpResponse httpResponse) { System.out.println("----setCookieStore"); cookieStore = new BasicCookieStore(); // JSESSIONID String setCookie = httpResponse.getFirstHeader("Set-Cookie") .getValue(); String JSESSIONID = setCookie.substring("JSESSIONID=".length(), setCookie.indexOf(";")); System.out.println("JSESSIONID:" + JSESSIONID); // 新建一个Cookie BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", JSESSIONID); cookie.setVersion(0); //cookie.setDomain("127.0.0.1"); //cookie.setPath("/CwlProClient"); // cookie.setAttribute(ClientCookie.VERSION_ATTR, "0"); // cookie.setAttribute(ClientCookie.DOMAIN_ATTR, "127.0.0.1"); // cookie.setAttribute(ClientCookie.PORT_ATTR, "8080"); // cookie.setAttribute(ClientCookie.PATH_ATTR, "/CwlProWeb"); cookieStore.addCookie(cookie); }
public HttpTransportClient(int retryAttemptsNetworkErrorCount, int retryAttemptsInvalidStatusCount) { this.retryAttemptsNetworkErrorCount = retryAttemptsNetworkErrorCount; this.retryAttemptsInvalidStatusCount = retryAttemptsInvalidStatusCount; CookieStore cookieStore = new BasicCookieStore(); RequestConfig requestConfig = RequestConfig.custom() .setSocketTimeout(SOCKET_TIMEOUT_MS) .setConnectTimeout(CONNECTION_TIMEOUT_MS) .setConnectionRequestTimeout(CONNECTION_TIMEOUT_MS) .setCookieSpec(CookieSpecs.STANDARD) .build(); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); connectionManager.setMaxTotal(MAX_SIMULTANEOUS_CONNECTIONS); connectionManager.setDefaultMaxPerRoute(MAX_SIMULTANEOUS_CONNECTIONS); httpClient = HttpClients.custom() .setConnectionManager(connectionManager) .setDefaultRequestConfig(requestConfig) .setDefaultCookieStore(cookieStore) .setUserAgent(USER_AGENT) .build(); }
@Override public BasicHttpContext createContext(final HttpHost targetHost) { BasicHttpContext context = CACHED_CONTEXTS.get(targetHost); if (context == null) { log.trace("creating and configuring new HttpContext for system user"); context = basicAuthConfigurator.createContext(targetHost); final CookieStore cookieStore = new BasicCookieStore(); context.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore); log.trace("caching HttpContext for system user"); CACHED_CONTEXTS.put(targetHost, context); } else { log.trace("using cached HttpContext for system user"); } return context; }
public boolean authenticate(String username, String password) { closeQuietly(); cookieStore = new BasicCookieStore(); httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).setDefaultCookieStore(cookieStore).build(); String url = concatePath(baseUrl, "index.php"); String result = internalHttpPost(url, new BasicNameValuePair("username", username), new BasicNameValuePair("password", password), new BasicNameValuePair("action", "login"), new BasicNameValuePair("view", "console")); if (result.contains("loginForm") == true) { log.warn("Authentication for user '" + username + "' wasn't sucessful."); return false; } log.info("Authentication for user '" + username + "' was sucessful."); authorized = true; return true; }
/** * Authentication with google account * @param cookies cookies list keys from google auth * @param login username associated to zds login * @param id user id on ZdS associated to login */ public void authToGoogle(List<HttpCookie> cookies, String login, String id) { if(login != null && id != null) { this.login = login; this.idUser = id; log.info("L'identifiant de l'utilisateur " + this.login + " est : " + idUser); cookieStore = new BasicCookieStore(); for(HttpCookie cookie:cookies) { BasicClientCookie c = new BasicClientCookie(cookie.getName(), cookie.getValue()); c.setDomain(cookie.getDomain()); c.setPath(cookie.getPath()); c.setSecure(cookie.getSecure()); c.setVersion(cookie.getVersion()); c.setComment(cookie.getComment()); cookieStore.addCookie(c); } context.setCookieStore(cookieStore); this.authenticated = true; } else { log.debug("Le login de l'utilisateur n'a pas pu être trouvé"); } }
@Override public BasicHttpContext createContext(final HttpHost targetHost) { final CookieStore cookieStore = new BasicCookieStore(); final BasicClientCookie clientCookie = new BasicClientCookie(cookie.getName(), cookie.getValue()); clientCookie.setDomain(targetHost.getHostName()); clientCookie.setPath("/"); cookieStore.addCookie(clientCookie); final BasicHttpContext context = new BasicHttpContext(); context.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore); return context; }
public WebDavHttpClient getHttpClient() throws MessagingException { if (mHttpClient == null) { mHttpClient = mHttpClientFactory.create(); // Disable automatic redirects on the http client. mHttpClient.getParams().setBooleanParameter("http.protocol.handle-redirects", false); // Setup a cookie store for forms-based authentication. mContext = new BasicHttpContext(); mAuthCookies = new BasicCookieStore(); mContext.setAttribute(ClientContext.COOKIE_STORE, mAuthCookies); SchemeRegistry reg = mHttpClient.getConnectionManager().getSchemeRegistry(); try { Scheme s = new Scheme("https", new WebDavSocketFactory(mHost, 443), 443); reg.register(s); } catch (NoSuchAlgorithmException nsa) { Log.e(LOG_TAG, "NoSuchAlgorithmException in getHttpClient: " + nsa); throw new MessagingException("NoSuchAlgorithmException in getHttpClient: " + nsa); } catch (KeyManagementException kme) { Log.e(LOG_TAG, "KeyManagementException in getHttpClient: " + kme); throw new MessagingException("KeyManagementException in getHttpClient: " + kme); } } return mHttpClient; }
public static CloseableHttpClient createHttpClient(final int maxRedirects) throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException { s_logger.info("Creating new HTTP connection pool and client"); final Registry<ConnectionSocketFactory> socketFactoryRegistry = createSocketFactoryConfigration(); final BasicCookieStore cookieStore = new BasicCookieStore(); final PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry); connManager.setDefaultMaxPerRoute(MAX_ALLOCATED_CONNECTIONS_PER_ROUTE); connManager.setMaxTotal(MAX_ALLOCATED_CONNECTIONS); final RequestConfig requestConfig = RequestConfig.custom() .setCookieSpec(CookieSpecs.DEFAULT) .setMaxRedirects(maxRedirects) .setSocketTimeout(DEFAULT_SOCKET_TIMEOUT) .setConnectionRequestTimeout(DEFAULT_CONNECTION_REQUEST_TIMEOUT) .setConnectTimeout(DEFAULT_CONNECT_TIMEOUT) .build(); return HttpClientBuilder.create() .setConnectionManager(connManager) .setRedirectStrategy(new LaxRedirectStrategy()) .setDefaultRequestConfig(requestConfig) .setDefaultCookieStore(cookieStore) .setRetryHandler(new StandardHttpRequestRetryHandler()) .build(); }
@Ignore public void grabKey() throws Exception { BasicCookieStore store = new BasicCookieStore(); HttpClient client = HttpClientBuilder.create().setDefaultCookieStore(store).build(); store.addCookie(new BCookie("LIVE_LOGIN_DATA", LIVE_LOGIN_DATA, "live.bilibili.com")); store.addCookie(new BCookie("DedeUserID", DEDE_USER_ID, "live.bilibili.com")); store.addCookie(new BCookie("SESSDATA", SESS_DATA, "live.bilibili.com")); store.addCookie(new BCookie("ck_pv", CK_PV, "live.bilibili.com")); store.addCookie(new BCookie("DedeUserID__ckMd5", DEDE_USER_ID_CKMD5, "live.bilibili.com")); Session tempSession = new Session(client, store); for(int i = 0;i < 50; i++) { File file = new File("/captcha/captcha" + i + ".png"); file.mkdirs(); if (!file.exists()) file.createNewFile(); ImageIO.write(ImageIO.read(HttpHelper.responseToInputStream(tempSession.getHttpHelper() .createGetBiliLiveHost("/freeSilver/getCaptcha"))), "png", file); System.out.println(file.getAbsolutePath()); } }
@Before public void setUp() { this.target = new HttpHost("localhost.local", 80); this.cookieStore = new BasicCookieStore(); final BasicClientCookie2 cookie1 = new BasicClientCookie2("name1", "value1"); cookie1.setVersion(1); cookie1.setDomain("localhost.local"); cookie1.setPath("/"); this.cookieStore.addCookie(cookie1); final BasicClientCookie2 cookie2 = new BasicClientCookie2("name2", "value2"); cookie2.setVersion(1); cookie2.setDomain("localhost.local"); cookie2.setPath("/"); this.cookieStore.addCookie(cookie2); this.cookieSpecRegistry = RegistryBuilder.<CookieSpecProvider>create() .register(CookieSpecs.DEFAULT, new DefaultCookieSpecProvider()) .register(CookieSpecs.STANDARD, new RFC2965SpecProvider()) .register(CookieSpecs.NETSCAPE, new NetscapeDraftSpecProvider()) .register(CookieSpecs.IGNORE_COOKIES, new IgnoreSpecProvider()) .build(); }
@Test(timeout = 60000) public void test_multipleRequest() throws Exception { final CookieStore cookieStore = new BasicCookieStore(); executeRequest("read", serverPort1, cookieStore); Thread thread = new Thread(new Runnable() { @Override public void run() { try { executeRequest("write_wait", serverPort1, cookieStore); } catch (Exception e) { e.printStackTrace(); } } }); thread.start(); Thread.sleep(500); executeRequest("read", serverPort1, cookieStore); thread.join(); assertEquals("value", executeRequest("read", serverPort1, cookieStore)); }
@Test(timeout = 60000) public void testAttributeReloadSession() throws Exception { IMap<String, Object> map = hz.getMap(DEFAULT_MAP_NAME); CookieStore cookieStore = new BasicCookieStore(); assertEquals("true", executeRequest("write", serverPort1, cookieStore)); assertEquals(1, map.size()); String oldSessionId = findHazelcastSessionId(map); assertNotNull(oldSessionId); assertEquals("value", executeRequest("read", serverPort2, cookieStore)); assertEquals("true", executeRequest("reload", serverPort2, cookieStore)); String newSessionId = findHazelcastSessionId(map); assertEquals(1, map.size()); assertNotNull(map.get(newSessionId)); SessionState sessionState = (SessionState) map.get(newSessionId); SerializationService ss = getNode(hz).getSerializationService(); assertEquals("first-value", ss.toObject(sessionState.getAttributes().get("first-key"))); assertEquals("second-value", ss.toObject(sessionState.getAttributes().get("second-key"))); }
@Test(timeout = 80000) public void testAttributeInvalidate() throws Exception { CookieStore cookieStore = new BasicCookieStore(); executeRequest("write", SERVER_PORT_1, cookieStore); String value = executeRequest("read", SERVER_PORT_1, cookieStore); assertEquals("value", value); value = executeRequest("invalidate", SERVER_PORT_1, cookieStore); assertEquals("true", value); HazelcastInstance instance = createHazelcastInstance(); IMap<Object, Object> map = instance.getMap("default"); assertEquals(0, map.size()); }
@Test public void whenClusterIsDown_enabledDeferredWrite() throws Exception { CookieStore cookieStore = new BasicCookieStore(); assertEquals("true", executeRequest("write", serverPort1, cookieStore)); assertEquals("value", executeRequest("read", serverPort1, cookieStore)); hz.shutdown(); assertEquals("value", executeRequest("read", serverPort1, cookieStore)); assertEquals("true", executeRequest("remove", serverPort1, cookieStore)); assertEquals("null", executeRequest("read", serverPort1, cookieStore)); hz = Hazelcast.newHazelcastInstance( new FileSystemXmlConfig(new File(sourceDir + "/WEB-INF/", "hazelcast.xml"))); assertClusterSizeEventually(1, hz); assertEquals("true", executeRequest("write", serverPort1, cookieStore)); assertEquals("value", executeRequest("read", serverPort1, cookieStore)); }
@Test public void whenClusterIsDownAtBeginning_enabledDeferredWrite() throws Exception { hz.shutdown(); CookieStore cookieStore = new BasicCookieStore(); assertEquals("true", executeRequest("write", serverPort1, cookieStore)); assertEquals("value", executeRequest("read", serverPort1, cookieStore)); assertEquals("value", executeRequest("read", serverPort1, cookieStore)); assertEquals("true", executeRequest("remove", serverPort1, cookieStore)); assertEquals("null", executeRequest("read", serverPort1, cookieStore)); assertEquals("true", executeRequest("write", serverPort1, cookieStore)); assertEquals("value", executeRequest("read", serverPort1, cookieStore)); hz = Hazelcast.newHazelcastInstance( new FileSystemXmlConfig(new File(sourceDir + "/WEB-INF/", "hazelcast.xml"))); assertClusterSizeEventually(1, hz); assertEquals("value", executeRequest("read", serverPort1, cookieStore)); assertEquals("true", executeRequest("remove", serverPort1, cookieStore)); assertEquals("null", executeRequest("read", serverPort1, cookieStore)); }
@Test public void testWhenClusterIsDownAtBeginningInNonDeferredMode() throws Exception { if (!testName.equals("client - not deferred")) { return; } hz.shutdown(); CookieStore cookieStore = new BasicCookieStore(); assertEquals("true", executeRequest("write", serverPort1, cookieStore)); assertEquals("value", executeRequest("read", serverPort1, cookieStore)); hz = Hazelcast.newHazelcastInstance( new FileSystemXmlConfig(new File(sourceDir + "/WEB-INF/", "hazelcast.xml"))); assertClusterSizeEventually(1, hz); assertEquals("value", executeRequest("read", serverPort1, cookieStore)); IMap<String, Object> map = hz.getMap(DEFAULT_MAP_NAME); assertEquals(1, map.size()); }
@Test public void testWhenClusterIsDownAtBeginningInDeferedMode() throws Exception { if (!testName.equals("client - not deferred")) { return; } hz.shutdown(); CookieStore cookieStore = new BasicCookieStore(); assertEquals("true", executeRequest("write", serverPort1, cookieStore)); assertEquals("value", executeRequest("read", serverPort1, cookieStore)); hz = Hazelcast.newHazelcastInstance( new FileSystemXmlConfig(new File(sourceDir + "/WEB-INF/", "hazelcast.xml"))); assertClusterSizeEventually(1, hz); assertEquals("value", executeRequest("read", serverPort1, cookieStore)); IMap<String, Object> map = hz.getMap(DEFAULT_MAP_NAME); assertEquals(0, map.size()); }
private HttpClientContext createContext(Authentication auth, HttpHost target) { HttpClientContext httpClientContext = HttpClientContext.create(); CookieStore cookieStore = new BasicCookieStore(); httpClientContext.setCookieStore(cookieStore); if (auth.usePreemptiveAuthentication()) { httpClientContext.setAuthCache(new Auth().getAuthCache(auth, target)); } return httpClientContext; }
/** * @param url * @param parameters * @param log * @return * @throws MojoExecutionException */ public Map<String, String> connect( String url, Map<String, Object> parameters, Log log ) throws MojoExecutionException { Map<String, String> response = new HashMap<String, String>(); CloseableHttpClient httpclient = HttpClients.createDefault(); List<NameValuePair> nvps = new ArrayList<>(); nvps.add( new BasicNameValuePair( "j_username", (String) parameters.get( "login" ) ) ); nvps.add( new BasicNameValuePair( "j_password", (String) parameters.get( "password" ) ) ); localContext = HttpClientContext.create(); localContext.setCookieStore( new BasicCookieStore() ); HttpPost httpPost = new HttpPost( url ); try { httpPost.setEntity( new UrlEncodedFormEntity( nvps ) ); CloseableHttpResponse httpResponse = httpclient.execute( httpPost, localContext ); ResponseHandler<String> handler = new ResponseErrorHandler(); String body = handler.handleResponse( httpResponse ); response.put( "body", body ); httpResponse.close(); isConnected = true; log.info( "Connection successful" ); } catch ( Exception e ) { log.error( "Connection failed! : " + e.getMessage() ); isConnected = false; throw new MojoExecutionException( "Connection failed, please check your manager location or your credentials" ); } return response; }
/** * Clear all cookies */ @PublicAtsApi public void clearCookies() { BasicCookieStore cookieStore = (BasicCookieStore) httpContext.getAttribute(HttpClientContext.COOKIE_STORE); if (cookieStore != null) { cookieStore.clear(); } }
public static CloseableHttpAsyncClient createSSLClient(HttpHost proxy) { TrustStrategy acceptingTrustStrategy = new TrustStrategy() { @Override public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { return true; } }; try { SSLContext sslContext = SSLContexts.custom() .loadTrustMaterial(null, acceptingTrustStrategy) .build(); HttpAsyncClientBuilder client = HttpAsyncClients.custom() .setDefaultCookieStore(new BasicCookieStore()) .setSSLContext(sslContext) .setSSLHostnameVerifier(SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); if (proxy !=null) { client.setProxy(proxy); } return client.build(); } catch (Exception e) { System.out.println("Could not create SSLContext"); return null; } }
/** * 获取Http客户端连接对象 * @param timeOut 超时时间 * @param proxy 代理 * @param cookie Cookie * @return Http客户端连接对象 */ public CloseableHttpClient createHttpClient(int timeOut,HttpHost proxy,BasicClientCookie cookie) { // 创建Http请求配置参数 RequestConfig.Builder builder = RequestConfig.custom() // 获取连接超时时间 .setConnectionRequestTimeout(timeOut) // 请求超时时间 .setConnectTimeout(timeOut) // 响应超时时间 .setSocketTimeout(timeOut) .setCookieSpec(CookieSpecs.STANDARD); if (proxy!=null) { builder.setProxy(proxy); } RequestConfig requestConfig = builder.build(); // 创建httpClient HttpClientBuilder httpClientBuilder = HttpClients.custom(); httpClientBuilder // 把请求相关的超时信息设置到连接客户端 .setDefaultRequestConfig(requestConfig) // 把请求重试设置到连接客户端 .setRetryHandler(new RetryHandler()) // 配置连接池管理对象 .setConnectionManager(connManager); if (cookie!=null) { CookieStore cookieStore = new BasicCookieStore(); cookieStore.addCookie(cookie); httpClientBuilder.setDefaultCookieStore(cookieStore); } return httpClientBuilder.build(); }
/** * 获取Http客户端连接对象 * @param timeOut 超时时间 * @param proxy 代理 * @param cookie Cookie * @return Http客户端连接对象 */ private CloseableHttpClient createHttpClient(int timeOut,HttpHost proxy,BasicClientCookie cookie) { // 创建Http请求配置参数 RequestConfig.Builder builder = RequestConfig.custom() // 获取连接超时时间 .setConnectionRequestTimeout(timeOut) // 请求超时时间 .setConnectTimeout(timeOut) // 响应超时时间 .setSocketTimeout(timeOut) .setCookieSpec(CookieSpecs.STANDARD); if (proxy!=null) { builder.setProxy(proxy); } RequestConfig requestConfig = builder.build(); // 创建httpClient HttpClientBuilder httpClientBuilder = HttpClients.custom(); httpClientBuilder // 把请求相关的超时信息设置到连接客户端 .setDefaultRequestConfig(requestConfig) // 把请求重试设置到连接客户端 .setRetryHandler(new RetryHandler()) // 配置连接池管理对象 .setConnectionManager(connManager); if (cookie!=null) { CookieStore cookieStore = new BasicCookieStore(); cookieStore.addCookie(cookie); httpClientBuilder.setDefaultCookieStore(cookieStore); } return httpClientBuilder.build(); }
/** * Setup some variables */ public void setup() { log.info("Setup..."); if (StringUtils.isEmpty(this.username)) { throw new IllegalArgumentException("Username is mandatory."); } if (StringUtils.isEmpty(this.password)) { throw new IllegalArgumentException("Password is mandatory."); } this.deviceId = InstagramHashUtil.generateDeviceId(this.username, this.password); if (StringUtils.isEmpty(this.uuid)) { this.uuid = InstagramGenericUtil.generateUuid(true); } if (this.cookieStore == null) { this.cookieStore = new BasicCookieStore(); } log.info("Device ID is: " + this.deviceId + ", random id: " + this.uuid); this.client = new DefaultHttpClient(); this.client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY); this.client.setCookieStore(this.cookieStore); }
/** * Initializes an http communication session with the application. */ public ApplicationSession authenticate(Application application) { String uri = application.makeHostnameUri() + "/" + DbFreezeRest.POST_LOGIN; Executor httpExecutor = executorFactory.makeExecutor(); CookieStore cookieStore = new BasicCookieStore(); httpExecutor.cookieStore(cookieStore); NameValuePair[] authParams = new NameValuePair[] { new BasicNameValuePair(PARAMNAME_AUTHUSERNAME, applicationUsername), new BasicNameValuePair(PARAMNAME_AUTHPASSWORD, applicationPassword) }; httpHelper.postAuthForCookie(httpExecutor, uri, authParams); return new ApplicationSession(httpExecutor, cookieStore); }
/** * 构造方法,获得Cookie并登录 * @param username 账号 * @param password 密码 */ public Crawler(String username, String password) { this.username = username; this.password = password; this.cookieStore = new BasicCookieStore(); this.httpclient = HttpClients.custom().setDefaultCookieStore(cookieStore).build(); try { //获取cookie HttpGet getCookie = new HttpGet(educationalUrl); CloseableHttpResponse response1 = httpclient.execute(getCookie); response1.close(); //登录 HttpUriRequest postLogin = RequestBuilder.post().setUri(new URI(loginUrl)) .addParameter("utype", "S") .addParameter("ucode", this.username) .addParameter("pwd", this.password) .addParameter("rember", "true") .build(); CloseableHttpResponse response2 = httpclient.execute(postLogin); response2.close(); }catch (URISyntaxException | IOException e){ System.out.println("登录失败"); System.exit(1); } }
@Bean public RestTemplate restTemplate() { CookieStore cookieStore = new BasicCookieStore(); HttpContext httpContext = new BasicHttpContext(); httpContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore); httpContext.setAttribute(HttpClientContext.REQUEST_CONFIG, RequestConfig.custom().setRedirectsEnabled(false).build()); return new StatefullRestTemplate(httpContext); }
/** * 根据配置自动生成需要的Cookies * @param httpClient * @param setting */ public void generateCookies(HttpClientBuilder httpClient, Setting setting){ CookieStore cookieStore = new BasicCookieStore(); for(Map.Entry<String,String> cookieEntry : setting.getCookies().entrySet()){ BasicClientCookie cookie = new BasicClientCookie(cookieEntry.getKey(), cookieEntry.getValue()); cookie.setDomain(setting.getDomain()); cookie.setPath("/"); cookieStore.addCookie(cookie); httpClient.setDefaultCookieStore(cookieStore).build(); } }
/** * Get a cookie from Google and place it in a ClosableHttpClient * @return An instance of CloseableHttpClient that is critical to this Class * @throws IOException When URL connection is improperly formed */ private static CloseableHttpClient getCookieHttpClient() throws IOException { /* * Tutorial: http://www.hccp.org/java-net-cookie-how-to.html */ URL myUrl = new URL("https://trends.google.com"); URLConnection urlConn = myUrl.openConnection(); urlConn.connect(); String headerName = null; for(int i=1; (headerName = urlConn.getHeaderFieldKey(i)) != null; i++) if(headerName.equals("Set-Cookie")) cookieString = urlConn.getHeaderField(i); cookieString = cookieString.substring(0, cookieString.indexOf(";")); /* * Tutorial: http://hc.apache.org/httpcomponents-client-ga/tutorial/html/statemgmt.html#d5e499 */ CookieStore cookieStore = new BasicCookieStore(); BasicClientCookie cookie = new BasicClientCookie("Cookie", cookieString); cookie.setDomain(".google.com"); cookie.setPath("/trends"); cookieStore.addCookie(cookie); return HttpClients.custom().setDefaultCookieStore(cookieStore).build(); }
/** Creates a new fetching thread. * * @param frontier a reference to the {@link Frontier}. * @param index the index of this thread (only for logging purposes). */ public FetchingThread(final Frontier frontier, final int index) throws NoSuchAlgorithmException, IllegalArgumentException, IOException { setName(this.getClass().getSimpleName() + '-' + index); setPriority(Thread.MIN_PRIORITY); // Low priority; there will be thousands of this guys around. this.frontier = frontier; final BasicHttpClientConnectionManager connManager = new BasicHttpClientConnectionManagerWithAlternateDNS(frontier.rc.dnsResolver); connManager.closeIdleConnections(0, TimeUnit.MILLISECONDS); connManager.setConnectionConfig(ConnectionConfig.custom().setBufferSize(8 * 1024).build()); // TODO: make this configurable cookieStore = new BasicCookieStore(); BasicHeader[] headers = { new BasicHeader("From", frontier.rc.userAgentFrom), new BasicHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.95,text/*;q=0.9,*/*;q=0.8") }; httpClient = HttpClients.custom() .setSSLContext(frontier.rc.acceptAllCertificates ? TRUST_ALL_CERTIFICATES_SSL_CONTEXT : TRUST_SELF_SIGNED_SSL_CONTEXT) .setConnectionManager(connManager) .setConnectionReuseStrategy(frontier.rc.keepAliveTime == 0 ? NoConnectionReuseStrategy.INSTANCE : DefaultConnectionReuseStrategy.INSTANCE) .setUserAgent(frontier.rc.userAgent) .setDefaultCookieStore(cookieStore) .setDefaultHeaders(ObjectArrayList.wrap(headers)) .build(); fetchData = new FetchData(frontier.rc); }
public YouTrackClient(String host, String keyStoreType, String keyStorePath, String keyStorePassword, String keyPassword, String trustStoreType, String trustStorePath, String trustStorePassword) { this.host = host; CookieStore cookieStore = new BasicCookieStore(); RequestConfig requestConfig = RequestConfig.custom() .setSocketTimeout(SOCKET_TIMEOUT_MS) .setConnectTimeout(CONNECTION_TIMEOUT_MS) .setConnectionRequestTimeout(CONNECTION_TIMEOUT_MS) .setCookieSpec(CookieSpecs.STANDARD) .build(); LayeredConnectionSocketFactory sslFactory; RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", new PlainConnectionSocketFactory()); if (host.contains("https://")) { try { sslFactory = initSslContext(keyStoreType, keyStorePath, keyStorePassword, keyPassword, trustStoreType, trustStorePath, trustStorePassword); registryBuilder.register("https", sslFactory); } catch (Exception e) { throw new IllegalStateException(e); } } Registry<ConnectionSocketFactory> registry = registryBuilder.build(); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry); connectionManager.setMaxTotal(MAX_SIMULTANEOUS_CONNECTIONS); connectionManager.setDefaultMaxPerRoute(MAX_SIMULTANEOUS_CONNECTIONS); client = HttpClients.custom() .setConnectionManager(connectionManager) .setDefaultRequestConfig(requestConfig) .setDefaultCookieStore(cookieStore) .build(); }