本文整理汇总了Java中org.apache.http.client.protocol.ClientContext类的典型用法代码示例。如果您正苦于以下问题:Java ClientContext类的具体用法?Java ClientContext怎么用?Java ClientContext使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ClientContext类属于org.apache.http.client.protocol包,在下文中一共展示了ClientContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createOkResponseWithCookie
import org.apache.http.client.protocol.ClientContext; //导入依赖的package包/类
private Answer<HttpResponse> createOkResponseWithCookie() {
return new Answer<HttpResponse>() {
@Override
public HttpResponse answer(InvocationOnMock invocation) throws Throwable {
HttpContext context = (HttpContext) invocation.getArguments()[1];
if (context.getAttribute(ClientContext.COOKIE_STORE) != null) {
BasicCookieStore cookieStore =
(BasicCookieStore) context.getAttribute(ClientContext.COOKIE_STORE);
BasicClientCookie cookie = new BasicClientCookie("cookie", "meLikeCookie");
cookieStore.addCookie(cookie);
}
return OK_200_RESPONSE;
}
};
}
示例2: getHttpClient
import org.apache.http.client.protocol.ClientContext; //导入依赖的package包/类
public QMailHttpClient getHttpClient() throws MessagingException {
if (httpClient == null) {
httpClient = httpClientFactory.create();
// Disable automatic redirects on the http client.
httpClient.getParams().setBooleanParameter("http.protocol.handle-redirects", false);
// Setup a cookie store for forms-based authentication.
httpContext = new BasicHttpContext();
authCookies = new BasicCookieStore();
httpContext.setAttribute(ClientContext.COOKIE_STORE, authCookies);
SchemeRegistry reg = httpClient.getConnectionManager().getSchemeRegistry();
try {
Scheme s = new Scheme("https", new WebDavSocketFactory(hostname, 443), 443);
reg.register(s);
} catch (NoSuchAlgorithmException nsa) {
Timber.e(nsa, "NoSuchAlgorithmException in getHttpClient");
throw new MessagingException("NoSuchAlgorithmException in getHttpClient: ", nsa);
} catch (KeyManagementException kme) {
Timber.e(kme, "KeyManagementException in getHttpClient");
throw new MessagingException("KeyManagementException in getHttpClient: ", kme);
}
}
return httpClient;
}
示例3: createHttpContext
import org.apache.http.client.protocol.ClientContext; //导入依赖的package包/类
protected HttpContext createHttpContext() {
HttpContext context = new BasicHttpContext();
context.setAttribute(
ClientContext.SCHEME_REGISTRY,
getConnectionManager().getSchemeRegistry());
context.setAttribute(
ClientContext.AUTHSCHEME_REGISTRY,
getAuthSchemes());
context.setAttribute(
ClientContext.COOKIESPEC_REGISTRY,
getCookieSpecs());
context.setAttribute(
ClientContext.COOKIE_STORE,
getCookieStore());
context.setAttribute(
ClientContext.CREDS_PROVIDER,
getCredentialsProvider());
return context;
}
示例4: authSucceeded
import org.apache.http.client.protocol.ClientContext; //导入依赖的package包/类
public void authSucceeded(
final HttpHost authhost, final AuthScheme authScheme, final HttpContext context) {
if (authhost == null) {
throw new IllegalArgumentException("Host may not be null");
}
if (authScheme == null) {
throw new IllegalArgumentException("Auth scheme may not be null");
}
if (context == null) {
throw new IllegalArgumentException("HTTP context may not be null");
}
if (isCachable(authScheme)) {
AuthCache authCache = (AuthCache) context.getAttribute(ClientContext.AUTH_CACHE);
if (authCache == null) {
authCache = new BasicAuthCache();
context.setAttribute(ClientContext.AUTH_CACHE, authCache);
}
if (this.log.isDebugEnabled()) {
this.log.debug("Caching '" + authScheme.getSchemeName() +
"' auth scheme for " + authhost);
}
authCache.put(authhost, authScheme);
}
}
示例5: authFailed
import org.apache.http.client.protocol.ClientContext; //导入依赖的package包/类
public void authFailed(
final HttpHost authhost, final AuthScheme authScheme, final HttpContext context) {
if (authhost == null) {
throw new IllegalArgumentException("Host may not be null");
}
if (context == null) {
throw new IllegalArgumentException("HTTP context may not be null");
}
AuthCache authCache = (AuthCache) context.getAttribute(ClientContext.AUTH_CACHE);
if (authCache != null) {
if (this.log.isDebugEnabled()) {
this.log.debug("Clearing cached auth scheme for " + authhost);
}
authCache.remove(authhost);
}
}
示例6: downloadHTTPfile_post
import org.apache.http.client.protocol.ClientContext; //导入依赖的package包/类
private byte[] downloadHTTPfile_post(String formToDownloadLocation, List<NameValuePair> params) throws IOException, NullPointerException, URISyntaxException {
BasicHttpContext localContext = new BasicHttpContext();
LOG.info("Mimic WebDriver cookie state: " + this.mimicWebDriverCookieState);
if (this.mimicWebDriverCookieState) {
localContext.setAttribute(ClientContext.COOKIE_STORE, mimicCookieState(this.driver.manage().getCookies()));
}
HttpPost httppost = new HttpPost(formToDownloadLocation);
HttpParams httpRequestParameters = httppost.getParams();
httpRequestParameters.setParameter(ClientPNames.HANDLE_REDIRECTS, this.followRedirects);
httppost.setParams(httpRequestParameters);
httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
LOG.info("Sending POST request for: " + httppost.getURI());
@SuppressWarnings("resource")
HttpResponse response = new DefaultHttpClient().execute(httppost, localContext);
this.httpStatusOfLastDownloadAttempt = response.getStatusLine().getStatusCode();
LOG.info("HTTP GET request status: " + this.httpStatusOfLastDownloadAttempt);
byte[] file = IOUtils.toByteArray(response.getEntity().getContent());
response.getEntity().getContent().close();
return file;
}
示例7: getHTTPStatusCode
import org.apache.http.client.protocol.ClientContext; //导入依赖的package包/类
/**
* Perform an HTTP Status check and return the response code
*
* @return
* @throws IOException
*/
@SuppressWarnings("resource")
public int getHTTPStatusCode() throws IOException {
HttpClient client = new DefaultHttpClient();
BasicHttpContext localContext = new BasicHttpContext();
LOG.info("Mimic WebDriver cookie state: " + this.mimicWebDriverCookieState);
if (this.mimicWebDriverCookieState) {
localContext.setAttribute(ClientContext.COOKIE_STORE, mimicCookieState(this.driver.manage().getCookies()));
}
HttpRequestBase requestMethod = this.httpRequestMethod.getRequestMethod();
requestMethod.setURI(this.linkToCheck);
HttpParams httpRequestParameters = requestMethod.getParams();
httpRequestParameters.setParameter(ClientPNames.HANDLE_REDIRECTS, this.followRedirects);
requestMethod.setParams(httpRequestParameters);
LOG.info("Sending " + requestMethod.getMethod() + " request for: " + requestMethod.getURI());
HttpResponse response = client.execute(requestMethod, localContext);
LOG.info("HTTP " + requestMethod.getMethod() + " request status: " + response.getStatusLine().getStatusCode());
return response.getStatusLine().getStatusCode();
}
示例8: checkLocalContext
import org.apache.http.client.protocol.ClientContext; //导入依赖的package包/类
private synchronized void checkLocalContext()
{
if (null != sdkProtocolAdatperCustProvider && null != target)
{
// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// Generate DIGEST scheme object, initialize it and add it to the local auth cache
String authType = (String) ThreadLocalHolder.get().getEntities().get("AuthType");
if ("Basic".equals(authType))
{
LOGGER.debug("authentication type: basic");
}
else
{
DigestScheme digestAuth = new DigestScheme();
digestAuth.overrideParamter("nc", String.valueOf(serverNounceCount++));
digestAuth.overrideParamter("cnonce", UUID.randomUUID().toString().replaceAll("-", ""));
digestAuth.overrideParamter("qop", "auth");
authCache.put(target, digestAuth);
}
// Add AuthCache to the execution context
localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.AUTH_CACHE, authCache);
}
}
示例9: checkLocalContext
import org.apache.http.client.protocol.ClientContext; //导入依赖的package包/类
private synchronized void checkLocalContext()
{
if (null != sdkProtocolAdatperCustProvider && null != target)
{
// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// Generate DIGEST scheme object, initialize it and add it to the local auth cache
String authType = (String)ThreadLocalHolder.get().getEntities().get("AuthType");
if ("Basic".equals(authType))
{
LOGGER.debug("authentication type: basic");
}
else
{
DigestScheme digestAuth = new DigestScheme();
digestAuth.overrideParamter("nc", String.valueOf(serverNounceCount++));
digestAuth.overrideParamter("cnonce", UUID.randomUUID().toString().replaceAll("-", ""));
digestAuth.overrideParamter("qop", "auth");
authCache.put(target, digestAuth);
}
// Add AuthCache to the execution context
localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.AUTH_CACHE, authCache);
}
}
示例10: process
import org.apache.http.client.protocol.ClientContext; //导入依赖的package包/类
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
// If no auth scheme avaialble yet, try to initialize it preemptively
if (authState.getAuthScheme() == null) {
AuthScheme authScheme = (AuthScheme) context.getAttribute("preemptive-auth");
CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
if (authScheme != null) {
Credentials creds = credsProvider.getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()));
if (creds == null) {
throw new HttpException("No credentials for preemptive authentication");
}
authState.setAuthScheme(authScheme);
authState.setCredentials(creds);
}
}
}
示例11: process
import org.apache.http.client.protocol.ClientContext; //导入依赖的package包/类
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
//
// get the authentication state for the request
//
AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
//
// if no auth scheme avaialble yet, try to initialize it preemptively
//
if (authState.getAuthScheme() == null) {
// check if credentials are set
if (this.credentials == null) {
throw new HttpException("No credentials for preemptive authentication");
}
// add the credentials to the auth state
authState.setAuthScheme(this.basicAuthScheme);
authState.setCredentials(this.credentials);
// HttpHost targetHost = (HttpHost)context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
//CredentialsProvider credsProvider = (CredentialsProvider)context.getAttribute(ClientContext.CREDS_PROVIDER);
}
}
示例12: getHttpClient
import org.apache.http.client.protocol.ClientContext; //导入依赖的package包/类
public WebDavHttpClient getHttpClient() throws MessagingException {
if (mHttpClient == null) {
mHttpClient = mHttpClientFactory.create();
// Disable automatic redirects on the http client.
mHttpClient.getParams().setBooleanParameter("http.protocol.handle-redirects", false);
// Setup a cookie store for forms-based authentication.
mContext = new BasicHttpContext();
mAuthCookies = new BasicCookieStore();
mContext.setAttribute(ClientContext.COOKIE_STORE, mAuthCookies);
SchemeRegistry reg = mHttpClient.getConnectionManager().getSchemeRegistry();
try {
Scheme s = new Scheme("https", new WebDavSocketFactory(mHost, 443), 443);
reg.register(s);
} catch (NoSuchAlgorithmException nsa) {
Log.e(LOG_TAG, "NoSuchAlgorithmException in getHttpClient: " + nsa);
throw new MessagingException("NoSuchAlgorithmException in getHttpClient: " + nsa);
} catch (KeyManagementException kme) {
Log.e(LOG_TAG, "KeyManagementException in getHttpClient: " + kme);
throw new MessagingException("KeyManagementException in getHttpClient: " + kme);
}
}
return mHttpClient;
}
示例13: process
import org.apache.http.client.protocol.ClientContext; //导入依赖的package包/类
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
// If no auth scheme has been initialized yet
if (authState.getAuthScheme() == null) {
CredentialsProvider credentialsProvider = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
// Obtain credentials matching the target host
Credentials credentials = credentialsProvider.getCredentials(authScope);
// If found, generate BasicScheme preemptively
if (credentials != null) {
authState.setAuthScheme(new DiadocAuthScheme());
authState.setCredentials(credentials);
}
}
}
示例14: process
import org.apache.http.client.protocol.ClientContext; //导入依赖的package包/类
@Override
public void process(final HttpRequest request, final HttpContext context)
throws HttpException, IOException {
AuthState authState = (AuthState)context.getAttribute(ClientContext.TARGET_AUTH_STATE);
CredentialsProvider credsProvider = (CredentialsProvider)context
.getAttribute(ClientContext.CREDS_PROVIDER);
HttpHost targetHost = (HttpHost)context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
// If not auth scheme has been initialized yet
if (authState.getAuthScheme() == null) {
AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
// Obtain credentials matching the target host
org.apache.http.auth.Credentials creds = credsProvider.getCredentials(authScope);
// If found, generate BasicScheme preemptively
if (creds != null) {
authState.setAuthScheme(new BasicScheme());
authState.setCredentials(creds);
}
}
}
示例15: process
import org.apache.http.client.protocol.ClientContext; //导入依赖的package包/类
@Override
public void process(final HttpRequest request, final HttpContext context)
throws HttpException, IOException {
AuthState authState = (AuthState)context.getAttribute(ClientContext.TARGET_AUTH_STATE);
CredentialsProvider credsProvider = (CredentialsProvider)context
.getAttribute(ClientContext.CREDS_PROVIDER);
HttpHost targetHost = (HttpHost)context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
// If not auth scheme has been initialized yet
if (authState.getAuthScheme() == null) {
AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
org.apache.http.auth.Credentials creds = credsProvider.getCredentials(authScope);
if (creds != null) {
authState.setAuthScheme(new BasicScheme());
authState.setCredentials(creds);
}
}
}