Example usage for org.apache.http.impl.client DefaultHttpClient getCookieStore

List of usage examples for org.apache.http.impl.client DefaultHttpClient getCookieStore

Introduction

In this page you can find the example usage for org.apache.http.impl.client DefaultHttpClient getCookieStore.

Prototype

public synchronized final CookieStore getCookieStore() 

Source Link

Usage

From source file:org.androidnerds.reader.util.api.Authentication.java

/**
 * This method generates a quick token to send with API requests that require
 * editing content. This method is called as the API request is being built so
 * that it doesn't expire prior to the actual execution.
 *
 * @param sid - the user's authentication token from ClientLogin
 * @return token - the edit token generated by the server.
 *
 *///  w  w w. j  ava  2s.c  o  m
public static String generateFastToken(String sid) {
    try {
        BasicClientCookie cookie = Authentication.buildCookie(sid);
        DefaultHttpClient client = new DefaultHttpClient();

        client.getCookieStore().addCookie(cookie);

        HttpGet get = new HttpGet(TOKEN_URL);
        HttpResponse response = client.execute(get);
        HttpEntity entity = response.getEntity();

        Log.d(TAG, "Server Response: " + response.getStatusLine());

        InputStream in = entity.getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        String line = null;

        while ((line = reader.readLine()) != null) {
            Log.d(TAG, "Response Content: " + line);
        }

        reader.close();
        client.getConnectionManager().shutdown();

        return line;
    } catch (Exception e) {
        Log.d(TAG, "Exception caught:: " + e.toString());
        return null;
    }
}

From source file:com.quietlycoding.android.reader.util.api.Authentication.java

/**
 * This method generates a quick token to send with API requests that
 * require editing content. This method is called as the API request is
 * being built so that it doesn't expire prior to the actual execution.
 * /* w w  w  .  j  a va 2  s.c  o m*/
 * @param sid
 *            - the user's authentication token from ClientLogin
 * @return token - the edit token generated by the server.
 * 
 */
public static String generateFastToken(String sid) {
    try {
        final BasicClientCookie cookie = Authentication.buildCookie(sid);
        final DefaultHttpClient client = new DefaultHttpClient();

        client.getCookieStore().addCookie(cookie);

        final HttpGet get = new HttpGet(TOKEN_URL);
        final HttpResponse response = client.execute(get);
        final HttpEntity entity = response.getEntity();

        Log.d(TAG, "Server Response: " + response.getStatusLine());

        final InputStream in = entity.getContent();
        final BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        String line = null;

        while ((line = reader.readLine()) != null) {
            Log.d(TAG, "Response Content: " + line);
        }

        reader.close();
        client.getConnectionManager().shutdown();

        return line;
    } catch (final Exception e) {
        Log.d(TAG, "Exception caught:: " + e.toString());
        return null;
    }
}

From source file:be.ac.ucl.lfsab1509.llncampus.ExternalAppUtility.java

/**
 * Create a new HTTP client with always the same session of cookies.
 * This can be used for ADE connections for example.
 * /*www  .  ja v a2s .  co  m*/
 * @return An HttpClient. 
 */
public static synchronized HttpClient getHttpClient() {
    final DefaultHttpClient httpClient = new DefaultHttpClient();
    if (cookieStore == null) {
        cookieStore = httpClient.getCookieStore();
    } else {
        httpClient.setCookieStore(cookieStore);
    }
    HttpProtocolParams.setUserAgent(httpClient.getParams(), "Mozilla 5/0");
    return httpClient;
}

From source file:org.androidnerds.reader.util.api.Subscriptions.java

/**
 * This method queries Google Reader for the list of subscribed feeds.
 * /*w  ww  .  j  a  va 2 s .c  o  m*/
 * @param sid authentication code to pass along in a cookie.
 * @return arr returns a JSONArray of JSONObjects for each feed.
 *
 * The JSONObject returned by the service looks like this:
 *    id: this is the feed url.
 *    title: this is the title of the feed.
 *    sortid: this has not been figured out yet.
 *    firstitemsec: this has not been figured out yet.
 */
public static JSONArray getSubscriptionList(String sid) {
    DefaultHttpClient client = new DefaultHttpClient();
    HttpGet get = new HttpGet(SUB_URL + "/list?output=json");
    BasicClientCookie cookie = Authentication.buildCookie(sid);

    try {
        client.getCookieStore().addCookie(cookie);

        HttpResponse response = client.execute(get);
        HttpEntity respEntity = response.getEntity();

        Log.d(TAG, "Response from server: " + response.getStatusLine());

        InputStream in = respEntity.getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));

        String line = "";
        String arr = "";
        while ((line = reader.readLine()) != null) {
            arr += line;
        }

        JSONObject obj = new JSONObject(arr);
        JSONArray array = obj.getJSONArray("subscriptions");

        reader.close();
        client.getConnectionManager().shutdown();

        return array;
    } catch (Exception e) {
        Log.d(TAG, "Exception caught:: " + e.toString());
        return null;
    }
}

From source file:org.androidnerds.reader.util.api.Tags.java

/**
 * This method pulls the tags from Google Reader, its used by the 
 * methods in this class to communicate before parsing the specific
 * results./*from w ww .  j  a v  a 2s.  c o m*/
 *
 * @param sid the Google Reader authentication string.
 * @return arr JSONArray of the items from the server.
 */
private static JSONArray pullTags(String sid) {
    DefaultHttpClient client = new DefaultHttpClient();
    HttpGet get = new HttpGet(URL);
    BasicClientCookie cookie = Authentication.buildCookie(sid);

    try {
        client.getCookieStore().addCookie(cookie);

        HttpResponse response = client.execute(get);
        HttpEntity respEntity = response.getEntity();

        Log.d(TAG, "Response from server: " + response.getStatusLine());

        InputStream in = respEntity.getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));

        String line = "";
        String arr = "";
        while ((line = reader.readLine()) != null) {
            arr += line;
        }

        JSONObject obj = new JSONObject(arr);
        JSONArray array = obj.getJSONArray("tags");

        return array;
    } catch (Exception e) {
        Log.d(TAG, "Exception caught:: " + e.toString());
        return null;
    }
}

From source file:com.quietlycoding.android.reader.util.api.Subscriptions.java

/**
 * This method queries Google Reader for the list of subscribed feeds.
 * //from  ww w. ja v a  2 s. c o m
 * @param sid
 *            authentication code to pass along in a cookie.
 * @return arr returns a JSONArray of JSONObjects for each feed.
 * 
 *         The JSONObject returned by the service looks like this: id: this
 *         is the feed url. title: this is the title of the feed. sortid:
 *         this has not been figured out yet. firstitemsec: this has not
 *         been figured out yet.
 */
public static JSONArray getSubscriptionList(String sid) {
    final DefaultHttpClient client = new DefaultHttpClient();
    final HttpGet get = new HttpGet(SUB_URL + "/list?output=json");
    final BasicClientCookie cookie = Authentication.buildCookie(sid);

    try {
        client.getCookieStore().addCookie(cookie);

        final HttpResponse response = client.execute(get);
        final HttpEntity respEntity = response.getEntity();

        Log.d(TAG, "Response from server: " + response.getStatusLine());

        final InputStream in = respEntity.getContent();
        final BufferedReader reader = new BufferedReader(new InputStreamReader(in));

        String line = "";
        String arr = "";
        while ((line = reader.readLine()) != null) {
            arr += line;
        }

        final JSONObject obj = new JSONObject(arr);
        final JSONArray array = obj.getJSONArray("subscriptions");

        reader.close();
        client.getConnectionManager().shutdown();

        return array;
    } catch (final Exception e) {
        Log.d(TAG, "Exception caught:: " + e.toString());
        return null;
    }
}

From source file:com.quietlycoding.android.reader.util.api.Tags.java

/**
 * This method pulls the tags from Google Reader, its used by the methods in
 * this class to communicate before parsing the specific results.
 * /*from   www.j av  a  2 s  .c  om*/
 * @param sid
 *            the Google Reader authentication string.
 * @return arr JSONArray of the items from the server.
 */
private static JSONArray pullTags(String sid) {
    final DefaultHttpClient client = new DefaultHttpClient();
    final HttpGet get = new HttpGet(URL);
    final BasicClientCookie cookie = Authentication.buildCookie(sid);

    try {
        client.getCookieStore().addCookie(cookie);

        final HttpResponse response = client.execute(get);
        final HttpEntity respEntity = response.getEntity();

        Log.d(TAG, "Response from server: " + response.getStatusLine());

        final InputStream in = respEntity.getContent();
        final BufferedReader reader = new BufferedReader(new InputStreamReader(in));

        String line = "";
        String arr = "";
        while ((line = reader.readLine()) != null) {
            arr += line;
        }

        final JSONObject obj = new JSONObject(arr);
        final JSONArray array = obj.getJSONArray("tags");

        return array;
    } catch (final Exception e) {
        Log.d(TAG, "Exception caught:: " + e.toString());
        return null;
    }
}

From source file:com.base.httpclient.HttpJsonClient.java

/**
 * httpClient/*from w  w  w .j a v  a 2s  .  co  m*/
 * @return
 */
public static DefaultHttpClient getHttpClient() {
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
    schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));
    PoolingClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry);
    HttpParams httpParams = new BasicHttpParams();
    cm.setMaxTotal(10);//   
    cm.setDefaultMaxPerRoute(5);// ? 
    HttpConnectionParams.setConnectionTimeout(httpParams, 60000);//
    HttpConnectionParams.setSoTimeout(httpParams, 60000);//?
    HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setUseExpectContinue(httpParams, false);
    httpParams.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.IGNORE_COOKIES);
    DefaultHttpClient httpClient = new DefaultHttpClient(cm, httpParams);
    //httpClient.setCookieStore(null);
    httpClient.getCookieStore().clear();
    httpClient.getCookieStore().getCookies().clear();
    //   httpClient.setHttpRequestRetryHandler(new HttpJsonClient().new HttpRequestRetry());//?
    return httpClient;
}

From source file:com.dajodi.scandic.ScandicSessionHelper.java

private static void login(String username, String password) throws Exception {

    DefaultHttpClient client = Singleton.INSTANCE.getHttpClient();
    client.getCookieStore().clear();

    HttpHead head = new HttpHead("https://www.scandichotels.com/Frequent-Guest-Programme/");

    // only for user-agent
    Util.gzipify(head);//from   ww  w . j  a  va2s.c o m

    HttpResponse response = client.execute(head);

    if (response.getStatusLine().getStatusCode() != 200) {
        throw new ScandicHtmlException("HEAD request to FG page did not return a 200, instead "
                + response.getStatusLine().getStatusCode());
    }
    head.abort();

    boolean found = false;
    // assume this cookie exists
    for (Cookie cookie : client.getCookieStore().getCookies()) {
        if (SESSION_ID_COOKIE_NAME.equals(cookie.getName())) {
            found = true;
            break;
        }
    }
    if (!found) {
        throw new ScandicHtmlException("Session id cookie not valid from head request, dying");
    }

    List<NameValuePair> nvps = new LinkedList<NameValuePair>();
    nvps.add(new BasicNameValuePair("ctl00$MenuLoginStatus$txtLoyaltyUsername", username));
    nvps.add(new BasicNameValuePair("ctl00$MenuLoginStatus$txtLoyaltyPassword", password));
    nvps.add(new BasicNameValuePair("ctl00$MenuLoginStatus$loginPopUpID", "LOGIN_POPUP_MODULE"));
    nvps.add(new BasicNameValuePair("ctl00$MenuLoginStatus$loginPopUpPageID", "LOGIN_POPUP_MODULE"));
    nvps.add(new BasicNameValuePair("__PREVIOUSPAGE", ""));
    nvps.add(new BasicNameValuePair("__EVENTTARGET", "ctl00$MenuLoginStatus$btnLogIn"));

    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nvps);

    // now the post
    HttpPost post = new HttpPost("https://www.scandichotels.com/templates/Booking/Units/LoginValidator.aspx");

    post.setHeader("Content-Type", "application/x-www-form-urlencoded");

    // needed, we don't want redirecting here
    final HttpParams params = new BasicHttpParams();
    HttpClientParams.setRedirecting(params, false);
    post.setParams(params);

    // not really needed, but why not
    Util.gzipify(post);

    post.setEntity(entity);
    response = client.execute(post);
    post.abort();

    if (isLoggedIn()) {
        Log.d("Success!  Logged in via Java code!");
    } else if (response.getStatusLine().getStatusCode() == 302
            && response.getFirstHeader("Location").getValue().contains("Login-Error")) {
        throw new InvalidLoginException();

    } else {
        throw new RuntimeException("Could not login!");
    }
}

From source file:at.ac.uniklu.mobile.sportal.util.Utils.java

public static Cookie getCookie(DefaultHttpClient httpClient, String cookieName) {
    List<Cookie> cookies = httpClient.getCookieStore().getCookies();
    if (!cookies.isEmpty()) {
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(cookieName)) {
                return cookie;
            }//from   w w  w .  j  a  v  a  2s  .co m
        }
    }
    return null;
}