@Override public void afterPropertiesSet() throws Exception { cookieSpecRegistry = RegistryBuilder.<CookieSpecProvider> create().register("easy", new CookieSpecProvider() { public CookieSpec create(HttpContext context) { return new DefaultCookieSpec() { @Override public void validate(Cookie cookie, CookieOrigin origin) throws MalformedCookieException { } }; } }).build(); requestConfig = RequestConfig.custom().setCookieSpec("easy") .setConnectionRequestTimeout(propertyConfigurer.getIntValue("connection.request.timeout")) .setSocketTimeout(propertyConfigurer.getIntValue("socket_timeout")) .setConnectTimeout(propertyConfigurer.getIntValue("connection_timeout")).build(); }
public InternalHttpClient( final ClientExecChain execChain, final HttpClientConnectionManager connManager, final HttpRoutePlanner routePlanner, final Lookup<CookieSpecProvider> cookieSpecRegistry, final Lookup<AuthSchemeProvider> authSchemeRegistry, final CookieStore cookieStore, final CredentialsProvider credentialsProvider, final RequestConfig defaultConfig, final List<Closeable> closeables) { super(); Args.notNull(execChain, "HTTP client exec chain"); Args.notNull(connManager, "HTTP connection manager"); Args.notNull(routePlanner, "HTTP route planner"); this.execChain = execChain; this.connManager = connManager; this.routePlanner = routePlanner; this.cookieSpecRegistry = cookieSpecRegistry; this.authSchemeRegistry = authSchemeRegistry; this.cookieStore = cookieStore; this.credentialsProvider = credentialsProvider; this.defaultConfig = defaultConfig; this.closeables = closeables; }
/** * Creates a builder containing the default registry entries, using the provided public suffix matcher. */ public static RegistryBuilder<CookieSpecProvider> createDefaultBuilder(final PublicSuffixMatcher publicSuffixMatcher) { final CookieSpecProvider defaultProvider = new DefaultCookieSpecProvider(publicSuffixMatcher); final CookieSpecProvider laxStandardProvider = new RFC6265CookieSpecProvider( RFC6265CookieSpecProvider.CompatibilityLevel.RELAXED, publicSuffixMatcher); final CookieSpecProvider strictStandardProvider = new RFC6265CookieSpecProvider( RFC6265CookieSpecProvider.CompatibilityLevel.STRICT, publicSuffixMatcher); return RegistryBuilder.<CookieSpecProvider>create() .register(CookieSpecs.DEFAULT, defaultProvider) .register("best-match", defaultProvider) .register("compatibility", defaultProvider) .register(CookieSpecs.STANDARD, laxStandardProvider) .register(CookieSpecs.STANDARD_STRICT, strictStandardProvider) .register(CookieSpecs.NETSCAPE, new NetscapeDraftSpecProvider()) .register(CookieSpecs.IGNORE_COOKIES, new IgnoreSpecProvider()); }
@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(); }
@SuppressWarnings("unchecked") @Test public void testExecuteLocalContext() throws Exception { final HttpGet httpget = new HttpGet("http://somehost/stuff"); final HttpClientContext context = HttpClientContext.create(); final Lookup<CookieSpecProvider> localCookieSpecRegistry = Mockito.mock(Lookup.class); final Lookup<AuthSchemeProvider> localAuthSchemeRegistry = Mockito.mock(Lookup.class); final CookieStore localCookieStore = Mockito.mock(CookieStore.class); final CredentialsProvider localCredentialsProvider = Mockito.mock(CredentialsProvider.class); final RequestConfig localConfig = RequestConfig.custom().build(); context.setCookieSpecRegistry(localCookieSpecRegistry); context.setAuthSchemeRegistry(localAuthSchemeRegistry); context.setCookieStore(localCookieStore); context.setCredentialsProvider(localCredentialsProvider); context.setRequestConfig(localConfig); client.execute(httpget, context); Assert.assertSame(localCookieSpecRegistry, context.getCookieSpecRegistry()); Assert.assertSame(localAuthSchemeRegistry, context.getAuthSchemeRegistry()); Assert.assertSame(localCookieStore, context.getCookieStore()); Assert.assertSame(localCredentialsProvider, context.getCredentialsProvider()); Assert.assertSame(localConfig, context.getRequestConfig()); }
protected Lookup<CookieSpecProvider> createCookieSpecRegistry() { CookieSpecProvider cookieSpecProvider = createCookieSpecProvider(); Registry<CookieSpecProvider> cookieSpecRegistry = RegistryBuilder.<CookieSpecProvider>create() .register(NoopCookieSpecProvider.NAME, cookieSpecProvider) .build(); return cookieSpecRegistry; }
public void setCookieStore(String path, String md5) throws ClassNotFoundException, IOException { context = HttpClientContext.create(); Registry<CookieSpecProvider> registry = RegistryBuilder .<CookieSpecProvider> create() .register(CookieSpecs.DEFAULT, new DefaultCookieSpecProvider()).build(); context.setCookieSpecRegistry(registry); context.setCookieStore(loadCookies(path, md5)); }
public static Executor createExecutor(CookieStore cookieStore) { @SuppressWarnings("deprecation") final Registry<CookieSpecProvider> cookieSpecRegistry = RegistryBuilder.<CookieSpecProvider>create().register(CookieSpecs.DEFAULT, new BrowserCompatSpecFactory()).build(); final CloseableHttpClient client = HttpClients.custom() .setDefaultCookieSpecRegistry(cookieSpecRegistry) .setDefaultCookieStore(cookieStore) .build(); return Executor.newInstance(client); }
public static HttpClientContext getHttpClientContext() { HttpClientContext context = null; context = HttpClientContext.create(); Registry<CookieSpecProvider> registry = RegistryBuilder .<CookieSpecProvider>create() .register(CookieSpecs.BEST_MATCH, new BestMatchSpecFactory()) .register(CookieSpecs.BROWSER_COMPATIBILITY, new BrowserCompatSpecFactory()).build(); context.setCookieSpecRegistry(registry); return context; }
protected CookieSpecProvider createCookieSpecProvider() { return new NoopCookieSpecProvider(); }
public static Registry<CookieSpecProvider> registry() { CookieSpecProvider specProvider = (HttpContext hc) -> new LenientCookieSpec(); return RegistryBuilder.<CookieSpecProvider>create() .register(KARATE, specProvider).build(); }
public Lookup<CookieSpecProvider> getCookieSpecRegistry() { return getLookup(COOKIESPEC_REGISTRY, CookieSpecProvider.class); }
public void setCookieSpecRegistry(final Lookup<CookieSpecProvider> lookup) { setAttribute(COOKIESPEC_REGISTRY, lookup); }
/** * Assigns default {@link org.apache.http.cookie.CookieSpec} registry which will * be used for request execution if not explicitly set in the client execution * context. */ public final HttpClientBuilder setDefaultCookieSpecRegistry( final Lookup<CookieSpecProvider> cookieSpecRegistry) { this.cookieSpecRegistry = cookieSpecRegistry; return this; }
public final static void main(String[] args) throws Exception { // Use PublicSuffixMatcherLoader to load public suffix list from a file, // resource or from an arbitrary URL PublicSuffixMatcher publicSuffixMatcher = PublicSuffixMatcherLoader.load( new URL("https://publicsuffix.org/list/effective_tld_names.dat")); // Please use the publicsuffix.org URL to download the list no more than once per day !!! // Please consider making a local copy !!! DefaultHostnameVerifier hostnameVerifier = new DefaultHostnameVerifier(publicSuffixMatcher); RFC6265CookieSpecProvider cookieSpecProvider = new RFC6265CookieSpecProvider(publicSuffixMatcher); Lookup<CookieSpecProvider> cookieSpecRegistry = RegistryBuilder.<CookieSpecProvider>create() .register(CookieSpecs.DEFAULT, cookieSpecProvider) .register(CookieSpecs.STANDARD, cookieSpecProvider) .register(CookieSpecs.STANDARD_STRICT, cookieSpecProvider) .build(); CloseableHttpClient httpclient = HttpClients.custom() .setSSLHostnameVerifier(hostnameVerifier) .setDefaultCookieSpecRegistry(cookieSpecRegistry) .build(); try { HttpGet httpget = new HttpGet("https://httpbin.org/"); System.out.println("executing request " + httpget.getRequestLine()); CloseableHttpResponse response = httpclient.execute(httpget); try { HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); EntityUtils.consume(entity); } finally { response.close(); } } finally { httpclient.close(); } }
/** * Creates a builder containing the default registry entries with the default public suffix matcher. */ public static RegistryBuilder<CookieSpecProvider> createDefaultBuilder() { return createDefaultBuilder(PublicSuffixMatcherLoader.getDefault()); }
/** * Creates the default registry, using the default public suffix matcher. */ public static Lookup<CookieSpecProvider> createDefault() { return createDefault(PublicSuffixMatcherLoader.getDefault()); }
/** * Creates the default registry with the provided public suffix matcher */ public static Lookup<CookieSpecProvider> createDefault(final PublicSuffixMatcher publicSuffixMatcher) { return createDefaultBuilder(publicSuffixMatcher).build(); }
public final static void main(String[] args) throws Exception { // Use PublicSuffixMatcherLoader to load public suffix list from a file, // resource or from an arbitrary URL PublicSuffixMatcher publicSuffixMatcher = PublicSuffixMatcherLoader.load( new URL("https://publicsuffix.org/list/effective_tld_names.dat")); // Please use the publicsuffix.org URL to download the list no more than once per day !!! // Please consider making a local copy !!! DefaultHostnameVerifier hostnameVerifier = new DefaultHostnameVerifier(publicSuffixMatcher); RFC6265CookieSpecProvider cookieSpecProvider = new RFC6265CookieSpecProvider(publicSuffixMatcher); Lookup<CookieSpecProvider> cookieSpecRegistry = RegistryBuilder.<CookieSpecProvider>create() .register(CookieSpecs.DEFAULT, cookieSpecProvider) .register(CookieSpecs.STANDARD, cookieSpecProvider) .register(CookieSpecs.STANDARD_STRICT, cookieSpecProvider) .build(); CloseableHttpClient httpclient = HttpClients.custom() .setSSLHostnameVerifier(hostnameVerifier) .setDefaultCookieSpecRegistry(cookieSpecRegistry) .build(); try { HttpGet httpget = new HttpGet("https://remotehost/"); System.out.println("executing request " + httpget.getRequestLine()); CloseableHttpResponse response = httpclient.execute(httpget); try { HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); EntityUtils.consume(entity); } finally { response.close(); } } finally { httpclient.close(); } }
/** * Assigns default {@link org.apache.http.cookie.CookieSpec} registry which will * be used for request execution if not explicitly set in the client execution * context. * * @see org.apache.http.impl.client.CookieSpecRegistries * */ public final HttpClientBuilder setDefaultCookieSpecRegistry( final Lookup<CookieSpecProvider> cookieSpecRegistry) { this.cookieSpecRegistry = cookieSpecRegistry; return this; }