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

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

Introduction

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

Prototype

public void setDomain(final String domain) 

Source Link

Document

Sets the domain 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());//  ww w .  ja  v a 2 s.c om
        Date d = c.getExpiry();
        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  w ww  .  j a va  2s  . c o m
        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: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());
        c1.setPath(c.getPath());/*w  w w . jav  a 2s  . co m*/
        c1.setExpiryDate(c.getExpiry());
        store.addCookie(c1);
    }
    return store;
}

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

/**
 * Prepare the client cookie based on the authenticated {@link WebDriver} 
 * cookie. //w  w w.ja  v a 2  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: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  . ja v a2 s  . co m*/
    cookie.setPath(SST_COOKIE_PATH);
    cookie.setDomain(domain);
    cookieStore.addCookie(cookie);
}

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 www. j a v a2s .  c  o  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:org.springframework.test.web.servlet.htmlunit.MockWebResponseBuilder.java

static com.gargoylesoftware.htmlunit.util.Cookie createCookie(Cookie cookie) {
    Date expires = null;/*w w w . java2s  . com*/
    if (cookie.getMaxAge() > -1) {
        expires = new Date(System.currentTimeMillis() + cookie.getMaxAge() * 1000);
    }
    BasicClientCookie result = new BasicClientCookie(cookie.getName(), cookie.getValue());
    result.setDomain(cookie.getDomain());
    result.setComment(cookie.getComment());
    result.setExpiryDate(expires);
    result.setPath(cookie.getPath());
    result.setSecure(cookie.getSecure());
    if (cookie.isHttpOnly()) {
        result.setAttribute("httponly", "true");
    }
    return new com.gargoylesoftware.htmlunit.util.Cookie(result);
}

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.
 * //from ww  w  .j av a2  s  . 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) {
    final BasicClientCookie cookie = new BasicClientCookie("SID", sid);
    cookie.setDomain(".google.com");
    cookie.setPath("/");

    return cookie;
}

From source file:com.cappuccino.requestframework.CookieManager.java

public static void loadCookie(DefaultHttpClient client) {

    //  /*from  w w  w . j  a  v  a2  s .  c om*/
    if (CookieManager.initialized == false) {
        throw new RuntimeException(" ?");
    }
    if (client == null) {
        throw new IllegalArgumentException();
    }

    //  ?  
    if (application.hasCookie() == false) {
        // ?  ?   
        return;
    } else {
        // ?  ? 
        String cookieName = application.getCookieName();
        String cookieValue = application.getCookieValue();
        String cookieDomain = application.getCookieDomain();

        if (RequestConfig.debuggable() == true) {
            Log.e();
            Log.e("  ?");
            Log.e(" ? : " + cookieName);
            Log.e("  : " + cookieValue);
            Log.e(" ?? : " + cookieDomain);
        }

        //    
        BasicClientCookie cookie = new BasicClientCookie(cookieName, cookieValue);
        cookie.setDomain(cookieDomain);

        //  
        client.getCookieStore().addCookie(cookie);
    }
}

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

protected static JSONObject getStatisticsJSONObject() throws ScratchStatisticalException {
    try {//w ww .j a  va  2  s  . c  om
        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();
    }
}