Search in sources :

Example 31 with CookieJar

use of okhttp3.CookieJar in project okhttp by square.

the class URLConnectionTest method connectViaHttpsReusingConnections.

private void connectViaHttpsReusingConnections(boolean rebuildClient) throws Exception {
    server.useHttps(sslClient.socketFactory, false);
    server.enqueue(new MockResponse().setBody("this response comes via HTTPS"));
    server.enqueue(new MockResponse().setBody("another response via HTTPS"));
    // The pool will only reuse sockets if the SSL socket factories are the same.
    SSLSocketFactory clientSocketFactory = sslClient.socketFactory;
    RecordingHostnameVerifier hostnameVerifier = new RecordingHostnameVerifier();
    CookieJar cookieJar = new JavaNetCookieJar(new CookieManager());
    ConnectionPool connectionPool = new ConnectionPool();
    urlFactory.setClient(new OkHttpClient.Builder().cache(cache).connectionPool(connectionPool).cookieJar(cookieJar).sslSocketFactory(clientSocketFactory, sslClient.trustManager).hostnameVerifier(hostnameVerifier).build());
    connection = urlFactory.open(server.url("/").url());
    assertContent("this response comes via HTTPS", connection);
    if (rebuildClient) {
        urlFactory.setClient(new OkHttpClient.Builder().cache(cache).connectionPool(connectionPool).cookieJar(cookieJar).sslSocketFactory(clientSocketFactory, sslClient.trustManager).hostnameVerifier(hostnameVerifier).build());
    }
    connection = urlFactory.open(server.url("/").url());
    assertContent("another response via HTTPS", connection);
    assertEquals(0, server.takeRequest().getSequenceNumber());
    assertEquals(1, server.takeRequest().getSequenceNumber());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) CookieManager(java.net.CookieManager)

Example 32 with CookieJar

use of okhttp3.CookieJar in project 2017-01-HUDI-MAC-CHAR by NHNNEXT.

the class ServiceGenerator method createService.

public static <S> S createService(Class<S> serviceClass, Context context) {
    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    if (BuildConfig.DEBUG) {
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
    } else {
        logging.setLevel(HttpLoggingInterceptor.Level.NONE);
    }
    PersistentCookieStore cookieStore = new PersistentCookieStore(context);
    java.net.CookieManager cookieManager = new java.net.CookieManager(cookieStore, CookiePolicy.ACCEPT_ALL);
    OkHttpClient.Builder httpClient = new OkHttpClient.Builder().cookieJar(new JavaNetCookieJar(cookieManager)).addInterceptor(logging).addNetworkInterceptor(new StethoInterceptor());
    Gson gson = new GsonBuilder().setLenient().create();
    Retrofit retorfit = new Retrofit.Builder().baseUrl(MafiaRemoteService.BASE_URL).addConverterFactory(GsonConverterFactory.create(gson)).client(httpClient.build()).build();
    return retorfit.create(serviceClass);
}
Also used : PersistentCookieStore(com.zimincom.mafiaonline.PersistentCookieStore) JavaNetCookieJar(okhttp3.internal.JavaNetCookieJar) OkHttpClient(okhttp3.OkHttpClient) GsonBuilder(com.google.gson.GsonBuilder) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) Retrofit(retrofit2.Retrofit) StethoInterceptor(com.facebook.stetho.okhttp3.StethoInterceptor) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor)

Example 33 with CookieJar

use of okhttp3.CookieJar in project WeexErosFramework by bmfe.

the class AxiosManager method createClient.

public OkHttpClient createClient(Context context, long timeout) {
    CookieJarImpl cookieJar = new CookieJarImpl(new BMPersistentCookieStore(context));
    OkHttpClient.Builder builder = new OkHttpClient.Builder().addInterceptor(new LoggerInterceptor("TAG")).connectTimeout(timeout == 0 ? 3000L : timeout, TimeUnit.MILLISECONDS).readTimeout(timeout == 0 ? 30000L : timeout, TimeUnit.MILLISECONDS).cookieJar(cookieJar);
    if (DebugableUtil.isDebug()) {
        builder.addNetworkInterceptor(new WeexOkhttp3Interceptor());
    }
    return builder.build();
}
Also used : BMPersistentCookieStore(com.benmu.framework.http.BMPersistentCookieStore) OkHttpClient(okhttp3.OkHttpClient) LoggerInterceptor(com.benmu.framework.http.okhttp.log.LoggerInterceptor) WeexOkhttp3Interceptor(com.benmu.framework.extend.adapter.WeexOkhttp3Interceptor) CookieJarImpl(com.benmu.framework.http.okhttp.cookie.CookieJarImpl) OkHttpRequestBuilder(com.benmu.framework.http.okhttp.builder.OkHttpRequestBuilder) OtherRequestBuilder(com.benmu.framework.http.okhttp.builder.OtherRequestBuilder) GetBuilder(com.benmu.framework.http.okhttp.builder.GetBuilder) PostFormBuilder(com.benmu.framework.http.okhttp.builder.PostFormBuilder)

Example 34 with CookieJar

use of okhttp3.CookieJar in project MiMangaNu by raulhaag.

the class BatoTo method testLogin.

// just log in batoto to store the cookie if all ok
@Override
public boolean testLogin(String user, String password) throws Exception {
    Navigator nav = getNavigatorAndFlushParameters();
    CookieJar ccj = nav.getHttpClient().cookieJar();
    VolatileCookieJar cj = new VolatileCookieJar();
    nav.setCookieJar(cj);
    String data = nav.get(HOST + "/forums/index.php?app=core&amp;module=global&amp;section=login");
    HashMap<String, String> params = Navigator.getFormParamsFromSource(data);
    nav = getNavigatorAndFlushParameters();
    nav.addPost("auth_key", params.get("auth_key"));
    nav.addPost("ips_password", password);
    nav.addPost("ips_username", user);
    nav.addPost("referer", "https://bato.to/forums/");
    nav.addPost("rememberMe", "1");
    nav.post(HOST + "/forums/index.php?app=core&module=global&section=login&do=process");
    List<Cookie> cookies = Navigator.getCookieJar().loadForRequest(HttpUrl.parse("https://bato.to"));
    nav.setCookieJar(ccj);
    return cj.contain("member_id");
}
Also used : Cookie(okhttp3.Cookie) Navigator(ar.rulosoft.navegadores.Navigator) VolatileCookieJar(ar.rulosoft.navegadores.VolatileCookieJar) CookieJar(okhttp3.CookieJar) VolatileCookieJar(ar.rulosoft.navegadores.VolatileCookieJar)

Example 35 with CookieJar

use of okhttp3.CookieJar in project GzuClassSchedule by mnnyang.

the class app method initOkHttp.

private void initOkHttp() {
    OkHttpClient okHttpClient = new OkHttpClient.Builder().followSslRedirects(false).cookieJar(// 为OkHttp设置自动携带Cookie的功能
    new LocalCookieJar()).addInterceptor(new LoggerInterceptor("TAG")).cookieJar(// 要在内存Cookie前
    new CookieJarImpl(new PersistentCookieStore(getBaseContext()))).cookieJar(// 内存Cookie
    new CookieJarImpl(new MemoryCookieStore())).build();
    OkHttpUtils.initClient(okHttpClient);
}
Also used : PersistentCookieStore(com.zhy.http.okhttp.cookie.store.PersistentCookieStore) OkHttpClient(okhttp3.OkHttpClient) LoggerInterceptor(com.zhy.http.okhttp.log.LoggerInterceptor) CookieJarImpl(com.zhy.http.okhttp.cookie.CookieJarImpl) MemoryCookieStore(com.zhy.http.okhttp.cookie.store.MemoryCookieStore)

Aggregations

Test (org.junit.Test)17 MockResponse (okhttp3.mockwebserver.MockResponse)16 OkHttpClient (okhttp3.OkHttpClient)14 CookieManager (java.net.CookieManager)11 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)9 HttpCookie (java.net.HttpCookie)8 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)8 MockWebServer (okhttp3.mockwebserver.MockWebServer)8 Request (okhttp3.Request)6 Response (okhttp3.Response)5 NonNull (android.support.annotation.NonNull)4 IOException (java.io.IOException)4 List (java.util.List)4 CookieHashSet (me.postaddict.instagram.scraper.cookie.CookieHashSet)4 DefaultCookieJar (me.postaddict.instagram.scraper.cookie.DefaultCookieJar)4 ErrorInterceptor (me.postaddict.instagram.scraper.interceptor.ErrorInterceptor)4 UserAgentInterceptor (me.postaddict.instagram.scraper.interceptor.UserAgentInterceptor)4 Cookie (okhttp3.Cookie)4 JavaNetCookieJar (okhttp3.JavaNetCookieJar)4 ClearableCookieJar (com.franmontiel.persistentcookiejar.ClearableCookieJar)3