當前位置: 首頁>>代碼示例>>Java>>正文


Java CookieStore.addCookie方法代碼示例

本文整理匯總了Java中org.apache.http.client.CookieStore.addCookie方法的典型用法代碼示例。如果您正苦於以下問題:Java CookieStore.addCookie方法的具體用法?Java CookieStore.addCookie怎麽用?Java CookieStore.addCookie使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.apache.http.client.CookieStore的用法示例。


在下文中一共展示了CookieStore.addCookie方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: fillSession

import org.apache.http.client.CookieStore; //導入方法依賴的package包/類
/**
 * 將登錄結果保存到給定會話對象中,並激活給定會話對象。
 * @param session 會話對象
 * @throws IllegalStateException 登錄未成功時拋出
 * @throws NetworkException 在發生網絡問題時拋出
 */
public void fillSession(Session session) throws BiliLiveException {
    try {
        if (status != SUCCESS) throw new IllegalStateException("Bad status: " + status);

        Set<Cookie> cookies = webClient.getCookieManager().getCookies();

        CookieStore store = session.getCookieStore();
        store.clear();
        for (Cookie cookie : cookies) {
            store.addCookie(cookie.toHttpClient()); // HtmlUnit Cookie to HttpClient Cookie
        }

        if (getStatus() == SUCCESS) session.activate();
    } catch (IOException ex) {
        throw new NetworkException("IO Exception", ex);
    }
}
 
開發者ID:cqjjjzr,項目名稱:BiliLiveLib,代碼行數:24,代碼來源:SessionLoginHelper.java

示例2: getuCookie

import org.apache.http.client.CookieStore; //導入方法依賴的package包/類
public static CookieStore getuCookie() {
    CookieStore uCookie = new BasicCookieStore();
    try {
        String COOKIE_S_LINKDATA = LemallPlatform.getInstance().getCookieLinkdata();
        if (!TextUtils.isEmpty(COOKIE_S_LINKDATA)) {
            String[] cookies = COOKIE_S_LINKDATA.split("&");
            for (String item : cookies) {
                String[] keyValue = item.split(SearchCriteria.EQ);
                if (keyValue.length == 2) {
                    if (OtherUtil.isContainsChinese(keyValue[1])) {
                        keyValue[1] = URLEncoder.encode(keyValue[1], "UTF-8");
                    }
                    BasicClientCookie cookie = new BasicClientCookie(keyValue[0], keyValue[1]);
                    cookie.setVersion(0);
                    cookie.setDomain(".lemall.com");
                    cookie.setPath("/");
                    uCookie.addCookie(cookie);
                }
            }
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return uCookie;
}
 
開發者ID:JackChan1999,項目名稱:letv,代碼行數:26,代碼來源:AsyncHttpClient.java

示例3: fromBase64

import org.apache.http.client.CookieStore; //導入方法依賴的package包/類
@SuppressWarnings("unchecked")
public static void fromBase64(@NotNull Session session, @NonNls @NotNull String base64) {
    try {
        ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64.getDecoder().decode(base64.getBytes()));
        ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
        List<Cookie> cookies = (List<Cookie>) objectInputStream.readObject();

        CookieStore store = session.getCookieStore();
        store.clear();
        for (Cookie cookie : cookies) {
            store.addCookie(cookie);
        }
    } catch (IOException | ClassNotFoundException e) {
        throw new AssertionError();
    }
}
 
開發者ID:cqjjjzr,項目名稱:BiliLiveLib,代碼行數:17,代碼來源:SessionPersistenceHelper.java

示例4: createContext

import org.apache.http.client.CookieStore; //導入方法依賴的package包/類
@Override
public BasicHttpContext createContext(final HttpHost targetHost) {

    final CookieStore cookieStore = new BasicCookieStore();

    final BasicClientCookie clientCookie =
            new BasicClientCookie(cookie.getName(), cookie.getValue());
    clientCookie.setDomain(targetHost.getHostName());
    clientCookie.setPath("/");
    cookieStore.addCookie(clientCookie);

    final BasicHttpContext context = new BasicHttpContext();
    context.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);

    return context;
}
 
開發者ID:NovaOrdis,項目名稱:playground,代碼行數:17,代碼來源:UseCookieConfigurator.java

示例5: createHttpClient

import org.apache.http.client.CookieStore; //導入方法依賴的package包/類
/**
 * 獲取Http客戶端連接對象
 * @param timeOut 超時時間
 * @param proxy   代理
 * @param cookie  Cookie
 * @return Http客戶端連接對象
 */
public CloseableHttpClient createHttpClient(int timeOut,HttpHost proxy,BasicClientCookie cookie) {

    // 創建Http請求配置參數
    RequestConfig.Builder builder = RequestConfig.custom()
            // 獲取連接超時時間
            .setConnectionRequestTimeout(timeOut)
            // 請求超時時間
            .setConnectTimeout(timeOut)
            // 響應超時時間
            .setSocketTimeout(timeOut)
            .setCookieSpec(CookieSpecs.STANDARD);

    if (proxy!=null) {
        builder.setProxy(proxy);
    }

    RequestConfig requestConfig = builder.build();

    // 創建httpClient
    HttpClientBuilder httpClientBuilder = HttpClients.custom();

    httpClientBuilder
            // 把請求相關的超時信息設置到連接客戶端
            .setDefaultRequestConfig(requestConfig)
            // 把請求重試設置到連接客戶端
            .setRetryHandler(new RetryHandler())
            // 配置連接池管理對象
            .setConnectionManager(connManager);

    if (cookie!=null) {
        CookieStore cookieStore = new BasicCookieStore();
        cookieStore.addCookie(cookie);
        httpClientBuilder.setDefaultCookieStore(cookieStore);
    }

    return httpClientBuilder.build();
}
 
開發者ID:fengzhizi715,項目名稱:NetDiscovery,代碼行數:45,代碼來源:HttpManager.java

示例6: createHttpClient

import org.apache.http.client.CookieStore; //導入方法依賴的package包/類
/**
 * 獲取Http客戶端連接對象
 * @param timeOut 超時時間
 * @param proxy   代理
 * @param cookie  Cookie
 * @return Http客戶端連接對象
 */
private CloseableHttpClient createHttpClient(int timeOut,HttpHost proxy,BasicClientCookie cookie) {

    // 創建Http請求配置參數
    RequestConfig.Builder builder = RequestConfig.custom()
            // 獲取連接超時時間
            .setConnectionRequestTimeout(timeOut)
            // 請求超時時間
            .setConnectTimeout(timeOut)
            // 響應超時時間
            .setSocketTimeout(timeOut)
            .setCookieSpec(CookieSpecs.STANDARD);

    if (proxy!=null) {
        builder.setProxy(proxy);
    }

    RequestConfig requestConfig = builder.build();

    // 創建httpClient
    HttpClientBuilder httpClientBuilder = HttpClients.custom();

    httpClientBuilder
            // 把請求相關的超時信息設置到連接客戶端
            .setDefaultRequestConfig(requestConfig)
            // 把請求重試設置到連接客戶端
            .setRetryHandler(new RetryHandler())
            // 配置連接池管理對象
            .setConnectionManager(connManager);

    if (cookie!=null) {
        CookieStore cookieStore = new BasicCookieStore();
        cookieStore.addCookie(cookie);
        httpClientBuilder.setDefaultCookieStore(cookieStore);
    }

    return httpClientBuilder.build();
}
 
開發者ID:fengzhizi715,項目名稱:PicCrawler,代碼行數:45,代碼來源:HttpManager.java

示例7: generateCookies

import org.apache.http.client.CookieStore; //導入方法依賴的package包/類
/**
 * 根據配置自動生成需要的Cookies
 * @param httpClient
 * @param setting
 */
public void generateCookies(HttpClientBuilder httpClient, Setting setting){
    CookieStore cookieStore = new BasicCookieStore();
    for(Map.Entry<String,String> cookieEntry : setting.getCookies().entrySet()){
        BasicClientCookie cookie = new BasicClientCookie(cookieEntry.getKey(), cookieEntry.getValue());
        cookie.setDomain(setting.getDomain());
        cookie.setPath("/");
        cookieStore.addCookie(cookie);
         httpClient.setDefaultCookieStore(cookieStore).build();
    }
}
 
開發者ID:java-webbee,項目名稱:webBee,代碼行數:16,代碼來源:HttpClientPool.java

示例8: getCookieHttpClient

import org.apache.http.client.CookieStore; //導入方法依賴的package包/類
/**
 * Get a cookie from Google and place it in a ClosableHttpClient
 * @return An instance of CloseableHttpClient that is critical to this Class
 * @throws IOException When URL connection is improperly formed
 */
private static CloseableHttpClient getCookieHttpClient() throws IOException {

	/*
	 * Tutorial: http://www.hccp.org/java-net-cookie-how-to.html
	 */
	URL myUrl = new URL("https://trends.google.com");
	URLConnection urlConn = myUrl.openConnection();
	urlConn.connect();

	String headerName = null;
	for(int i=1; (headerName = urlConn.getHeaderFieldKey(i)) != null; i++)
		if(headerName.equals("Set-Cookie"))
			cookieString = urlConn.getHeaderField(i);
	cookieString = cookieString.substring(0, cookieString.indexOf(";"));

	/*
	 * Tutorial: http://hc.apache.org/httpcomponents-client-ga/tutorial/html/statemgmt.html#d5e499
	 */
	CookieStore cookieStore = new BasicCookieStore();
	BasicClientCookie cookie = new BasicClientCookie("Cookie", cookieString);
	cookie.setDomain(".google.com");
	cookie.setPath("/trends");
	cookieStore.addCookie(cookie);
	return HttpClients.custom().setDefaultCookieStore(cookieStore).build();

}
 
開發者ID:Jaewan-Yun,項目名稱:Cryptocurrency-Java-Wrappers,代碼行數:32,代碼來源:GoogleTrends.java

示例9: addProxyCookie

import org.apache.http.client.CookieStore; //導入方法依賴的package包/類
private void addProxyCookie(CookieStore cookieStore) {
  BasicClientCookie proxyCookie = new BasicClientCookie(
      properties.getProperty(ConfigKeys.WEBDRIVER_PROXY_COOKIE_NAME),
      properties.getProperty(ConfigKeys.WEBDRIVER_PROXY_COOKIE_VALUE));
  proxyCookie.setDomain(properties.getProperty(ConfigKeys.WEBDRIVER_PROXY_COOKIE_DOMAIN));
  proxyCookie.setPath("/");
  cookieStore.addCookie(proxyCookie);
}
 
開發者ID:Cognifide,項目名稱:bobcat,代碼行數:9,代碼來源:AemAuthCookieFactory.java

示例10: clearMessages

import org.apache.http.client.CookieStore; //導入方法依賴的package包/類
public void clearMessages() throws ScratchUserException {
    try {
        final RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT).build();
        
        final CookieStore cookieStore = new BasicCookieStore();
        final BasicClientCookie lang = new BasicClientCookie("scratchlanguage", "en");
        lang.setDomain(".scratch.mit.edu");
        lang.setPath("/");
        cookieStore.addCookie(lang);
        
        final CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(globalConfig)
                .setUserAgent(Scratch.USER_AGENT).setDefaultCookieStore(cookieStore).build();
        final HttpUriRequest update = RequestBuilder.get()
                .setUri("https://scratch.mit.edu/messages/ajax/messages-clear/")
                .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")
                .addHeader("Cookie",
                        "scratchsessionsid=" + this.session.getSessionID() + "; scratchcsrftoken="
                                + this.session.getCSRFToken())
                .addHeader("X-CSRFToken", this.session.getCSRFToken()).build();
        
        httpClient.execute(update);
    } catch (final Exception e) {
        throw new ScratchUserException();
    }
}
 
開發者ID:Arinerron,項目名稱:ScratchAPI,代碼行數:29,代碼來源:ScratchUserManager.java

示例11: setCookie

import org.apache.http.client.CookieStore; //導入方法依賴的package包/類
/**
 * 設置cookie
 * @param cookies
 */
public void setCookie(Map<String, String> cookies) {
	CookieStore cookieStore = new BasicCookieStore();
	for (Map.Entry<String, String> each : cookies.entrySet()) {
		cookieStore.addCookie(new BasicClientCookie(each.getKey(), each.getValue()));
	}
	httpAsyncClientBuilder.setDefaultCookieStore(cookieStore);
}
 
開發者ID:luohaha,項目名稱:jlitespider,代碼行數:12,代碼來源:AsyncNetwork.java

示例12: pushCookie

import org.apache.http.client.CookieStore; //導入方法依賴的package包/類
public static void pushCookie(Context context, String cookieStr) {
        if (StringUtils.isEmptyOrNullOrNullStr(cookieStr))
            return;

        com.youzu.clan.base.net.CookieManager cm = com.youzu.clan.base.net.CookieManager.getInstance();
//        PersistentCookieStore cookieStore = new PersistentCookieStore(context);
        CookieStore cookieStore = cm.getCookieStore(context);

//        ClanBaseUtils.printCookieStore(cookieStore);

        String[] kvs = cookieStr.split("; ");

        for (String s : kvs) {
            String[] kv = s.split("=");
            String key = "";
            String value = "";
            if (kv.length >= 1) {
                key = kv[0];
            }
            if (kv.length >= 1) {
                value = kv[1];
            }
            Log.e("APP", "key:" + key + " value:" + value);
            MyCooike cookie = new MyCooike(key, value);
//            cm.getCookieStore(context).addCookie(cookie);
            cookie.setDomain(ClanUtils.getDomainName(Url.DOMAIN));
            cookie.setPath("/");
            cookieStore.addCookie(cookie);
        }

        HttpUtils httpUtils = new HttpUtils(60 * 1000);
        httpUtils.configCookieStore(cookieStore);

    }
 
開發者ID:BigAppOS,項目名稱:BigApp_Discuz_Android,代碼行數:35,代碼來源:ClanUtils.java

示例13: createAndAddCookiesOnStoreForHeader

import org.apache.http.client.CookieStore; //導入方法依賴的package包/類
/**
 * This method creates a cookie for every {@link HeaderElement} of the {@link Header} given as parameter.
 * Then, it adds this newly created cookie into the {@link CookieStore} provided as parameter.
 */
protected void createAndAddCookiesOnStoreForHeader(CookieStore cookieStore, Header header) {
    for (HeaderElement element : header.getElements()) {
        BasicClientCookie cookie = createCookieForHeaderElement(element);
        cookieStore.addCookie(cookie);
    }
}
 
開發者ID:Autonomiccs,項目名稱:apache-cloudstack-java-client,代碼行數:11,代碼來源:ApacheCloudStackClient.java

示例14: testRedirectWithCookie

import org.apache.http.client.CookieStore; //導入方法依賴的package包/類
@Test
public void testRedirectWithCookie() throws Exception {
    this.serverBootstrap.registerHandler("*", new BasicRedirectService());

    final HttpHost target = start();

    final CookieStore cookieStore = new BasicCookieStore();

    final BasicClientCookie cookie = new BasicClientCookie("name", "value");
    cookie.setDomain(target.getHostName());
    cookie.setPath("/");

    cookieStore.addCookie(cookie);

    final HttpClientContext context = HttpClientContext.create();
    context.setCookieStore(cookieStore);
    final HttpGet httpget = new HttpGet("/oldlocation/");

    final HttpResponse response = this.httpclient.execute(target, httpget, context);
    EntityUtils.consume(response.getEntity());

    final HttpRequest reqWrapper = context.getRequest();

    Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
    Assert.assertEquals("/newlocation/", reqWrapper.getRequestLine().getUri());

    final Header[] headers = reqWrapper.getHeaders(SM.COOKIE);
    Assert.assertEquals("There can only be one (cookie)", 1, headers.length);
}
 
開發者ID:MyPureCloud,項目名稱:purecloud-iot,代碼行數:30,代碼來源:TestRedirects.java

示例15: retrieveWsdl

import org.apache.http.client.CookieStore; //導入方法依賴的package包/類
static void retrieveWsdl(final Context context, final HttpGet httpGet, final String wsdlFileName) throws Exception {
    final BasicClientCookie cookie = new BasicClientCookie("sid", context.loginContext.getSessionId());
    cookie.setDomain(".salesforce.com");
    cookie.setAttribute(ClientCookie.DOMAIN_ATTR, "true");

    CookieStore cookieStore = new BasicCookieStore();
    cookieStore.addCookie(cookie);

    HttpContext localContext = new BasicHttpContext();
    localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

    final String outputFile = StringUtils.concatWithSeparator(false, System.getProperty("file.separator"), context.outputDir, context.prefix + wsdlFileName);

    System.out.println("Retreiving [" + outputFile + "]");

    CloseableHttpResponse loginResponse = HttpClients.createDefault().execute(httpGet, localContext);

    final FileWriter writer = new FileWriter(outputFile);

    IOUtils.copy(loginResponse.getEntity().getContent(), writer);

    writer.close();

    loginResponse.close();

}
 
開發者ID:solenopsis,項目名稱:Metadata,代碼行數:27,代碼來源:RetrieveWsdls.java


注:本文中的org.apache.http.client.CookieStore.addCookie方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。