Search in sources :

Example 21 with Cookie

use of com.firenio.codec.http11.Cookie in project okhttp-OkGo by jeasonlzy.

the class DBCookieStore method loadCookie.

/**
 * 根据当前url获取所有需要的cookie,只返回没有过期的cookie
 */
@Override
public synchronized List<Cookie> loadCookie(HttpUrl url) {
    List<Cookie> ret = new ArrayList<>();
    if (!cookies.containsKey(url.host()))
        return ret;
    List<SerializableCookie> query = CookieManager.getInstance().query("host=?", new String[] { url.host() });
    for (SerializableCookie serializableCookie : query) {
        Cookie cookie = serializableCookie.getCookie();
        if (isCookieExpired(cookie)) {
            removeCookie(url, cookie);
        } else {
            ret.add(cookie);
        }
    }
    return ret;
}
Also used : Cookie(okhttp3.Cookie) SerializableCookie(com.lzy.okgo.cookie.SerializableCookie) ArrayList(java.util.ArrayList) SerializableCookie(com.lzy.okgo.cookie.SerializableCookie)

Example 22 with Cookie

use of com.firenio.codec.http11.Cookie in project PokeGOAPI-Java by Grover-c13.

the class PtcCredentialProvider method login.

/**
 * Starts a login flow for pokemon.com (PTC) using a username and password,
 * this uses pokemon.com's oauth endpoint and returns a usable AuthInfo without user interaction
 *
 * @param username PTC username
 * @param password PTC password
 * @param attempt the current attempt index
 * @throws LoginFailedException if an exception occurs while attempting to log in
 * @throws InvalidCredentialsException if invalid credentials are used
 */
private void login(String username, String password, int attempt) throws LoginFailedException, InvalidCredentialsException {
    try {
        // TODO: stop creating an okhttp client per request
        Response getResponse;
        try {
            getResponse = client.newCall(new Request.Builder().url(HttpUrl.parse("https://sso.pokemon.com/sso/oauth2.0/authorize").newBuilder().addQueryParameter("client_id", CLIENT_ID).addQueryParameter("redirect_uri", REDIRECT_URI).addQueryParameter("locale", "en").build()).get().build()).execute();
        } catch (IOException e) {
            throw new LoginFailedException("Failed to receive contents from server", e);
        }
        Moshi moshi = new Moshi.Builder().build();
        PtcAuthJson ptcAuth;
        try {
            ptcAuth = moshi.adapter(PtcAuthJson.class).fromJson(getResponse.body().string());
        } catch (IOException e) {
            throw new LoginFailedException("Looks like the servers are down", e);
        }
        Response postResponse;
        try {
            FormBody postForm = new Builder().add("lt", ptcAuth.lt).add("execution", ptcAuth.execution).add("_eventId", "submit").add("username", username).add("password", password).add("locale", "en_US").build();
            HttpUrl loginPostUrl = HttpUrl.parse(LOGIN_URL).newBuilder().addQueryParameter("service", SERVICE_URL).build();
            Request postRequest = new Request.Builder().url(loginPostUrl).post(postForm).build();
            // Need a new client for this to not follow redirects
            postResponse = client.newBuilder().followRedirects(false).followSslRedirects(false).build().newCall(postRequest).execute();
        } catch (IOException e) {
            throw new LoginFailedException("Network failure", e);
        }
        String postBody;
        try {
            postBody = postResponse.body().string();
        } catch (IOException e) {
            throw new LoginFailedException("Response body fetching failed", e);
        }
        List<Cookie> cookies = client.cookieJar().loadForRequest(HttpUrl.parse(LOGIN_URL));
        for (Cookie cookie : cookies) {
            if (cookie.name().startsWith("CASTGC")) {
                this.tokenId = cookie.value();
                expiresTimestamp = time.currentTimeMillis() + 7140000L;
                return;
            }
        }
        if (postBody.length() > 0) {
            try {
                String[] errors = moshi.adapter(PtcAuthError.class).fromJson(postBody).errors;
                if (errors != null && errors.length > 0) {
                    throw new InvalidCredentialsException(errors[0]);
                }
            } catch (IOException e) {
                throw new LoginFailedException("Failed to parse ptc error json");
            }
        }
    } catch (LoginFailedException e) {
        if (shouldRetry && attempt < MAXIMUM_RETRIES) {
            login(username, password, ++attempt);
        } else {
            throw new LoginFailedException("Exceeded maximum login retries", e);
        }
    }
}
Also used : Cookie(okhttp3.Cookie) Moshi(com.squareup.moshi.Moshi) Builder(okhttp3.FormBody.Builder) FormBody(okhttp3.FormBody) Request(okhttp3.Request) IOException(java.io.IOException) HttpUrl(okhttp3.HttpUrl) Response(okhttp3.Response) LoginFailedException(com.pokegoapi.exceptions.request.LoginFailedException) InvalidCredentialsException(com.pokegoapi.exceptions.request.InvalidCredentialsException)

Example 23 with Cookie

use of com.firenio.codec.http11.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 24 with Cookie

use of com.firenio.codec.http11.Cookie in project OkHttp3 by MrZhousf.

the class SharedPrefsCookiePersistor method saveAll.

@Override
public void saveAll(Collection<Cookie> cookies) {
    SharedPreferences.Editor editor = sharedPreferences.edit();
    for (Cookie cookie : cookies) {
        if (cookie.persistent()) {
            editor.putString(createCookieKey(cookie), new SerializableCookie().encode(cookie));
        }
    }
    editor.apply();
}
Also used : Cookie(okhttp3.Cookie) SharedPreferences(android.content.SharedPreferences)

Example 25 with Cookie

use of com.firenio.codec.http11.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)

Aggregations

Cookie (okhttp3.Cookie)102 ArrayList (java.util.ArrayList)31 IOException (java.io.IOException)20 HttpUrl (okhttp3.HttpUrl)18 ByteArrayInputStream (java.io.ByteArrayInputStream)16 ObjectInputStream (java.io.ObjectInputStream)16 SharedPreferences (android.content.SharedPreferences)12 Context (android.content.Context)8 HashMap (java.util.HashMap)8 List (java.util.List)8 Map (java.util.Map)8 Test (org.junit.Test)8 Response (okhttp3.Response)7 SerializableCookie (com.lzy.okgo.cookie.SerializableCookie)6 Request (okhttp3.Request)6 SuppressLint (android.annotation.SuppressLint)4 CookieManager (android.webkit.CookieManager)4 EhCookieStore (com.hippo.ehviewer.client.EhCookieStore)4 OnClick (butterknife.OnClick)3 SerializableCookie (com.franmontiel.persistentcookiejar.persistence.SerializableCookie)3