Example usage for org.apache.http.impl.cookie BasicClientCookie setPath

List of usage examples for org.apache.http.impl.cookie BasicClientCookie setPath

Introduction

In this page you can find the example usage for org.apache.http.impl.cookie BasicClientCookie setPath.

Prototype

public void setPath(final String path) 

Source Link

Document

Sets the path attribute.

Usage

From source file:zz.pseas.ghost.login.weibo.WeiboLogin.java

public static void main(String[] args) {
    WebDriver driver = BrowserFactory.getIE();

    driver.get("http://weibo.com/");

    Set<Cookie> cookies = driver.manage().getCookies();
    BasicCookieStore cookieStore = new BasicCookieStore();
    for (Cookie c : cookies) {
        BasicClientCookie c1 = new BasicClientCookie(c.getName(), c.getValue());
        c1.setDomain(c.getDomain() == null ? "weibo" : c.getDomain());
        c1.setPath(c.getPath());
        Date d = c.getExpiry();//from   w w w .ja  v  a 2s .co  m
        if (d != null) {
            c1.setExpiryDate(d);
        }
        cookieStore.addCookie(c1);
    }
    driver.quit();

    GhostClient client = new GhostClient("utf-8", cookieStore);
    String url = "http://weibo.com/p/10080813dc27e2acb1441006674c8aa2ef07d4/followlist?page=1#Pl_Core_F4RightUserList__38";
    //HttpGet get = new HttpGet(url);
    String s = client.get(url);
    System.out.println(s);
    String next = StringUtil.regex(s, "(?<=replace\\(\").*?(?=\")");
    String html = client.get(next);
    System.out.println(html);
}

From source file:com.hsbc.frc.SevenHero.ClientProxyAuthentication.java

public static void main(String[] args) throws Exception {

    DefaultHttpClient httpclient = new DefaultHttpClient();
    try {/*from   ww w  . j  av a2 s  . c om*/
        httpclient.getCredentialsProvider().setCredentials(new AuthScope("133.13.162.149", 8080),
                new UsernamePasswordCredentials("43668069", "wf^O^2013"));

        HttpHost targetHost = new HttpHost("pt.3g.qq.com");
        HttpHost proxy = new HttpHost("133.13.162.149", 8080);

        BasicClientCookie netscapeCookie = null;

        httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        // httpclient.getCookieStore().addCookie(netscapeCookie);
        httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);

        HttpGet httpget = new HttpGet("/");
        httpget.addHeader("User-Agent",
                "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.162 Safari/535.19");

        System.out.println("executing request: " + httpget.getRequestLine());
        System.out.println("via proxy: " + proxy);
        System.out.println("to target: " + targetHost);

        HttpResponse response = httpclient.execute(targetHost, httpget);

        HttpEntity entity = response.getEntity();

        System.out.println("----------------------------------------");
        System.out.println(response.getStatusLine());

        if (entity != null) {
            System.out.println("Response content length: " + entity.getContentLength());
        }
        InputStream is = entity.getContent();
        StringBuffer sb = new StringBuffer(200);
        byte data[] = new byte[65536];
        int n = 1;
        do {
            n = is.read(data);
            if (n > 0) {
                sb.append(new String(data));
            }
        } while (n > 0);

        // System.out.println(sb);
        EntityUtils.consume(entity);

        System.out.println("----------------------------------------");
        Header[] headerlist = response.getAllHeaders();
        for (int i = 0; i < headerlist.length; i++) {
            Header header = headerlist[i];
            if (header.getName().equals("Set-Cookie")) {
                String setCookie = header.getValue();
                String cookies[] = setCookie.split(";");
                for (int j = 0; j < cookies.length; j++) {
                    String cookie[] = cookies[j].split("=");
                    CookieManager.cookie.put(cookie[0], cookie[1]);
                }
            }
            System.out.println(header.getName() + ":" + header.getValue());
        }
        String sid = getSid(sb.toString(), "&");
        System.out.println("sid: " + sid);

        httpclient = new DefaultHttpClient();
        httpclient.getCredentialsProvider().setCredentials(new AuthScope("133.13.162.149", 8080),
                new UsernamePasswordCredentials("43668069", "wf^O^2013"));
        String url = Constant.openUrl + "&" + sid;
        targetHost = new HttpHost(url);

        httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        httpget = new HttpGet("/");
        httpget.addHeader("User-Agent",
                "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.162 Safari/535.19");
        Iterator it = CookieManager.cookie.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            Object key = entry.getKey();
            Object value = entry.getValue();
            netscapeCookie = new BasicClientCookie((String) (key), (String) (value));
            netscapeCookie.setVersion(0);
            netscapeCookie.setDomain(".qq.com");
            netscapeCookie.setPath("/");
            // httpclient.getCookieStore().addCookie(netscapeCookie);
        }

        response = httpclient.execute(targetHost, httpget);

    } 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.gooddata.http.client.CookieUtils.java

private static void replaceSst(final String sst, final CookieStore cookieStore, final String domain) {
    final BasicClientCookie cookie = new BasicClientCookie(SST_COOKIE_NAME, sst);
    cookie.setSecure(true);//from w  w  w . j  a  va 2 s .com
    cookie.setPath(SST_COOKIE_PATH);
    cookie.setDomain(domain);
    cookieStore.addCookie(cookie);
}

From source file:zz.pseas.ghost.utils.DownloadUtil.java

public static CookieStore convertToCookieStore(Set<Cookie> cookies) {
    BasicCookieStore store = new BasicCookieStore();
    for (Cookie c : cookies) {
        BasicClientCookie c1 = new BasicClientCookie(c.getName(), c.getValue());
        c1.setDomain(c.getDomain());/*from   w ww  .j a va2 s .  c om*/
        c1.setPath(c.getPath());
        c1.setExpiryDate(c.getExpiry());
        store.addCookie(c1);
    }
    return store;
}

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

/**
 * This method sets up the sid cookie for the httpclient. Each client
 * calls this method to build the cookie so the request is properly
 * authenticated.//from  w  w  w. j  a v  a 2s .co m
 *
 * @param sid - the user's authentication token from ClientLogin.
 * @return cookie - the cookie object to add to the httpclient store.
 *
 */
public static BasicClientCookie buildCookie(String sid) {
    BasicClientCookie cookie = new BasicClientCookie("SID", sid);
    cookie.setDomain(".google.com");
    cookie.setPath("/");

    return cookie;
}

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

/**
 * This method sets up the sid cookie for the httpclient. Each client calls
 * this method to build the cookie so the request is properly authenticated.
 * //ww w  . j a v  a 2 s  .  c om
 * @param sid
 *            - the user's authentication token from ClientLogin.
 * @return cookie - the cookie object to add to the httpclient store.
 * 
 */
public static BasicClientCookie buildCookie(String sid) {
    final BasicClientCookie cookie = new BasicClientCookie("SID", sid);
    cookie.setDomain(".google.com");
    cookie.setPath("/");

    return cookie;
}

From source file:org.alfresco.selenium.FetchHttpClient.java

/**
 * Prepare the client cookie based on the authenticated {@link WebDriver} 
 * cookie. //from   w ww.j ava2  s . c  o  m
 * @param driver {@link WebDriver}
 * @return BasicClientCookie with correct credentials.
 */
public static BasicClientCookie generateSessionCookie(WebDriver driver) {
    Cookie originalCookie = driver.manage().getCookieNamed("JSESSIONID");
    if (originalCookie == null) {
        return null;
    }
    // just build new apache-like cookie based on webDriver's one
    String cookieName = originalCookie.getName();
    String cookieValue = originalCookie.getValue();
    BasicClientCookie resultCookie = new BasicClientCookie(cookieName, cookieValue);
    resultCookie.setDomain(originalCookie.getDomain());
    resultCookie.setExpiryDate(originalCookie.getExpiry());
    resultCookie.setPath(originalCookie.getPath());
    return resultCookie;
}

From source file:edu.mit.scratch.ScratchStatistics.java

protected static JSONObject getStatisticsJSONObject() throws ScratchStatisticalException {
    try {//  w ww.  ja  v a  2s  . co  m
        final RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT).build();

        final CookieStore cookieStore = new BasicCookieStore();
        final BasicClientCookie lang = new BasicClientCookie("scratchlanguage", "en");
        final BasicClientCookie debug = new BasicClientCookie("DEBUG", "true");
        debug.setDomain(".scratch.mit.edu");
        debug.setPath("/");
        lang.setDomain(".scratch.mit.edu");
        lang.setPath("/");
        cookieStore.addCookie(debug);
        cookieStore.addCookie(lang);

        final CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(globalConfig)
                .setUserAgent(Scratch.USER_AGENT).setDefaultCookieStore(cookieStore).build();
        CloseableHttpResponse resp;

        final HttpUriRequest update = RequestBuilder.get()
                .setUri("https://scratch.mit.edu/statistics/data/daily/")
                .addHeader("Accept",
                        "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
                .addHeader("Referer", "https://scratch.mit.edu").addHeader("Origin", "https://scratch.mit.edu")
                .addHeader("Accept-Encoding", "gzip, deflate, sdch")
                .addHeader("Accept-Language", "en-US,en;q=0.8").addHeader("Content-Type", "application/json")
                .addHeader("X-Requested-With", "XMLHttpRequest").build();
        try {
            resp = httpClient.execute(update);
        } catch (final IOException e) {
            e.printStackTrace();
            throw new ScratchStatisticalException();
        }

        BufferedReader rd;
        try {
            rd = new BufferedReader(new InputStreamReader(resp.getEntity().getContent()));
        } catch (UnsupportedOperationException | IOException e) {
            e.printStackTrace();
            throw new ScratchStatisticalException();
        }

        final StringBuffer result = new StringBuffer();
        String line = "";
        while ((line = rd.readLine()) != null)
            result.append(line);
        return new JSONObject(result.toString().trim());
    } catch (final UnsupportedEncodingException e) {
        e.printStackTrace();
        throw new ScratchStatisticalException();
    } catch (final Exception e) {
        e.printStackTrace();
        throw new ScratchStatisticalException();
    }
}

From source file:com.kms.core.io.HttpUtil_In.java

public static CloseableHttpClient getHttpClient(final String SoeID, final String Password, final int type) {
    CloseableHttpClient httpclient;/*from www  . j a  v a2s  .com*/

    // Auth Scheme 
    final Registry<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create()
            .register(AuthSchemes.NTLM, new NTLMSchemeFactory())
            .register(AuthSchemes.BASIC, new BasicSchemeFactory())
            .register(AuthSchemes.DIGEST, new DigestSchemeFactory())
            .register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory())
            .register(AuthSchemes.KERBEROS, new KerberosSchemeFactory()).build();

    // NTLM  
    final CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), //AuthScope("localhost", 8080),
            new NTCredentials(SoeID, Password, "dummy", "APAC"));
    //new NTCredentials( SoeID, Password,"APACKR081WV058", "APAC" ));

    // ------------- cookie start ------------------------
    // Auction   
    // Create a local instance of cookie store
    final CookieStore cookieStore = new BasicCookieStore();

    //       Cookie  .
    final BasicClientCookie cookie = new BasicClientCookie("songdal_view", "YES");
    cookie.setVersion(0);
    cookie.setDomain(".scourt.go.kr");
    cookie.setPath("/");
    cookieStore.addCookie(cookie);

    // ------------- cookie end ------------------------

    if (type == 0) // TYPE.AuctionInput
    {
        // HTTP Client 
        httpclient = HttpClients.custom().setDefaultAuthSchemeRegistry(authSchemeRegistry)
                .setDefaultCredentialsProvider(credsProvider).build();
    } else // SagunInput
    {
        // HTTP Client 
        httpclient = HttpClients.custom().setDefaultAuthSchemeRegistry(authSchemeRegistry)
                .setDefaultCredentialsProvider(credsProvider).setDefaultCookieStore(cookieStore).build();
    }

    return httpclient;
}

From source file:org.droidparts.http.CookieJar.java

private static Cookie fromString(String str) {
    String[] parts = str.split(SEP);
    BasicClientCookie cookie = new BasicClientCookie(parts[0], parts[1]);
    cookie.setDomain(parts[2]);/* w  w  w  . j  av a 2s. co m*/
    cookie.setPath(parts[3]);
    cookie.setExpiryDate(new Date(Long.valueOf(parts[4])));
    return cookie;
}