Example usage for org.apache.http.impl.client BasicCookieStore BasicCookieStore

List of usage examples for org.apache.http.impl.client BasicCookieStore BasicCookieStore

Introduction

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

Prototype

public BasicCookieStore() 

Source Link

Usage

From source file:com.radicaldynamic.groupinform.application.Collect.java

/**
 * Shared HttpContext so a user doesn't have to re-enter login information
 * @return/*from   www.  ja v  a  2s  .  co  m*/
 */
public synchronized HttpContext getHttpContext() {
    if (localContext == null) {
        // set up one context for all HTTP requests so that authentication
        // and cookies can be retained.
        localContext = new SyncBasicHttpContext(new BasicHttpContext());

        // establish a local cookie store for this attempt at downloading...
        CookieStore cookieStore = new BasicCookieStore();
        localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

        // and establish a credentials provider.  Default is 7 minutes.
        CredentialsProvider credsProvider = new AgingCredentialsProvider(7 * 60 * 1000);
        localContext.setAttribute(ClientContext.CREDS_PROVIDER, credsProvider);
    }
    return localContext;
}

From source file:com.bright.json.JSonRequestor.java

public static String doRequest(String jsonReq, String myURL, List<Cookie> cookies) {
    try {/*from   ww  w  .ja  v  a 2s. co m*/

        /* HttpClient httpclient = new DefaultHttpClient(); */

        HttpClient httpclient = getNewHttpClient();
        CookieStore cookieStore = new BasicCookieStore();
        Cookie[] cookiearray = cookies.toArray(new Cookie[0]);
        cookieStore.addCookie(cookiearray[0]);
        HttpContext localContext = new BasicHttpContext();
        localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

        /* httpclient = WebClientDevWrapper.wrapClient(httpclient); */

        httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
        HttpParams params = httpclient.getParams();
        HttpConnectionParams.setConnectionTimeout(params, 1000);
        HttpConnectionParams.setSoTimeout(params, 1000);
        HttpPost httppost = new HttpPost(myURL);
        StringEntity stringEntity = new StringEntity(jsonReq);
        stringEntity.setContentType("application/json");
        httppost.setEntity(stringEntity);

        System.out.println("executing request " + httppost.getRequestLine()
                + System.getProperty("line.separator") + jsonReq);

        HttpResponse response = httpclient.execute(httppost, localContext);

        System.out.println(response + "\n");
        for (Cookie c : ((AbstractHttpClient) httpclient).getCookieStore().getCookies()) {
            System.out.println("\n Cookie: " + c.toString() + "\n");
        }

        HttpEntity resEntity = response.getEntity();

        System.out.println("----------------------------------------");
        System.out.println(response.getStatusLine());
        if (resEntity != null) {
            System.out.println("Response content length: " + resEntity.getContentLength());
            System.out.println("Chunked?: " + resEntity.isChunked());
            String message = new String(EntityUtils.toString(resEntity));
            System.out.println(message);
            return message;
        }
        EntityUtils.consume(resEntity);

    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

    return null;

}

From source file:org.wso2.carbon.registry.notes.test.PublisherNotesTestCase.java

@Test(groups = "wso2.greg", description = "Add Schema without name", dependsOnMethods = "addNoteTestCase")
public void getNoteTestCase() throws Exception {
    Thread.sleep(60000);/*from  w  w w .  j  a  v  a2 s .  c  o  m*/
    String session_id = authenticateJaggeryAPI();
    CookieStore cookieStore = new BasicCookieStore();
    HttpContext httpContext = new BasicHttpContext();
    httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

    HttpClient client = new DefaultHttpClient();

    URIBuilder builder = new URIBuilder();
    builder.setScheme("https").setHost("localhost:10343").setPath("/publisher/apis/assets")
            .setParameter("type", "note").setParameter("q",
                    "overview_resourcepath\":\"/_system/governance/trunk/soapservices/4.5.0/SOAPService1\"");
    URI uri = builder.build();
    HttpGet httpget = new HttpGet(uri);
    HttpResponse response = client.execute(httpget);
    assertEquals("OK", response.getStatusLine().getReasonPhrase());
}

From source file:com.mpower.mintel.android.application.MIntel.java

/**
 * Shared HttpContext so a user doesn't have to re-enter login information
 * /*w ww.  j a v  a2  s  .  c  o m*/
 * @return
 */
public synchronized HttpContext getHttpContext() {
    if (localContext == null) {
        // set up one context for all HTTP requests so that authentication
        // and cookies can be retained.
        localContext = new SyncBasicHttpContext(new BasicHttpContext());

        // establish a local cookie store for this attempt at downloading...
        CookieStore cookieStore = new BasicCookieStore();
        localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

        // and establish a credentials provider. Default is 7 minutes.
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        localContext.setAttribute(ClientContext.CREDS_PROVIDER, credsProvider);
    }
    return localContext;
}

From source file:com.hp.mqm.client.AbstractMqmRestClient.java

/**
 * Constructor for AbstractMqmRestClient.
 *
 * @param connectionConfig MQM connection configuration, Fields 'location', 'domain', 'project' and 'clientType' must not be null or empty.
 *///from   ww  w.j  a v a  2 s  .  c om
protected AbstractMqmRestClient(MqmConnectionConfig connectionConfig) {
    checkNotEmpty("Parameter 'location' must not be null or empty.", connectionConfig.getLocation());
    checkNotEmpty("Parameter 'sharedSpace' must not be null or empty.", connectionConfig.getSharedSpace());
    checkNotEmpty("Parameter 'clientType' must not be null or empty.", connectionConfig.getClientType());
    clientType = connectionConfig.getClientType();
    location = connectionConfig.getLocation();
    sharedSpace = connectionConfig.getSharedSpace();
    username = connectionConfig.getUsername();
    password = connectionConfig.getPassword();

    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    cm.setMaxTotal(20);
    cm.setDefaultMaxPerRoute(20);
    cookieStore = new BasicCookieStore();

    if (connectionConfig.getProxyHost() != null && !connectionConfig.getProxyHost().isEmpty()) {
        HttpHost proxy = new HttpHost(connectionConfig.getProxyHost(), connectionConfig.getProxyPort());

        RequestConfig requestConfig = RequestConfig.custom().setProxy(proxy)
                .setConnectTimeout(connectionConfig.getDefaultConnectionTimeout() != null
                        ? connectionConfig.getDefaultConnectionTimeout()
                        : DEFAULT_CONNECTION_TIMEOUT)
                .setSocketTimeout(connectionConfig.getDefaultSocketTimeout() != null
                        ? connectionConfig.getDefaultSocketTimeout()
                        : DEFAULT_SO_TIMEOUT)
                .build();

        if (connectionConfig.getProxyCredentials() != null) {
            AuthScope proxyAuthScope = new AuthScope(connectionConfig.getProxyHost(),
                    connectionConfig.getProxyPort());
            Credentials credentials = proxyCredentialsToCredentials(connectionConfig.getProxyCredentials());

            CredentialsProvider credsProvider = new BasicCredentialsProvider();
            credsProvider.setCredentials(proxyAuthScope, credentials);

            httpClient = HttpClients.custom().setConnectionManager(cm).setDefaultCookieStore(cookieStore)
                    .setDefaultCredentialsProvider(credsProvider).setDefaultRequestConfig(requestConfig)
                    .build();
        } else {
            httpClient = HttpClients.custom().setConnectionManager(cm).setDefaultCookieStore(cookieStore)
                    .setDefaultRequestConfig(requestConfig).build();
        }
    } else {
        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(connectionConfig.getDefaultConnectionTimeout() != null
                        ? connectionConfig.getDefaultConnectionTimeout()
                        : DEFAULT_CONNECTION_TIMEOUT)
                .setSocketTimeout(connectionConfig.getDefaultSocketTimeout() != null
                        ? connectionConfig.getDefaultSocketTimeout()
                        : DEFAULT_SO_TIMEOUT)
                .build();
        httpClient = HttpClients.custom().setConnectionManager(cm).setDefaultCookieStore(cookieStore)
                .setDefaultRequestConfig(requestConfig).build();
    }
}

From source file:com.gsma.mobileconnect.utils.RestClient.java

/**
 * Build a cookie store that contains the specified cookies.
 * <p>//from w  w w  .j  a  v  a2s  .c o m
 * The domain of the cookies is set to the specified host.
 *
 * @param host The domain of the cookies.
 * @param cookiesToProxy The cookies to add to the store.
 * @return The cookie store.
 */
private CookieStore buildCookieStore(String host, List<KeyValuePair> cookiesToProxy) {
    CookieStore cookieStore = new BasicCookieStore();

    if (null == cookiesToProxy) {
        return cookieStore;
    }

    for (KeyValuePair cookieToProxy : cookiesToProxy) {
        BasicClientCookie cookie = new BasicClientCookie(cookieToProxy.getKey(), cookieToProxy.getValue());
        cookie.setDomain(host);
        cookieStore.addCookie(cookie);
    }

    return cookieStore;
}

From source file:com.googlesource.gerrit.plugins.hooks.rtc.network.RTCClient.java

private void setCookieStore() {
    cookieStore = new BasicCookieStore();
    httpclient.setCookieStore(cookieStore);
}

From source file:net.bither.http.BaseHttpResponse.java

private BasicCookieStore getCookieStore(String domain) {
    BasicCookieStore cookieStore = null;
    if (cookieCache.containsKey(domain)) {
        cookieStore = cookieCache.get(domain);
    } else {//from  w  ww  . j  a  v  a2s . c o m
        PersistentCookieStore persistentCookieStore = PersistentCookieStore.getInstance();
        cookieStore = new BasicCookieStore();
        for (Cookie cookie : persistentCookieStore.getCookies()) {
            BasicClientCookie basicClientCookie = new BasicClientCookie(cookie.getName(), cookie.getValue());
            basicClientCookie.setDomain(domain);
            basicClientCookie.setExpiryDate(cookie.getExpiryDate());
            basicClientCookie.setVersion(cookie.getVersion());
            basicClientCookie.setPath(cookie.getPath());
            cookieStore.addCookie(basicClientCookie);
        }
        cookieCache.put(domain, cookieStore);
    }
    return cookieStore;
}

From source file:com.lehman.ic9.net.httpClient.java

/**
 * Default constructor takes the reference to the script engine to reference 
 * later and the request URL string.//from  ww w .  j  av  a 2  s . c  o m
 * @param Eng is an ic9engine instance.
 * @param JsObj is a Map of String - Object with the JS instance of the httpClient for later reference.
 * @param ReqUrl is a String with the request URL.
 * @throws MalformedURLException Exception
 */
public httpClient(ic9engine Eng, Map<String, Object> JsObj, String ReqUrl) throws MalformedURLException {
    // Set engine.
    this.eng = Eng;
    this.jsobj = JsObj;

    this.cs = new BasicCookieStore();
    this.cp = new BasicCredentialsProvider();
    this.rcb = RequestConfig.custom();
    this.u = new URL(ReqUrl);
}

From source file:org.openlmis.UiUtils.HttpClient.java

public void createContext() {
    this.httpClient = new DefaultHttpClient();
    CookieStore cookieStore = new BasicCookieStore();
    httpContext = new BasicHttpContext();
    httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
}