本文整理汇总了Java中org.apache.http.client.protocol.HttpClientContext.setCookieStore方法的典型用法代码示例。如果您正苦于以下问题:Java HttpClientContext.setCookieStore方法的具体用法?Java HttpClientContext.setCookieStore怎么用?Java HttpClientContext.setCookieStore使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.http.client.protocol.HttpClientContext
的用法示例。
在下文中一共展示了HttpClientContext.setCookieStore方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testExecuteLocalContext
import org.apache.http.client.protocol.HttpClientContext; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
@Test
public void testExecuteLocalContext() throws Exception {
final HttpGet httpget = new HttpGet("http://somehost/stuff");
final HttpClientContext context = HttpClientContext.create();
final Lookup<CookieSpecProvider> localCookieSpecRegistry = Mockito.mock(Lookup.class);
final Lookup<AuthSchemeProvider> localAuthSchemeRegistry = Mockito.mock(Lookup.class);
final CookieStore localCookieStore = Mockito.mock(CookieStore.class);
final CredentialsProvider localCredentialsProvider = Mockito.mock(CredentialsProvider.class);
final RequestConfig localConfig = RequestConfig.custom().build();
context.setCookieSpecRegistry(localCookieSpecRegistry);
context.setAuthSchemeRegistry(localAuthSchemeRegistry);
context.setCookieStore(localCookieStore);
context.setCredentialsProvider(localCredentialsProvider);
context.setRequestConfig(localConfig);
client.execute(httpget, context);
Assert.assertSame(localCookieSpecRegistry, context.getCookieSpecRegistry());
Assert.assertSame(localAuthSchemeRegistry, context.getAuthSchemeRegistry());
Assert.assertSame(localCookieStore, context.getCookieStore());
Assert.assertSame(localCredentialsProvider, context.getCredentialsProvider());
Assert.assertSame(localConfig, context.getRequestConfig());
}
示例2: createContext
import org.apache.http.client.protocol.HttpClientContext; //导入方法依赖的package包/类
private HttpClientContext createContext(Authentication auth, HttpHost target) {
HttpClientContext httpClientContext = HttpClientContext.create();
CookieStore cookieStore = new BasicCookieStore();
httpClientContext.setCookieStore(cookieStore);
if (auth.usePreemptiveAuthentication()) {
httpClientContext.setAuthCache(new Auth().getAuthCache(auth, target));
}
return httpClientContext;
}
示例3: createHttpClientContextFromConfig
import org.apache.http.client.protocol.HttpClientContext; //导入方法依赖的package包/类
private HttpClientContext createHttpClientContextFromConfig() {
// create context from config
HttpClientContext context = HttpClientContext.create();
if (config.getCookieStore() != null) {
context.setCookieStore(config.getCookieStore());
}
if (config.getCredsProvider() != null) {
context.setCredentialsProvider(config.getCredsProvider());
}
if (config.getAuthCache() != null) {
context.setAuthCache(config.getAuthCache());
}
return context;
}
示例4: main
import org.apache.http.client.protocol.HttpClientContext; //导入方法依赖的package包/类
public final static void main(String[] args) throws Exception {
CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
try {
// Create a local instance of cookie store
CookieStore cookieStore = new BasicCookieStore();
// Create local HTTP context
HttpClientContext localContext = HttpClientContext.create();
// Bind custom cookie store to the local context
localContext.setCookieStore(cookieStore);
HttpGet httpget = new HttpGet("http://localhost/");
System.out.println("Executing request " + httpget.getRequestLine());
httpclient.start();
// Pass local context as a parameter
Future<HttpResponse> future = httpclient.execute(httpget, localContext, null);
// Please note that it may be unsafe to access HttpContext instance
// while the request is still being executed
HttpResponse response = future.get();
System.out.println("Response: " + response.getStatusLine());
List<Cookie> cookies = cookieStore.getCookies();
for (int i = 0; i < cookies.size(); i++) {
System.out.println("Local cookie: " + cookies.get(i));
}
System.out.println("Shutting down");
} finally {
httpclient.close();
}
}
示例5: getCookie
import org.apache.http.client.protocol.HttpClientContext; //导入方法依赖的package包/类
/**
* This method provides browser cookie for authenticating user to AEM instance
*
* @param url URL to AEM instance, like http://localhost:4502
* @param login Username to use
* @param password Password to use
* @return Cookie for selenium WebDriver.
*/
public Cookie getCookie(String url, String login, String password) {
if (!cookieJar.containsKey(url)) {
HttpPost loginPost = new HttpPost(url
+ "/libs/granite/core/content/login.html/j_security_check");
List<NameValuePair> nameValuePairs = new ArrayList<>();
nameValuePairs.add(new BasicNameValuePair("_charset_", "utf-8"));
nameValuePairs.add(new BasicNameValuePair("j_username", login));
nameValuePairs.add(new BasicNameValuePair("j_password", password));
nameValuePairs.add(new BasicNameValuePair("j_validate", "true"));
CookieStore cookieStore = new BasicCookieStore();
HttpClientContext context = HttpClientContext.create();
if ("true".equals(properties.getProperty(ConfigKeys.WEBDRIVER_PROXY_COOKIE))) {
addProxyCookie(cookieStore);
}
context.setCookieStore(cookieStore);
try {
loginPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
CloseableHttpResponse loginResponse = httpClient.execute(loginPost, context);
loginResponse.close();
} catch (IOException e) {
LOG.error("Can't get AEM authentication cookie", e);
} finally {
loginPost.reset();
}
Cookie cookie = findAuthenticationCookie(cookieStore.getCookies());
if (cookie != null) {
cookieJar.put(url, cookie);
}
}
return cookieJar.get(url);
}
示例6: deserializeCookie
import org.apache.http.client.protocol.HttpClientContext; //导入方法依赖的package包/类
public static boolean deserializeCookie(String path, HttpClientContext httpClientContext) {
try {
CookieStore cookieStore = (CookieStore) deserializeMyContext(path);
httpClientContext.setCookieStore(cookieStore);
} catch (Exception e) {
return false;
}
return true;
}
示例7: getHttpContext
import org.apache.http.client.protocol.HttpClientContext; //导入方法依赖的package包/类
private HttpClientContext getHttpContext(String serviceUsername, String servicePassword) {
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(serviceUsername + ":" + servicePassword));
CookieStore cookieStore = new BasicCookieStore();
HttpClientContext context = HttpClientContext.create();
context.setCookieStore(cookieStore);
context.setCredentialsProvider(credentialsProvider);
return context;
}
示例8: main
import org.apache.http.client.protocol.HttpClientContext; //导入方法依赖的package包/类
public final static void main(String[] args) throws Exception {
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
// Create a local instance of cookie store
CookieStore cookieStore = new BasicCookieStore();
// Create local HTTP context
HttpClientContext localContext = HttpClientContext.create();
// Bind custom cookie store to the local context
localContext.setCookieStore(cookieStore);
HttpGet httpget = new HttpGet("http://httpbin.org/cookies");
System.out.println("Executing request " + httpget.getRequestLine());
// Pass local context as a parameter
CloseableHttpResponse response = httpclient.execute(httpget, localContext);
try {
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
List<Cookie> cookies = cookieStore.getCookies();
for (int i = 0; i < cookies.size(); i++) {
System.out.println("Local cookie: " + cookies.get(i));
}
EntityUtils.consume(response.getEntity());
} finally {
response.close();
}
} finally {
httpclient.close();
}
}
示例9: testRedirectWithCookie
import org.apache.http.client.protocol.HttpClientContext; //导入方法依赖的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);
}
示例10: createHttpContext
import org.apache.http.client.protocol.HttpClientContext; //导入方法依赖的package包/类
public static void createHttpContext() {
HttpClientContext localContext = HttpClientContext.create();
localContext.setCookieStore(cookieStore);
localContext.setCredentialsProvider(credentialsProvider);
}
示例11: createHttpClientContext
import org.apache.http.client.protocol.HttpClientContext; //导入方法依赖的package包/类
public static HttpClientContext createHttpClientContext() {
HttpClientContext context = HttpClientContext.create();
context.setCookieStore(cookieStore);
return context;
}
示例12: authentifyBancho
import org.apache.http.client.protocol.HttpClientContext; //导入方法依赖的package包/类
private void authentifyBancho() throws IOException, LoginException {
if (httpClient == null)
throw new StateException("Invalid HTTP client.");
HttpClientContext httpContext = new HttpClientContext();
httpContext.setCookieStore(new BasicCookieStore());
String requestBody = username + "\n" + password + "\n";
// Documentation for the third line:
// (osu client version)|(UTC offset)|(1 if your city should be public, 0 otherwise)|(MD5 hashed described below)|(1 if non-friend PMs should be blocked, 0 otherwise)
// 4th argument: <MD5 hash for the executable>::<MD5 for empty string>:<MD5 for "unknown">:<MD5 for "unknown">
// Only the first one seems to really matter.
// Latest MD5 hash for the osu!.exe executable can be found here: https://goo.gl/IVUVA3
TimeZone tz = TimeZone.getDefault();
int offset = tz.getRawOffset()/3600000;
requestBody += CLIENT_VERSION + "|" + offset + "|0|" + EXE_HASH + "::d41d8cd98f00b204e9800998ecf8427e:ad921d60486366258809553a3db49a4a:ad921d60486366258809553a3db49a4a:|0" + "\n";
HttpEntity entity = new ByteArrayEntity(requestBody.getBytes("UTF-8"));
HttpPost request = new HttpPost(BANCHO_URI);
request.setEntity(entity);
request.addHeader("osu-version", CLIENT_VERSION);
request.addHeader("Accept-Encoding", "gzip");
request.addHeader("User-Agent", "osu!");
request.addHeader("Connection", "Keep-Alive");
HttpResponse response = httpClient.execute(request, httpContext);
for (Header header : response.getAllHeaders()) {
if (header.getName().equals("cho-token")) {
token = header.getValue();
}
}
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != 200) {
System.out.println("Error: " + statusCode);
if (statusCode != 520) {
httpClient = null; // invalidate http client
throw new LoginException("Failed to authentify to bancho (invalid creditials? offline?)");
} else {
System.out.println("Server error detected.");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {}
}
}
InputStream content = response.getEntity().getContent();
handleBanchoResponse(response, content);
}
示例13: authentifyBancho
import org.apache.http.client.protocol.HttpClientContext; //导入方法依赖的package包/类
private void authentifyBancho() throws URISyntaxException, ClientProtocolException, IOException, LoginException {
if (httpClient == null)
throw new StateException("Invalid HTTP client.");
HttpClientContext httpContext = new HttpClientContext();
httpContext.setCookieStore(new BasicCookieStore());
String requestBody = username + "\n" + password + "\n";
// Documentation for the third line:
// (osu client version)|(UTC offset)|(1 if your city should be public, 0 otherwise)|(MD5 hashed described below)|(1 if non-friend PMs should be blocked, 0 otherwise)
// 4th argument: <MD5 hash for the executable>::<MD5 for empty string>:<MD5 for "unknown">:<MD5 for "unknown">
// Only the first one seems to really matter.
// Latest MD5 hash for the osu!.exe executable can be found here: https://goo.gl/IVUVA3
TimeZone tz = TimeZone.getDefault();
int offset = tz.getRawOffset()/3600000;
requestBody += CLIENT_VERSION + "|" + offset + "|0|" + EXE_HASH + "::d41d8cd98f00b204e9800998ecf8427e:ad921d60486366258809553a3db49a4a:ad921d60486366258809553a3db49a4a:|0" + "\n";
HttpEntity entity = new ByteArrayEntity(requestBody.getBytes("UTF-8"));
HttpPost request = new HttpPost(BANCHO_URI);
request.setEntity(entity);
request.addHeader("osu-version", CLIENT_VERSION);
request.addHeader("Accept-Encoding", "gzip");
request.addHeader("User-Agent", "osu!");
request.addHeader("Connection", "Keep-Alive");
HttpResponse response = httpClient.execute(request, httpContext);
for (Header header : response.getAllHeaders()) {
if (header.getName().equals("cho-token")) {
token = header.getValue();
}
}
if (response.getStatusLine().getStatusCode() != 200) {
httpClient = null; // invalidate http client
throw new LoginException("Failed to authentify to bancho (invalid creditials? offline?)");
}
InputStream content = response.getEntity().getContent();
handleBanchoResponse(content);
}