Search in sources :

Example 31 with Cookie

use of okhttp3.Cookie in project OkHttp3 by MrZhousf.

the class SharedPrefsCookiePersistor method removeAll.

@Override
public void removeAll(Collection<Cookie> cookies) {
    SharedPreferences.Editor editor = sharedPreferences.edit();
    for (Cookie cookie : cookies) {
        editor.remove(createCookieKey(cookie));
    }
    editor.apply();
}
Also used : Cookie(okhttp3.Cookie) SharedPreferences(android.content.SharedPreferences)

Example 32 with Cookie

use of okhttp3.Cookie in project OkHttp3 by MrZhousf.

the class SharedPrefsCookiePersistor method loadAll.

@Override
public List<Cookie> loadAll() {
    List<Cookie> cookies = new ArrayList<>(sharedPreferences.getAll().size());
    for (Map.Entry<String, ?> entry : sharedPreferences.getAll().entrySet()) {
        String serializedCookie = (String) entry.getValue();
        Cookie cookie = new SerializableCookie().decode(serializedCookie);
        cookies.add(cookie);
    }
    return cookies;
}
Also used : Cookie(okhttp3.Cookie) ArrayList(java.util.ArrayList) Map(java.util.Map)

Example 33 with Cookie

use of okhttp3.Cookie in project smartmodule by carozhu.

the class AddCookiesInterceptor method intercept.

@Override
public Response intercept(Chain chain) throws IOException {
    final Request.Builder builder = chain.request().newBuilder();
    SharedPreferences sharedPreferences = context.getSharedPreferences("cookie", Context.MODE_PRIVATE);
    //RxJava的API
    Observable.just(sharedPreferences.getString("cookie", "")).subscribe(new Action1<String>() {

        @Override
        public void call(String cookie) {
            //添加cookie
            builder.addHeader("Cookie", cookie);
            //                        builder.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
            //                        builder.addHeader("Accept-Encoding", "gzip, deflate");
            //                        builder.addHeader("Connection", "keep-alive");
            //                        builder.addHeader("Accept", "*/*");
            //                        builder.addHeader("Cookie", "add cookies here");
            Log.d("AddCookiesInterceptor", "addHeader ------->:" + cookie);
        }
    });
    return chain.proceed(builder.build());
}
Also used : SharedPreferences(android.content.SharedPreferences) Request(okhttp3.Request)

Example 34 with Cookie

use of okhttp3.Cookie in project smartmodule by carozhu.

the class ReceivedCookiesInterceptor method intercept.

@Override
public Response intercept(Chain chain) throws IOException {
    Response originalResponse = chain.proceed(chain.request());
    //这里获取请求返回的cookie
    if (!originalResponse.headers("Set-Cookie").isEmpty()) {
        final StringBuffer cookieBuffer = new StringBuffer();
        //这里用了RxJava的相关API大家可以忽略,用自己逻辑实现即可.大家可以用别的方法保存cookie数据
        Observable.from(originalResponse.headers("Set-Cookie")).map(new Func1<String, String>() {

            @Override
            public String call(String s) {
                String[] cookieArray = s.split(";");
                return cookieArray[0];
            }
        }).subscribe(new Action1<String>() {

            @Override
            public void call(String cookie) {
                cookieBuffer.append(cookie).append(";");
            }
        });
        SharedPreferences sharedPreferences = mcontext.getSharedPreferences("cookie", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        String[] sessionArry = cookieBuffer.toString().split("=");
        editor.putString("cookie", sessionArry[1]);
        editor.commit();
        Log.d("ReceivedCookiesIntec", "get cookie ------->:" + sessionArry[1]);
    } else {
        Log.d("ReceivedCookiesIntec", "headers Set-Cookie.isEmpty()");
    }
    return originalResponse;
}
Also used : Response(okhttp3.Response) SharedPreferences(android.content.SharedPreferences) Func1(rx.functions.Func1)

Example 35 with Cookie

use of okhttp3.Cookie in project OkHttp3 by MrZhousf.

the class PersistentCookieJar method loadForRequest.

@Override
public synchronized List<Cookie> loadForRequest(HttpUrl url) {
    List<Cookie> removedCookies = new ArrayList<>();
    List<Cookie> validCookies = new ArrayList<>();
    for (Iterator<Cookie> it = cache.iterator(); it.hasNext(); ) {
        Cookie currentCookie = it.next();
        if (isCookieExpired(currentCookie)) {
            removedCookies.add(currentCookie);
            it.remove();
        } else if (currentCookie.matches(url)) {
            validCookies.add(currentCookie);
        }
    }
    persistor.removeAll(removedCookies);
    return validCookies;
}
Also used : Cookie(okhttp3.Cookie) ArrayList(java.util.ArrayList)

Aggregations

Response (okhttp3.Response)34 IOException (java.io.IOException)33 Request (okhttp3.Request)33 Call (okhttp3.Call)25 Callback (okhttp3.Callback)22 RequestBody (okhttp3.RequestBody)21 Test (org.junit.Test)18 Cookie (okhttp3.Cookie)17 MockResponse (okhttp3.mockwebserver.MockResponse)16 FormBody (okhttp3.FormBody)12 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)10 CookieManager (java.net.CookieManager)9 HttpCookie (java.net.HttpCookie)9 MockWebServer (okhttp3.mockwebserver.MockWebServer)8 OkHttpClient (okhttp3.OkHttpClient)6 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 HttpUrl (okhttp3.HttpUrl)5 SharedPreferences (android.content.SharedPreferences)4 OnClick (butterknife.OnClick)4