Search in sources :

Example 1 with Cookie

use of cz.msebera.android.httpclient.cookie.Cookie in project AndroidStudy by tinggengyan.

the class HttpCookie method run.

@Override
public void run() {
    list.add(new BasicNameValuePair("name", "steve"));
    list.add(new BasicNameValuePair("age", "12"));
    try {
        post.setEntity(new UrlEncodedFormEntity(list));
        HttpResponse response = client.execute(post);
        if (response.getStatusLine().getStatusCode() == 200) {
            AbstractHttpClient absclient = (AbstractHttpClient) client;
            List<Cookie> cookies = absclient.getCookieStore().getCookies();
            for (Cookie cookie : cookies) {
                System.out.println("name==" + cookie.getName());
                System.out.println("value==" + cookie.getValue());
                Message message = new Message();
                message.obj = cookie;
                mHandler.sendMessage(message);
                return;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : AbstractHttpClient(cz.msebera.android.httpclient.impl.client.AbstractHttpClient) Cookie(cz.msebera.android.httpclient.cookie.Cookie) Message(android.os.Message) BasicNameValuePair(cz.msebera.android.httpclient.message.BasicNameValuePair) HttpResponse(cz.msebera.android.httpclient.HttpResponse) UrlEncodedFormEntity(cz.msebera.android.httpclient.client.entity.UrlEncodedFormEntity)

Example 2 with Cookie

use of cz.msebera.android.httpclient.cookie.Cookie in project android-async-http by loopj.

the class PersistentCookieStore method clearExpired.

@Override
public boolean clearExpired(Date date) {
    boolean clearedAny = false;
    SharedPreferences.Editor prefsWriter = cookiePrefs.edit();
    for (ConcurrentHashMap.Entry<String, Cookie> entry : cookies.entrySet()) {
        String name = entry.getKey();
        Cookie cookie = entry.getValue();
        if (cookie.isExpired(date)) {
            // Clear cookies from local store
            cookies.remove(name);
            // Clear cookies from persistent store
            prefsWriter.remove(COOKIE_NAME_PREFIX + name);
            // We've cleared at least one
            clearedAny = true;
        }
    }
    // Update names in persistent store
    if (clearedAny) {
        prefsWriter.putString(COOKIE_NAME_STORE, TextUtils.join(",", cookies.keySet()));
    }
    prefsWriter.commit();
    return clearedAny;
}
Also used : Cookie(cz.msebera.android.httpclient.cookie.Cookie) SharedPreferences(android.content.SharedPreferences) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 3 with Cookie

use of cz.msebera.android.httpclient.cookie.Cookie in project android-async-http by loopj.

the class PersistentCookieStore method decodeCookie.

/**
     * Returns cookie decoded from cookie string
     *
     * @param cookieString string of cookie as returned from http request
     * @return decoded cookie or null if exception occured
     */
protected Cookie decodeCookie(String cookieString) {
    byte[] bytes = hexStringToByteArray(cookieString);
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
    Cookie cookie = null;
    try {
        ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
        cookie = ((SerializableCookie) objectInputStream.readObject()).getCookie();
    } catch (IOException e) {
        AsyncHttpClient.log.d(LOG_TAG, "IOException in decodeCookie", e);
    } catch (ClassNotFoundException e) {
        AsyncHttpClient.log.d(LOG_TAG, "ClassNotFoundException in decodeCookie", e);
    }
    return cookie;
}
Also used : Cookie(cz.msebera.android.httpclient.cookie.Cookie) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

Cookie (cz.msebera.android.httpclient.cookie.Cookie)3 SharedPreferences (android.content.SharedPreferences)1 Message (android.os.Message)1 HttpResponse (cz.msebera.android.httpclient.HttpResponse)1 UrlEncodedFormEntity (cz.msebera.android.httpclient.client.entity.UrlEncodedFormEntity)1 AbstractHttpClient (cz.msebera.android.httpclient.impl.client.AbstractHttpClient)1 BasicNameValuePair (cz.msebera.android.httpclient.message.BasicNameValuePair)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 ObjectInputStream (java.io.ObjectInputStream)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1