List of usage examples for com.squareup.okhttp OkHttpClient setCookieHandler
public OkHttpClient setCookieHandler(CookieHandler cookieHandler)
From source file:com.ushahidi.android.presentation.di.modules.AppModule.java
License:Open Source License
/** * Provides {@link HttpClientWrap} object * * @param context The calling context/* w ww.j a v a 2 s.c o m*/ * @return The http client wrap object */ @Provides @Singleton HttpClientWrap provideOkHttpClient(Context context) { final OkHttpClient okHttpClient = createOkHttpClient(context.getApplicationContext()); okHttpClient.setCookieHandler(CookieHandler.getDefault()); okHttpClient.setConnectTimeout(10, TimeUnit.SECONDS); okHttpClient.setReadTimeout(10, TimeUnit.SECONDS); okHttpClient.setWriteTimeout(10, TimeUnit.SECONDS); return new HttpClientWrap(context, new OkClient(okHttpClient)); }
From source file:io.apiman.gateway.platforms.servlet.connectors.HttpConnectorFactory.java
License:Apache License
/** * @return a new http client/*from w w w. jav a 2s . c om*/ */ private OkHttpClient createHttpClient() { OkHttpClient client = new OkHttpClient(); client.setReadTimeout(connectorOptions.getReadTimeout(), TimeUnit.SECONDS); client.setWriteTimeout(connectorOptions.getWriteTimeout(), TimeUnit.SECONDS); client.setConnectTimeout(connectorOptions.getConnectTimeout(), TimeUnit.SECONDS); client.setFollowRedirects(connectorOptions.isFollowRedirects()); client.setFollowSslRedirects(connectorOptions.isFollowRedirects()); client.setProxySelector(ProxySelector.getDefault()); client.setCookieHandler(CookieHandler.getDefault()); client.setCertificatePinner(CertificatePinner.DEFAULT); client.setAuthenticator(AuthenticatorAdapter.INSTANCE); client.setConnectionPool(ConnectionPool.getDefault()); client.setProtocols(Util.immutableList(Protocol.HTTP_1_1)); client.setConnectionSpecs(DEFAULT_CONNECTION_SPECS); client.setSocketFactory(SocketFactory.getDefault()); Internal.instance.setNetwork(client, Network.DEFAULT); return client; }
From source file:keywhiz.cli.ClientUtils.java
License:Apache License
/** * Creates a {@link OkHttpClient} to start a TLS connection. * * @param devTrustStore if not null, uses the provided TrustStore instead of whatever is * configured in the JVM. This is a convenient way to allow developers to * start playing with Keywhiz right away. This option should not be used in * production systems. * @param cookies list of cookies to include in the client. * @return new http client.//w ww. j ava2 s . c om */ public static OkHttpClient sslOkHttpClient(@Nullable KeyStore devTrustStore, List<HttpCookie> cookies) { checkNotNull(cookies); SSLContext sslContext; try { TrustManagerFactory trustManagerFactory = TrustManagerFactory .getInstance(TrustManagerFactory.getDefaultAlgorithm()); trustManagerFactory.init(devTrustStore); TrustManager[] trustManagers = trustManagerFactory.getTrustManagers(); sslContext = SSLContext.getInstance("TLSv1.2"); sslContext.init(new KeyManager[0], trustManagers, 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:org.jboss.arquillian.ce.utils.OkHttpClientUtils.java
License:Open Source License
public static void applyCookieHandler(OkHttpClient httpClient) { COOKIE_HANDLER.clear(); // reset httpClient.setCookieHandler(COOKIE_HANDLER); }
From source file:org.mariotaku.twidere.util.OAuthPasswordAuthenticator.java
License:Open Source License
public OAuthPasswordAuthenticator(final TwitterOAuth oauth, final LoginVerificationCallback loginVerificationCallback, final String userAgent) { final RestClient restClient = RestAPIFactory.getRestClient(oauth); this.oauth = oauth; this.client = (OkHttpRestClient) restClient.getRestClient(); final OkHttpClient okhttp = client.getClient(); okhttp.setCookieHandler(new CookieManager()); okhttp.networkInterceptors().add(new Interceptor() { @Override/*from ww w. jav a2 s .c om*/ public Response intercept(Chain chain) throws IOException { final Response response = chain.proceed(chain.request()); if (!response.isRedirect()) { return response; } final String location = response.header("Location"); final Response.Builder builder = response.newBuilder(); if (!TextUtils.isEmpty(location) && !endpoint.checkEndpoint(location)) { final HttpUrl originalLocation = HttpUrl .get(URI.create("https://api.twitter.com/").resolve(location)); final HttpUrl.Builder locationBuilder = HttpUrl.parse(endpoint.getUrl()).newBuilder(); for (String pathSegments : originalLocation.pathSegments()) { locationBuilder.addPathSegment(pathSegments); } for (int i = 0, j = originalLocation.querySize(); i < j; i++) { final String name = originalLocation.queryParameterName(i); final String value = originalLocation.queryParameterValue(i); locationBuilder.addQueryParameter(name, value); } final String encodedFragment = originalLocation.encodedFragment(); if (encodedFragment != null) { locationBuilder.encodedFragment(encodedFragment); } final HttpUrl newLocation = locationBuilder.build(); builder.header("Location", newLocation.toString()); } return builder.build(); } }); this.endpoint = restClient.getEndpoint(); this.loginVerificationCallback = loginVerificationCallback; this.userAgent = userAgent; }
From source file:tk.breezy64.pantex.core.Util.java
private static OkHttpClient getClient(String session) { OkHttpClient client;//from ww w.j a v a 2 s . c o m if (session == null) { session = DEFAULT_SESSION; } if (!SESSIONS.containsKey(session)) { OkHttpClient c = new OkHttpClient(); if (session.equals(DEFAULT_SESSION)) { CookieManager cookieManager = new CookieManager(); cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); c.setCookieHandler(cookieManager); } SESSIONS.put(session, c); } client = SESSIONS.get(session); return client; }
From source file:zblibrary.demo.manager.HttpRequest.java
License:Apache License
/** * @param url/*from w w w .j a va 2 s . com*/ * @return */ private OkHttpClient getHttpClient(String url) { Log.i(TAG, "getHttpClient url = " + url); if (StringUtil.isNotEmpty(url, true) == false) { Log.e(TAG, "getHttpClient StringUtil.isNotEmpty(url, true) == false >> return null;"); return null; } OkHttpClient client = new OkHttpClient(); client.setCookieHandler(new HttpHead()); client.setConnectTimeout(15, TimeUnit.SECONDS); client.setWriteTimeout(10, TimeUnit.SECONDS); client.setReadTimeout(10, TimeUnit.SECONDS); //https?,??,??? if (url.startsWith(StringUtil.URL_PREFIXs) && socketFactory != null) { client.setSslSocketFactory(socketFactory); } return client; }