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.debux.webmotion.test.ActionMiscIT.java

@Test
public void flashMessage() throws IOException, URISyntaxException {
    URI uri = createRequest("/message").build();

    HttpGet request = new HttpGet(uri);

    DefaultHttpClient client = new DefaultHttpClient();
    client.execute(request);/*from  w ww  .j  ava2  s  .  c  o m*/

    CookieStore cookieStore = client.getCookieStore();
    List<Cookie> cookies = cookieStore.getCookies();
    for (Cookie cookie : cookies) {
        String name = cookie.getName();
        String value = cookie.getValue();
        if ("wm_flash_infos_my_message".equals(name)) {
            AssertJUnit.assertEquals("\"bla bla bla bla\"", value);
            return;
        }
    }
    throw new RuntimeException("Invalid cookie");
}

From source file:org.debux.webmotion.test.ActionMiscIT.java

@Test
public void cookieManagerCreate() throws IOException, URISyntaxException {
    URI uri = createRequest("/cookie/create").addParameter("secured", "true").build();

    HttpGet request = new HttpGet(uri);

    DefaultHttpClient client = new DefaultHttpClient();
    HttpResponse response = client.execute(request);

    HttpEntity entity = response.getEntity();
    InputStream content = entity.getContent();
    String result = IOUtils.toString(content);
    AssertJUnit.assertTrue(result, result.contains("Value = a_value"));

    CookieStore cookieStore = client.getCookieStore();
    List<Cookie> cookies = cookieStore.getCookies();
    for (Cookie cookie : cookies) {
        String name = cookie.getName();
        String value = cookie.getValue();
        if ("secured_name".equals(name)) {
            AssertJUnit.assertTrue(value.startsWith("me|-1"));
            return;
        }//from w w w  . j  ava  2 s. c  o m
    }
    throw new RuntimeException("Invalid cookie");
}

From source file:org.debux.webmotion.test.ActionMiscIT.java

@Test
public void cookieManagerObjectCreate() throws IOException, URISyntaxException {
    URI uri = createRequest("/cookie/object/create").addParameter("value", "test").build();

    HttpGet request = new HttpGet(uri);

    DefaultHttpClient client = new DefaultHttpClient();
    client.execute(request);/*from www .  j  a va 2  s. c  o m*/

    CookieStore cookieStore = client.getCookieStore();
    List<Cookie> cookies = cookieStore.getCookies();
    for (Cookie cookie : cookies) {
        String name = cookie.getName();
        String value = cookie.getValue();
        if ("user_cookie".equals(name)) {
            value = value.replaceAll("\\\\", "");
            AssertJUnit.assertEquals("{\"value\":\"test\"}", value);
            return;
        }
    }
    throw new RuntimeException("Invalid cookie");
}

From source file:org.transdroid.core.gui.TorrentsActivity.java

@Background
protected void addTorrentFromWeb(String url, WebsearchSetting websearchSetting, String title) {

    try {/*from www  . j  av  a2 s. c  o m*/
        // Cookies are taken from the websearchSetting that we already matched against this target URL
        DefaultHttpClient httpclient = HttpHelper.createStandardHttpClient(false, null, null, true, null, 10000,
                null, -1);
        Map<String, String> cookies = HttpHelper.parseCookiePairs(websearchSetting.getCookies());
        String domain = Uri.parse(url).getHost();
        for (Entry<String, String> pair : cookies.entrySet()) {
            BasicClientCookie cookie = new BasicClientCookie(pair.getKey(), pair.getValue());
            cookie.setPath("/");
            cookie.setDomain(domain);
            httpclient.getCookieStore().addCookie(cookie);
        }

        // Download the torrent at the specified URL (which will first be written to a temporary file)
        // If we get an HTTP 401, 403 or 404 response, show an error to the user
        HttpResponse response = httpclient.execute(new HttpGet(url));
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED
                || response.getStatusLine().getStatusCode() == HttpStatus.SC_FORBIDDEN
                || response.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_FOUND) {
            log.e(this, "Can't retrieve web torrent " + url + ": Unexpected HTTP response status code "
                    + response.getStatusLine().toString());
            SnackbarManager.show(Snackbar.with(this).text(R.string.error_401).colorResource(R.color.red));
            return;
        }
        InputStream input = response.getEntity().getContent();
        addTorrentFromStream(input, title);
    } catch (Exception e) {
        log.e(this, "Can't retrieve web torrent " + url + ": " + e.toString());
        SnackbarManager.show(Snackbar.with(this).text(R.string.error_torrentfile).colorResource(R.color.red));
    }
}