本文整理匯總了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);
}