Search in sources :

Example 21 with CookieJar

use of okhttp3.CookieJar in project java-sdk by watson-developer-cloud.

the class WatsonCookieJar method loadForRequest.

/*
   * (non-Javadoc)
   *
   * @see okhttp3.CookieJar#loadForRequest(okhttp3.HttpUrl)
   */
@Override
public List<Cookie> loadForRequest(HttpUrl url) {
    List<Cookie> cookies = this.adapter.loadForRequest(url);
    // TODO: Removes the SESSIONID for speech to text session lest requests
    if (url.encodedPathSegments().contains(SPEECH_TO_TEXT) && !url.encodedPathSegments().contains(SESSIONS)) {
        List<Cookie> sessionLessCookies = new ArrayList<Cookie>();
        for (Cookie cookie : cookies) {
            if (!cookie.name().equalsIgnoreCase(SESSIONID)) {
                sessionLessCookies.add(cookie);
            }
        }
        cookies = sessionLessCookies;
    }
    return cookies;
}
Also used : Cookie(okhttp3.Cookie) ArrayList(java.util.ArrayList)

Example 22 with CookieJar

use of okhttp3.CookieJar in project AndroidFrame by tongxiaoyun.

the class m method initNetWorkWithCookie.

/**
 * 初始化网络控制器
 */
public m initNetWorkWithCookie(Context context) {
    // 持久化存储cookie
    ClearableCookieJar cookieJar = new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(context));
    // log拦截器
    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    logging.setLevel(HttpLoggingInterceptor.Level.BODY);
    // 自定义OkHttp
    OkHttpClient okHttpClient = new OkHttpClient.Builder().connectTimeout(10000L, TimeUnit.MILLISECONDS).readTimeout(10000L, TimeUnit.MILLISECONDS).cookieJar(// 设置开启cookie
    cookieJar).addInterceptor(// 设置开启log
    logging).build();
    netUtils = new MyOkHttp(okHttpClient);
    return instance;
}
Also used : MyOkHttp(com.risenb.expand.network.MyOkHttp) ClearableCookieJar(com.risenb.expand.network.cookie.ClearableCookieJar) OkHttpClient(okhttp3.OkHttpClient) PersistentCookieJar(com.risenb.expand.network.cookie.PersistentCookieJar) SetCookieCache(com.risenb.expand.network.cookie.cache.SetCookieCache) SharedPrefsCookiePersistor(com.risenb.expand.network.cookie.persistence.SharedPrefsCookiePersistor) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor)

Example 23 with CookieJar

use of okhttp3.CookieJar in project okhttputils by hongyangAndroid.

the class MyApplication method onCreate.

@Override
public void onCreate() {
    super.onCreate();
    ClearableCookieJar cookieJar1 = new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(getApplicationContext()));
    HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory(null, null, null);
    //        CookieJarImpl cookieJar1 = new CookieJarImpl(new MemoryCookieStore());
    OkHttpClient okHttpClient = new OkHttpClient.Builder().connectTimeout(10000L, TimeUnit.MILLISECONDS).readTimeout(10000L, TimeUnit.MILLISECONDS).addInterceptor(new LoggerInterceptor("TAG")).cookieJar(cookieJar1).hostnameVerifier(new HostnameVerifier() {

        @Override
        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    }).sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager).build();
    OkHttpUtils.initClient(okHttpClient);
}
Also used : ClearableCookieJar(com.franmontiel.persistentcookiejar.ClearableCookieJar) OkHttpClient(okhttp3.OkHttpClient) LoggerInterceptor(com.zhy.http.okhttp.log.LoggerInterceptor) PersistentCookieJar(com.franmontiel.persistentcookiejar.PersistentCookieJar) SetCookieCache(com.franmontiel.persistentcookiejar.cache.SetCookieCache) SSLSession(javax.net.ssl.SSLSession) SharedPrefsCookiePersistor(com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor) HttpsUtils(com.zhy.http.okhttp.https.HttpsUtils) HostnameVerifier(javax.net.ssl.HostnameVerifier)

Example 24 with CookieJar

use of okhttp3.CookieJar in project okhttp by square.

the class UrlConnectionCacheTest method cachePlusCookies.

@Test
public void cachePlusCookies() throws Exception {
    RecordingCookieJar cookieJar = new RecordingCookieJar();
    urlFactory.setClient(urlFactory.client().newBuilder().cookieJar(cookieJar).build());
    server.enqueue(new MockResponse().addHeader("Set-Cookie: a=FIRST").addHeader("Last-Modified: " + formatDate(-1, TimeUnit.HOURS)).addHeader("Cache-Control: max-age=0").setBody("A"));
    server.enqueue(new MockResponse().addHeader("Set-Cookie: a=SECOND").setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
    URL url = server.url("/").url();
    assertEquals("A", readAscii(urlFactory.open(url)));
    cookieJar.assertResponseCookies("a=FIRST; path=/");
    assertEquals("A", readAscii(urlFactory.open(url)));
    cookieJar.assertResponseCookies("a=SECOND; path=/");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) URL(java.net.URL) Test(org.junit.Test)

Example 25 with CookieJar

use of okhttp3.CookieJar in project okhttp by square.

the class CacheTest method cachePlusCookies.

@Test
public void cachePlusCookies() throws Exception {
    RecordingCookieJar cookieJar = new RecordingCookieJar();
    client = client.newBuilder().cookieJar(cookieJar).build();
    server.enqueue(new MockResponse().addHeader("Set-Cookie: a=FIRST").addHeader("Last-Modified: " + formatDate(-1, TimeUnit.HOURS)).addHeader("Cache-Control: max-age=0").setBody("A"));
    server.enqueue(new MockResponse().addHeader("Set-Cookie: a=SECOND").setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
    HttpUrl url = server.url("/");
    assertEquals("A", get(url).body().string());
    cookieJar.assertResponseCookies("a=FIRST; path=/");
    assertEquals("A", get(url).body().string());
    cookieJar.assertResponseCookies("a=SECOND; path=/");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

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