Example usage for com.squareup.okhttp OkHttpClient setCookieHandler

List of usage examples for com.squareup.okhttp OkHttpClient setCookieHandler

Introduction

In this page you can find the example usage for com.squareup.okhttp OkHttpClient setCookieHandler.

Prototype

public OkHttpClient setCookieHandler(CookieHandler cookieHandler) 

Source Link

Document

Sets the cookie handler to be used to read outgoing cookies and write incoming cookies.

Usage

From source file:apijson.demo.client.manager.HttpManager.java

License:Apache License

/**
 * @param url/*from ww  w. j  a va  2 s .c o m*/
 * @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);

    return client;
}

From source file:cl.magnet.magnetrestclient.VolleyManager.java

License:Open Source License

/**
 * Returns the singleton instance of RequestQueue that last the lifetime of the app. If there
 * is no instance of RequestQueue, then a new one is created.
 * <p/>/*from  w ww .j  a va 2s. com*/
 * The created RequestQueue uses {@link cl.magnet.magnetrestclient.OkHttpStack OkHttpStack}
 * for networking.
 *
 * @return RequestQueue instance
 */
private RequestQueue getRequestQueue() {
    if (mRequestQueue == null) {
        // getApplicationContext() keeps from leaking Activity or BroadcastReceiver if
        // someone pass one in
        OkHttpClient client = new OkHttpClient();
        client.setCookieHandler(CookieHandler.getDefault());
        mRequestQueue = Volley.newRequestQueue(mContext.getApplicationContext(), new OkHttpStack(client));
    }

    return mRequestQueue;
}

From source file:cn.finalteam.okhttpfinal.OkHttpFactory.java

License:Apache License

public static OkHttpClient getOkHttpClientFactory(long timeout) {
    OkHttpClient client = new OkHttpClient();
    ///*from   w w w.j  a  v  a  2  s  .c om*/
    client.setConnectTimeout(timeout, TimeUnit.MILLISECONDS);
    client.setWriteTimeout(timeout, TimeUnit.MILLISECONDS);
    client.setReadTimeout(timeout, TimeUnit.MILLISECONDS);
    //???
    client.setRetryOnConnectionFailure(false);
    //???
    client.setFollowRedirects(true);
    //?cookie
    client.setCookieHandler(new CookieManager(null, CookiePolicy.ACCEPT_ORIGINAL_SERVER));

    return client;
}

From source file:com.ibm.watson.developer_cloud.service.WatsonService.java

License:Open Source License

/**
 * Configures the HTTP client.//from  ww w  .ja  va2  s  . co m
 * 
 * @return the HTTP client
 */
protected OkHttpClient configureHttpClient() {
    final OkHttpClient client = new OkHttpClient();
    final CookieManager cookieManager = new CookieManager();
    cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
    client.setCookieHandler(cookieManager);

    client.setConnectTimeout(60, TimeUnit.SECONDS);
    client.setWriteTimeout(60, TimeUnit.SECONDS);
    client.setReadTimeout(90, TimeUnit.SECONDS);
    return client;
}

From source file:com.liferay.mobile.android.auth.CookieSignIn.java

License:Open Source License

protected Call signIn() throws Exception {
    if (!(session.getAuthentication() instanceof CookieAuthentication)) {
        throw new Exception("Can't sign in if authentication implementation is not " + "CookieAuthentication");
    }/*  w  w w  .j  av  a 2s .c  om*/

    CookieAuthentication cookieAuthentication = getCookieAuthentication(session.getAuthentication());

    username = cookieAuthentication.getUsername();
    password = cookieAuthentication.getPassword();

    cookieManager = new CookieManager();
    cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);

    OkHttpClient client = new OkHttpClient();

    Authenticator authenticator = authenticators.get(session.getServer());

    if (authenticator != null) {
        client.setAuthenticator(authenticator);
    }

    client.setCookieHandler(cookieManager);
    client.setFollowRedirects(true);

    Builder builder = getBuilder(session, username, password);

    return client.newCall(builder.build());
}

From source file:com.liferay.mobile.sdk.auth.CookieSignIn.java

License:Open Source License

public static void signIn(Config config, CookieCallback callback) {
    try {/*from ww  w.j  a  v  a  2s .  c  om*/
        Authentication auth = config.auth();

        if (!(auth instanceof BasicAuthentication)) {
            throw new Exception(
                    "Can't sign in if authentication implementation is not " + "BasicAuthentication");
        }

        OkHttpClient client = new OkHttpClient();

        CookieManager cookieManager = new CookieManager();

        cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);

        client.setCookieHandler(cookieManager);
        client.setFollowRedirects(true);

        Builder builder = new Builder();

        MediaType contentType = MediaType.parse("application/x-www-form-urlencoded");

        builder.post(RequestBody.create(contentType, getBody((BasicAuthentication) auth)));

        builder.addHeader("Cookie", "COOKIE_SUPPORT=true;");
        builder.url(getLoginURL(config.server()));

        Call call = client.newCall(builder.build());

        call.enqueue(getCallback(callback, cookieManager));
    } catch (Exception e) {
        callback.onFailure(e);
    }
}

From source file:com.nbonnec.mediaseb.di.modules.ApiModule.java

License:Apache License

private static OkHttpClient createOkHttpClient(Application app) {
    OkHttpClient client = new OkHttpClient();
    CookieManager cookieManager = new CookieManager();

    cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
    client.setCookieHandler(cookieManager);
    client.networkInterceptors().add(new UserAgentInterceptor(USER_AGENT));

    try {/*from  w  w  w  . j a  v a 2  s  . co m*/
        File cacheDir = new File(app.getCacheDir(), "http");
        Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE);
        client.setCache(cache);
    } catch (NullPointerException e) {
        Log.e(TAG, "Unable to initialize OkHttpclient with disk cache", e);
    }

    return client;
}

From source file:com.nguyenmp.gauchospace.GauchoSpaceClient.java

License:Open Source License

public static OkHttpClient getClient(Session session) {
    OkHttpClient client = new OkHttpClient();
    CookieManager cookieManager = new CookieManager(session != null ? session.getCookies() : null,
            CookiePolicy.ACCEPT_ALL);
    client.setCookieHandler(cookieManager);
    return client;
}

From source file:com.tinspx.util.net.okhttp.OkHttpContext.java

License:Open Source License

private OkHttpContext(@NonNull OkHttpClient client, boolean debug) {
    //todo: always set no follow redirects, rather should the user be allowed
    //to set this? this will effect the request creation/submisstion
    //document info about the cookie handler
    client = client.clone();/*from ww  w .ja v  a  2  s.  c o m*/
    client.setFollowRedirects(false);
    if (client.getCookieHandler() == null) {
        client.setCookieHandler(new CookieManager());
    }
    this.client = client;
    this.debug = debug;
}

From source file:com.urswolfer.gerrit.client.rest.http.GerritRestClient.java

License:Apache License

private HttpClientBuilder getHttpClient(HttpContext httpContext) {
    HttpClientBuilder client = HttpClients.custom();

    client.useSystemProperties(); // see also: com.intellij.util.net.ssl.CertificateManager

    OkHttpClient c = new OkHttpClient();
    c.setFollowRedirects(true);/*  w ww. ja  v  a 2 s.  c om*/
    // we need to get redirected result after login (which is done with POST) for extracting xGerritAuth
    client.setRedirectStrategy(new LaxRedirectStrategy());

    c.setCookieHandler(cookieManager);

    c.setConnectTimeout(CONNECTION_TIMEOUT_MS, TimeUnit.MILLISECONDS);
    c.setReadTimeout(CONNECTION_TIMEOUT_MS, TimeUnit.MILLISECONDS);
    c.setWriteTimeout(CONNECTION_TIMEOUT_MS, TimeUnit.MILLISECONDS);

    CredentialsProvider credentialsProvider = getCredentialsProvider();
    client.setDefaultCredentialsProvider(credentialsProvider);

    if (authData.isLoginAndPasswordAvailable()) {
        credentialsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials(authData.getLogin(), authData.getPassword()));

        BasicScheme basicAuth = new BasicScheme();
        httpContext.setAttribute(PREEMPTIVE_AUTH, basicAuth);
        client.addInterceptorFirst(new PreemptiveAuthHttpRequestInterceptor(authData));
    }

    client.addInterceptorLast(new UserAgentHttpRequestInterceptor());

    for (HttpClientBuilderExtension httpClientBuilderExtension : httpClientBuilderExtensions) {
        client = httpClientBuilderExtension.extend(client, authData);
        credentialsProvider = httpClientBuilderExtension.extendCredentialProvider(client, credentialsProvider,
                authData);
    }

    return client;
}