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.jboss.as.test.integration.web.security.form.AbstractWebSecurityFORMTestCase.java

/**
 * Makes a HTTP request to the protected web application.
 * /*ww w  .j  av  a  2 s . c  om*/
 * @param user
 * @param pass
 * @param expectedStatusCode
 * @throws Exception
 * @see org.jboss.as.test.integration.web.security.WebSecurityPasswordBasedBase#makeCall(java.lang.String, java.lang.String,
 *      int)
 */
@Override
protected void makeCall(String user, String pass, int expectedStatusCode) throws Exception {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    try {
        String req = url.toExternalForm() + "secured/";
        HttpGet httpget = new HttpGet(req);

        HttpResponse response = httpclient.execute(httpget);

        HttpEntity entity = response.getEntity();
        if (entity != null)
            EntityUtils.consume(entity);

        // We should get the Login Page
        StatusLine statusLine = response.getStatusLine();
        LOGGER.info("Login form get: " + statusLine);
        assertEquals(200, statusLine.getStatusCode());

        LOGGER.info("Initial set of cookies:");
        List<Cookie> cookies = httpclient.getCookieStore().getCookies();
        if (cookies.isEmpty()) {
            LOGGER.info("None");
        } else {
            for (int i = 0; i < cookies.size(); i++) {
                LOGGER.info("- " + cookies.get(i).toString());
            }
        }
        req = url.toExternalForm() + "secured/j_security_check";
        // We should now login with the user name and password
        HttpPost httpPost = new HttpPost(req);

        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("j_username", user));
        nvps.add(new BasicNameValuePair("j_password", pass));

        httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

        response = httpclient.execute(httpPost);
        entity = response.getEntity();
        if (entity != null)
            EntityUtils.consume(entity);

        statusLine = response.getStatusLine();

        // Post authentication - we have a 302
        assertEquals(302, statusLine.getStatusCode());
        Header locationHeader = response.getFirstHeader("Location");
        String location = locationHeader.getValue();

        HttpGet httpGet = new HttpGet(location);
        response = httpclient.execute(httpGet);

        entity = response.getEntity();
        if (entity != null)
            EntityUtils.consume(entity);

        LOGGER.info("Post logon cookies:");
        cookies = httpclient.getCookieStore().getCookies();
        if (cookies.isEmpty()) {
            LOGGER.info("None");
        } else {
            for (int i = 0; i < cookies.size(); i++) {
                LOGGER.info("- " + cookies.get(i).toString());
            }
        }

        // Either the authentication passed or failed based on the expected status code
        statusLine = response.getStatusLine();
        assertEquals(expectedStatusCode, statusLine.getStatusCode());
    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();
    }
}

From source file:org.jboss.as.test.integration.web.security.form.WebSecurityFORMTestCase.java

protected void makeCall(String user, String pass, int expectedStatusCode) throws Exception {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    try {//from  w  w  w.j  a v  a  2s . c om
        String req = url.toExternalForm() + "secured/";
        HttpGet httpget = new HttpGet(req);

        HttpResponse response = httpclient.execute(httpget);

        HttpEntity entity = response.getEntity();
        if (entity != null)
            EntityUtils.consume(entity);

        // We should get the Login Page
        StatusLine statusLine = response.getStatusLine();
        System.out.println("Login form get: " + statusLine);
        assertEquals(200, statusLine.getStatusCode());

        System.out.println("Initial set of cookies:");
        List<Cookie> cookies = httpclient.getCookieStore().getCookies();
        if (cookies.isEmpty()) {
            System.out.println("None");
        } else {
            for (int i = 0; i < cookies.size(); i++) {
                System.out.println("- " + cookies.get(i).toString());
            }
        }
        req = url.toExternalForm() + "secured/j_security_check";
        // We should now login with the user name and password
        HttpPost httpPost = new HttpPost(req);

        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("j_username", user));
        nvps.add(new BasicNameValuePair("j_password", pass));

        httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

        response = httpclient.execute(httpPost);
        entity = response.getEntity();
        if (entity != null)
            EntityUtils.consume(entity);

        statusLine = response.getStatusLine();

        // Post authentication - we have a 302
        assertEquals(302, statusLine.getStatusCode());
        Header locationHeader = response.getFirstHeader("Location");
        String location = locationHeader.getValue();

        HttpGet httpGet = new HttpGet(location);
        response = httpclient.execute(httpGet);

        entity = response.getEntity();
        if (entity != null)
            EntityUtils.consume(entity);

        System.out.println("Post logon cookies:");
        cookies = httpclient.getCookieStore().getCookies();
        if (cookies.isEmpty()) {
            System.out.println("None");
        } else {
            for (int i = 0; i < cookies.size(); i++) {
                System.out.println("- " + cookies.get(i).toString());
            }
        }

        // Either the authentication passed or failed based on the expected status code
        statusLine = response.getStatusLine();
        assertEquals(expectedStatusCode, statusLine.getStatusCode());
    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();
    }
}

From source file:org.transdroid.search.hdtorrents.HdTorrentsAdapter.java

/**
 * Attempts to log in to HD-Torrents.org with the given credentials. On success
 * the given DefaultHttpClient should hold all required cookies to access
 * the site.//from  w w w . ja v a2  s  . co m
 */
private void login(DefaultHttpClient client, String username, String password) throws Exception {
    Log.d(LOG_TAG, "Attempting to login.");

    HttpPost request = new HttpPost(LOGIN_URL);
    request.setEntity(new UrlEncodedFormEntity(
            Arrays.asList(new BasicNameValuePair[] { new BasicNameValuePair(LOGIN_POST_USERNAME, username),
                    new BasicNameValuePair(LOGIN_POST_PASSWORD, password) })));

    client.execute(request);

    // verify we have the cookies needed to log in
    boolean success = false, uid = false, pass = false, hash = false;
    for (Cookie cookie : client.getCookieStore().getCookies()) {
        if ("uid".equals(cookie.getName()))
            uid = true;
        if ("pass".equals(cookie.getName()))
            pass = true;
        if ("hashx".equals(cookie.getName()))
            hash = true;
    }

    // if we don't have the correct cookies, login failed. notify user with a toast and toss an exception.
    success = uid && pass && hash;
    if (!success) {
        Log.e(LOG_TAG, "Failed to log into HD-Torrents as '" + username
                + "'. Did not receive expected login cookies!");
        throw new LoginException("Failed to log into HD-Torrents as '" + username
                + "'. Did not receive expected login cookies!");
    }

    Log.d(LOG_TAG, "Successfully logged in to HD-Torrents");
}

From source file:com.da.img.SoStroyInfoList.java

private HttpGet executeLogin(DefaultHttpClient httpclient) throws IOException, ClientProtocolException {

    HttpGet httpget = new HttpGet(host_url);

    HttpResponse response = httpclient.execute(httpget);
    HttpEntity entity = response.getEntity();
    System.out.println("Login form get: " + response.getStatusLine());
    EntityUtils.consume(entity);// w ww.j a  v a2 s. c  o  m
    System.out.println("Initial set of cookies:");
    //List<Cookie> cookies = httpclient.getCookieStore().getCookies();

    List<Cookie> cookies = httpclient.getCookieStore().getCookies();
    if (cookies.isEmpty()) {
        System.out.println("None");
    } else {
        for (int i = 0; i < cookies.size(); i++) {
            System.out.println("- " + cookies.get(i).toString());
        }
    }
    //http://www.soraven.info/member/login.php
    HttpPost httpost = new HttpPost(host_url + "/common/include/login.php");
    Header header1 = new BasicHeader("Accept",
            "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,application/msword, */*");
    Header header2 = new BasicHeader("Referer", "http://www.soraven.info/index.php");
    List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    nvps.add(new BasicNameValuePair("p_userid", "bimohani"));
    nvps.add(new BasicNameValuePair("p_passwd", "cw8904"));
    httpost.setHeader(header1);
    httpost.setHeader(header2);
    httpost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));

    response = httpclient.execute(httpost);
    entity = response.getEntity();

    System.out.println("Login form get: " + response.getStatusLine());
    EntityUtils.consume(entity);

    System.out.println("Post logon cookies:");
    cookies = httpclient.getCookieStore().getCookies();
    if (cookies.isEmpty()) {
        System.out.println("None");
    } else {
        for (int i = 0; i < cookies.size(); i++) {

            System.out.println("- " + cookies.get(i).toString());
        }
    }
    return httpget;
}

From source file:org.jboss.as.test.integration.security.loginmodules.AbstractLoginModuleTest.java

protected void makeCall(String URL, String user, String pass, int expectedStatusCode) throws Exception {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    try {//from  ww  w.  j  a  v  a2 s  .  c o m
        HttpGet httpget = new HttpGet(URL);

        HttpResponse response = httpclient.execute(httpget);

        HttpEntity entity = response.getEntity();
        if (entity != null)
            EntityUtils.consume(entity);

        // We should get the Login Page
        StatusLine statusLine = response.getStatusLine();
        System.out.println("Login form get: " + statusLine);
        assertEquals(200, statusLine.getStatusCode());

        System.out.println("Initial set of cookies:");
        List<Cookie> cookies = httpclient.getCookieStore().getCookies();
        if (cookies.isEmpty()) {
            System.out.println("None");
        } else {
            for (int i = 0; i < cookies.size(); i++) {
                System.out.println("- " + cookies.get(i).toString());
            }
        }

        // We should now login with the user name and password
        HttpPost httpost = new HttpPost(URL + "/j_security_check");

        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("j_username", user));
        nvps.add(new BasicNameValuePair("j_password", pass));

        httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

        response = httpclient.execute(httpost);
        entity = response.getEntity();
        if (entity != null)
            EntityUtils.consume(entity);

        statusLine = response.getStatusLine();

        // Post authentication - we have a 302
        assertEquals(302, statusLine.getStatusCode());
        Header locationHeader = response.getFirstHeader("Location");
        String location = locationHeader.getValue();

        HttpGet httpGet = new HttpGet(location);
        response = httpclient.execute(httpGet);

        entity = response.getEntity();
        if (entity != null)
            EntityUtils.consume(entity);

        System.out.println("Post logon cookies:");
        cookies = httpclient.getCookieStore().getCookies();
        if (cookies.isEmpty()) {
            System.out.println("None");
        } else {
            for (int i = 0; i < cookies.size(); i++) {
                System.out.println("- " + cookies.get(i).toString());
            }
        }

        // Either the authentication passed or failed based on the expected status code
        statusLine = response.getStatusLine();
        assertEquals(expectedStatusCode, statusLine.getStatusCode());
    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();
    }
}

From source file:org.jboss.as.test.integration.security.CustomLoginModuleTestCase.java

protected void makeCall(String user, String pass, int expectedStatusCode) throws Exception {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    try {// w  w w. j a v  a  2  s. c o m
        HttpGet httpget = new HttpGet(URL);

        HttpResponse response = httpclient.execute(httpget);

        HttpEntity entity = response.getEntity();
        if (entity != null)
            EntityUtils.consume(entity);

        // We should get the Login Page
        StatusLine statusLine = response.getStatusLine();
        System.out.println("Login form get: " + statusLine);
        assertEquals(200, statusLine.getStatusCode());

        System.out.println("Initial set of cookies:");
        List<Cookie> cookies = httpclient.getCookieStore().getCookies();
        if (cookies.isEmpty()) {
            System.out.println("None");
        } else {
            for (int i = 0; i < cookies.size(); i++) {
                System.out.println("- " + cookies.get(i).toString());
            }
        }

        // We should now login with the user name and password
        HttpPost httpost = new HttpPost(URL + "/j_security_check");

        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("j_username", user));
        nvps.add(new BasicNameValuePair("j_password", pass));

        httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

        response = httpclient.execute(httpost);
        entity = response.getEntity();
        if (entity != null)
            EntityUtils.consume(entity);

        statusLine = response.getStatusLine();

        // Post authentication - we have a 302
        assertEquals(302, statusLine.getStatusCode());
        Header locationHeader = response.getFirstHeader("Location");
        String location = locationHeader.getValue();

        HttpGet httpGet = new HttpGet(location);
        response = httpclient.execute(httpGet);

        entity = response.getEntity();
        if (entity != null)
            EntityUtils.consume(entity);

        System.out.println("Post logon cookies:");
        cookies = httpclient.getCookieStore().getCookies();
        if (cookies.isEmpty()) {
            System.out.println("None");
        } else {
            for (int i = 0; i < cookies.size(); i++) {
                System.out.println("- " + cookies.get(i).toString());
            }
        }

        // Either the authentication passed or failed based on the expected status code
        statusLine = response.getStatusLine();
        assertEquals(expectedStatusCode, statusLine.getStatusCode());
    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();
    }
}

From source file:org.jboss.as.test.integration.security.loginmodules.CustomLoginModuleTestCase.java

protected void makeCall(String user, String pass, int expectedStatusCode) throws Exception {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    try {// ww w  .  j av a  2s .  c  o  m
        HttpGet httpget = new HttpGet(getURL());

        HttpResponse response = httpclient.execute(httpget);

        HttpEntity entity = response.getEntity();
        if (entity != null) {
            EntityUtils.consume(entity);
        }

        // We should get the Login Page
        StatusLine statusLine = response.getStatusLine();
        System.out.println("Login form get: " + statusLine);
        assertEquals(200, statusLine.getStatusCode());

        System.out.println("Initial set of cookies:");
        List<Cookie> cookies = httpclient.getCookieStore().getCookies();
        if (cookies.isEmpty()) {
            System.out.println("None");
        } else {
            for (int i = 0; i < cookies.size(); i++) {
                System.out.println("- " + cookies.get(i).toString());
            }
        }

        // We should now login with the user name and password
        HttpPost httpost = new HttpPost(getURL() + "j_security_check");

        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("j_username", user));
        nvps.add(new BasicNameValuePair("j_password", pass));

        httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

        response = httpclient.execute(httpost);
        entity = response.getEntity();
        if (entity != null) {
            EntityUtils.consume(entity);
        }

        statusLine = response.getStatusLine();

        // Post authentication - we have a 302
        assertEquals(302, statusLine.getStatusCode());
        Header locationHeader = response.getFirstHeader("Location");
        String location = locationHeader.getValue();

        HttpGet httpGet = new HttpGet(location);
        response = httpclient.execute(httpGet);

        entity = response.getEntity();
        if (entity != null) {
            EntityUtils.consume(entity);
        }

        System.out.println("Post logon cookies:");
        cookies = httpclient.getCookieStore().getCookies();
        if (cookies.isEmpty()) {
            System.out.println("None");
        } else {
            for (int i = 0; i < cookies.size(); i++) {
                System.out.println("- " + cookies.get(i).toString());
            }
        }

        // Either the authentication passed or failed based on the expected status code
        statusLine = response.getStatusLine();
        assertEquals(expectedStatusCode, statusLine.getStatusCode());
    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();
    }
}

From source file:com.servoy.extensions.plugins.http.HttpProvider.java

/**
 * Get cookie object from the specified client.
 * /*from  www .  ja  v a2  s.  co m*/
 * @deprecated Replaced by {@link HttpClient#setCookie(String,String)}.
 * 
 * @sample
 * var cookie = plugins.http.getHttpClientCookie('clientName', 'JSESSIONID');
 * if (cookie != null)
 * {
 *    // do something
 * }
 * else
 *    plugins.http.setHttpClientCookie('clientName', 'JSESSIONID', 'abc', 'localhost', '/', -1, false)
 *
 * @param clientName 
 * @param cookieName 
 */

@Deprecated
public Cookie js_getHttpClientCookie(String clientName, String cookieName) {
    DefaultHttpClient client = httpClients.get(clientName);
    if (client == null)
        return null;
    List<org.apache.http.cookie.Cookie> cookies = client.getCookieStore().getCookies();
    for (org.apache.http.cookie.Cookie element : cookies) {
        if (element.getName().equals(cookieName))
            return new com.servoy.extensions.plugins.http.Cookie(element);
    }
    return null;
}

From source file:messenger.PlurkApi.java

public String plurkAdd(String url) {
    PlurkApi p = PlurkApi.getInstance();
    if (cookiestore == null)
        p.login();//  w  w  w . j a v  a 2s .  c  o  m
    DefaultHttpClient httpclient = new DefaultHttpClient();
    if (use_proxy) {
        HttpHost proxy = new HttpHost(PROXY_NAME, PROXY_PORT);
        httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    }
    httpclient.setCookieStore(cookiestore);

    HttpResponse response = null;
    String responseString = null;
    try {
        String content = URLEncoder.encode(url, "UTF-8");
        HttpGet httpget = new HttpGet(getApiUri("/Timeline/plurkAdd?" + "api_key=" + API_KEY + "&" + "content="
                + content + "&" + "qualifier=" + "says" + "&" + "lang=tr_ch"));
        response = httpclient.execute(httpget);
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            cookiestore = httpclient.getCookieStore();
            responseString = EntityUtils.toString(response.getEntity());
            // pG^O 200 OK ~X
            // System.out.println(responseString);
            //
        } else {
            System.out.println(response.getStatusLine());
            responseString = EntityUtils.toString(response.getEntity());
            System.out.println(responseString);
        }
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        //e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        //e.printStackTrace();
    }
    httpclient.getConnectionManager().shutdown();
    return responseString;
}

From source file:com.servoy.extensions.plugins.http.HttpProvider.java

/**
 * Returns a Cookie array with all the cookies set on the specified client.
 * //w  w  w.  j av a2 s.  com
 * @deprecated Replaced by {@link HttpClient#setCookie(String,String)}.
 * 
 * @sample
 * var cookies = plugins.http.getHttpClientCookies('clientName')
 *
 * @param clientName 
 */
@Deprecated
public Cookie[] js_getHttpClientCookies(String httpClientName) {
    if (httpClientName == null || "".equals(httpClientName.trim())) {
        return new Cookie[] {};
    }
    DefaultHttpClient httpClient = httpClients.get(httpClientName);
    if (httpClient == null) {
        return new Cookie[] {};
    }
    List<org.apache.http.cookie.Cookie> cookies = httpClient.getCookieStore().getCookies();
    Cookie[] cookieObjects = new Cookie[cookies.size()];
    for (int i = 0; i < cookies.size(); i++) {
        cookieObjects[i] = new Cookie(cookies.get(i));
    }
    return cookieObjects;
}