当前位置: 首页>>代码示例>>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;未经允许,请勿转载。