本文整理汇总了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);
}
}
示例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;
}
示例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();
}
}
示例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;
}
示例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();
}
示例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();
}
示例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();
}
}
示例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();
}
示例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);
}
示例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();
}
}
示例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);
}
示例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);
}
示例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);
}
}
示例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);
}
示例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();
}