Search in sources :

Example 36 with Cookie

use of okhttp3.Cookie in project OkHttp3 by MrZhousf.

the class SerializableCookie method decode.

public Cookie decode(String encodedCookie) {
    byte[] bytes = hexStringToByteArray(encodedCookie);
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
    Cookie cookie = null;
    ObjectInputStream objectInputStream = null;
    try {
        objectInputStream = new ObjectInputStream(byteArrayInputStream);
        cookie = ((SerializableCookie) objectInputStream.readObject()).cookie;
    } catch (IOException e) {
        Log.d(TAG, "IOException in decodeCookie", e);
    } catch (ClassNotFoundException e) {
        Log.d(TAG, "ClassNotFoundException in decodeCookie", e);
    } finally {
        if (objectInputStream != null) {
            try {
                objectInputStream.close();
            } catch (IOException e) {
                Log.d(TAG, "Stream not closed in decodeCookie", e);
            }
        }
    }
    return cookie;
}
Also used : Cookie(okhttp3.Cookie) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

Example 37 with Cookie

use of okhttp3.Cookie in project JustAndroid by chinaltz.

the class PersistentCookieStore method decodeCookie.

/**
     * 将字符串反序列化成cookies
     *
     * @param cookieString cookies string
     * @return cookie object
     */
protected Cookie decodeCookie(String cookieString) {
    byte[] bytes = hexStringToByteArray(cookieString);
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
    Cookie cookie = null;
    try {
        ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
        cookie = ((SerializableOkHttpCookies) objectInputStream.readObject()).getCookies();
    } catch (IOException e) {
        Log.d(LOG_TAG, "IOException in decodeCookie", e);
    } catch (ClassNotFoundException e) {
        Log.d(LOG_TAG, "ClassNotFoundException in decodeCookie", e);
    }
    return cookie;
}
Also used : Cookie(okhttp3.Cookie) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

Example 38 with Cookie

use of okhttp3.Cookie in project okhttp-OkGo by jeasonlzy.

the class CookieActivity method addCookie.

@OnClick(R.id.addCookie)
public void addCookie(View view) {
    HttpUrl httpUrl = HttpUrl.parse(Urls.URL_METHOD);
    Cookie.Builder builder = new Cookie.Builder();
    Cookie cookie = builder.name("myCookieKey1").value("myCookieValue1").domain(httpUrl.host()).build();
    CookieStore cookieStore = OkGo.getInstance().getCookieJar().getCookieStore();
    cookieStore.saveCookie(httpUrl, cookie);
    showToast("详细添加cookie的代码,请看demo的代码");
    //
    OkGo.post(Urls.URL_TEXT_UPLOAD).tag(//
    this).execute(new StringDialogCallback(this) {

        @Override
        public void onSuccess(String s, Call call, Response response) {
            handleResponse(s, call, response);
        }

        @Override
        public void onError(Call call, Response response, Exception e) {
            super.onError(call, response, e);
            handleError(call, response);
        }
    });
}
Also used : Cookie(okhttp3.Cookie) StringDialogCallback(com.lzy.demo.callback.StringDialogCallback) Response(okhttp3.Response) CookieStore(com.lzy.okgo.cookie.store.CookieStore) Call(okhttp3.Call) HttpUrl(okhttp3.HttpUrl) OnClick(butterknife.OnClick)

Example 39 with Cookie

use of okhttp3.Cookie in project okhttp-OkGo by jeasonlzy.

the class MemoryCookieStore method saveCookie.

@Override
public synchronized void saveCookie(HttpUrl url, Cookie cookie) {
    List<Cookie> cookies = memoryCookies.get(url.host());
    List<Cookie> needRemove = new ArrayList<>();
    for (Cookie item : cookies) {
        if (cookie.name().equals(item.name())) {
            needRemove.add(item);
        }
    }
    cookies.removeAll(needRemove);
    cookies.add(cookie);
}
Also used : Cookie(okhttp3.Cookie) ArrayList(java.util.ArrayList)

Example 40 with Cookie

use of okhttp3.Cookie in project okhttp-OkGo by jeasonlzy.

the class MemoryCookieStore method saveCookie.

@Override
public synchronized void saveCookie(HttpUrl url, List<Cookie> cookies) {
    List<Cookie> oldCookies = memoryCookies.get(url.host());
    List<Cookie> needRemove = new ArrayList<>();
    for (Cookie newCookie : cookies) {
        for (Cookie oldCookie : oldCookies) {
            if (newCookie.name().equals(oldCookie.name())) {
                needRemove.add(oldCookie);
            }
        }
    }
    oldCookies.removeAll(needRemove);
    oldCookies.addAll(cookies);
}
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