本文整理汇总了Java中org.apache.http.client.protocol.HttpClientContext.setAttribute方法的典型用法代码示例。如果您正苦于以下问题:Java HttpClientContext.setAttribute方法的具体用法?Java HttpClientContext.setAttribute怎么用?Java HttpClientContext.setAttribute使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.http.client.protocol.HttpClientContext
的用法示例。
在下文中一共展示了HttpClientContext.setAttribute方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: newClientContext
import org.apache.http.client.protocol.HttpClientContext; //导入方法依赖的package包/类
/**
* Returns a new HttpClientContext used for request execution.
*/
public static HttpClientContext newClientContext(HttpClientSettings settings,
Map<String, ? extends Object>
attributes) {
final HttpClientContext clientContext = new HttpClientContext();
if (attributes != null && !attributes.isEmpty()) {
for (Map.Entry<String, ?> entry : attributes.entrySet()) {
clientContext.setAttribute(entry.getKey(), entry.getValue());
}
}
addPreemptiveAuthenticationProxy(clientContext, settings);
return clientContext;
}
示例2: setupContext
import org.apache.http.client.protocol.HttpClientContext; //导入方法依赖的package包/类
private void setupContext(final HttpClientContext context) {
if (context.getAttribute(HttpClientContext.TARGET_AUTH_STATE) == null) {
context.setAttribute(HttpClientContext.TARGET_AUTH_STATE, new AuthStateHC4());
}
if (context.getAttribute(HttpClientContext.PROXY_AUTH_STATE) == null) {
context.setAttribute(HttpClientContext.PROXY_AUTH_STATE, new AuthStateHC4());
}
if (context.getAttribute(HttpClientContext.AUTHSCHEME_REGISTRY) == null) {
context.setAttribute(HttpClientContext.AUTHSCHEME_REGISTRY, this.authSchemeRegistry);
}
if (context.getAttribute(HttpClientContext.COOKIESPEC_REGISTRY) == null) {
context.setAttribute(HttpClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry);
}
if (context.getAttribute(HttpClientContext.COOKIE_STORE) == null) {
context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore);
}
if (context.getAttribute(HttpClientContext.CREDS_PROVIDER) == null) {
context.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credentialsProvider);
}
if (context.getAttribute(HttpClientContext.REQUEST_CONFIG) == null) {
context.setAttribute(HttpClientContext.REQUEST_CONFIG, this.defaultConfig);
}
}
示例3: buildHttpContext
import org.apache.http.client.protocol.HttpClientContext; //导入方法依赖的package包/类
/**
* Builds a configured HTTP context object that is pre-configured for
* using HTTP Signature authentication.
*
* @param configurator HTTP Signatures configuration helper to pull properties from
* @return configured HTTP context object
*/
protected HttpContext buildHttpContext(final HttpSignatureConfigurator configurator) {
final HttpClientContext context = HttpClientContext.create();
if (configurator != null) {
AuthCache authCache = new BasicAuthCache();
context.setAuthCache(authCache);
AuthState authState = new AuthState();
authState.update(configurator.getAuthScheme(), configurator.getCredentials());
context.setAttribute(HttpClientContext.TARGET_AUTH_STATE,
authState);
context.getTargetAuthState().setState(AuthProtocolState.UNCHALLENGED);
}
return context;
}
示例4: setupContext
import org.apache.http.client.protocol.HttpClientContext; //导入方法依赖的package包/类
private void setupContext(final HttpClientContext context) {
if (context.getAttribute(HttpClientContext.TARGET_AUTH_STATE) == null) {
context.setAttribute(HttpClientContext.TARGET_AUTH_STATE, new AuthState());
}
if (context.getAttribute(HttpClientContext.PROXY_AUTH_STATE) == null) {
context.setAttribute(HttpClientContext.PROXY_AUTH_STATE, new AuthState());
}
if (context.getAttribute(HttpClientContext.AUTHSCHEME_REGISTRY) == null) {
context.setAttribute(HttpClientContext.AUTHSCHEME_REGISTRY, this.authSchemeRegistry);
}
if (context.getAttribute(HttpClientContext.COOKIESPEC_REGISTRY) == null) {
context.setAttribute(HttpClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry);
}
if (context.getAttribute(HttpClientContext.COOKIE_STORE) == null) {
context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore);
}
if (context.getAttribute(HttpClientContext.CREDS_PROVIDER) == null) {
context.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credentialsProvider);
}
if (context.getAttribute(HttpClientContext.REQUEST_CONFIG) == null) {
context.setAttribute(HttpClientContext.REQUEST_CONFIG, this.defaultConfig);
}
}
示例5: testRetryRequest
import org.apache.http.client.protocol.HttpClientContext; //导入方法依赖的package包/类
@Test
public void testRetryRequest() throws Exception {
final S3HttpRequestRetryHandler h = new S3HttpRequestRetryHandler(new JetS3tRequestAuthorizer() {
@Override
public void authorizeHttpRequest(final HttpUriRequest httpUriRequest, final HttpContext httpContext, final String s) throws ServiceException {
//
}
}, 1);
final HttpClientContext context = new HttpClientContext();
context.setAttribute(HttpCoreContext.HTTP_REQUEST, new HttpHead());
assertTrue(h.retryRequest(new SSLException(new SocketException("Broken pipe")), 1, context));
}
示例6: execute
import org.apache.http.client.protocol.HttpClientContext; //导入方法依赖的package包/类
@Override
public CloseableHttpResponse execute(
HttpRoute route,
HttpRequestWrapper request,
HttpClientContext clientContext,
HttpExecutionAware execAware) throws IOException, HttpException {
ActiveSpan localSpan = clientContext.getAttribute(ACTIVE_SPAN, ActiveSpan.class);
CloseableHttpResponse response = null;
try {
if (localSpan == null) {
localSpan = handleLocalSpan(request, clientContext);
}
return (response = handleNetworkProcessing(localSpan, route, request, clientContext, execAware));
} catch (Exception e) {
localSpan.deactivate();
throw e;
} finally {
if (response != null) {
/**
* This exec runs after {@link org.apache.http.impl.execchain.RedirectExec} which loops
* until there is no redirect or reaches max redirect count.
* {@link RedirectStrategy} is used to decide whether localSpan should be finished or not.
* If there is a redirect localSpan is not finished and redirect is logged.
*/
Integer redirectCount = clientContext.getAttribute(REDIRECT_COUNT, Integer.class);
if (!redirectHandlingDisabled &&
clientContext.getRequestConfig().isRedirectsEnabled() &&
redirectStrategy.isRedirected(request, response, clientContext) &&
++redirectCount < clientContext.getRequestConfig().getMaxRedirects()) {
clientContext.setAttribute(REDIRECT_COUNT, redirectCount);
} else {
localSpan.deactivate();
}
}
}
}
示例7: handleLocalSpan
import org.apache.http.client.protocol.HttpClientContext; //导入方法依赖的package包/类
protected ActiveSpan handleLocalSpan(HttpRequest httpRequest, HttpClientContext clientContext) {
Tracer.SpanBuilder spanBuilder = tracer.buildSpan(httpRequest.getRequestLine().getMethod())
.withTag(Tags.COMPONENT.getKey(), COMPONENT_NAME);
if (clientContext.getAttribute(PARENT_CONTEXT, SpanContext.class) != null) {
spanBuilder.ignoreActiveSpan()
.asChildOf(clientContext.getAttribute(PARENT_CONTEXT, SpanContext.class));
}
ActiveSpan localSpan = spanBuilder.startActive();
clientContext.setAttribute(ACTIVE_SPAN, localSpan);
clientContext.setAttribute(REDIRECT_COUNT, 0);
return localSpan;
}
示例8: handleCacheHit
import org.apache.http.client.protocol.HttpClientContext; //导入方法依赖的package包/类
private CloseableHttpResponse handleCacheHit(
final HttpRoute route,
final HttpRequestWrapper request,
final HttpClientContext context,
final HttpExecutionAware execAware,
final HttpCacheEntry entry) throws IOException, HttpException {
final HttpHost target = context.getTargetHost();
recordCacheHit(target, request);
CloseableHttpResponse out = null;
final Date now = getCurrentDate();
if (suitabilityChecker.canCachedResponseBeUsed(target, request, entry, now)) {
log.debug("Cache hit");
out = generateCachedResponse(request, context, entry, now);
} else if (!mayCallBackend(request)) {
log.debug("Cache entry not suitable but only-if-cached requested");
out = generateGatewayTimeout(context);
} else if (!(entry.getStatusCode() == HttpStatus.SC_NOT_MODIFIED
&& !suitabilityChecker.isConditional(request))) {
log.debug("Revalidating cache entry");
return revalidateCacheEntry(route, request, context, execAware, entry, now);
} else {
log.debug("Cache entry not usable; calling backend");
return callBackend(route, request, context, execAware);
}
context.setAttribute(HttpClientContext.HTTP_ROUTE, route);
context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, target);
context.setAttribute(HttpCoreContext.HTTP_REQUEST, request);
context.setAttribute(HttpCoreContext.HTTP_RESPONSE, out);
context.setAttribute(HttpCoreContext.HTTP_REQ_SENT, Boolean.TRUE);
return out;
}
示例9: main
import org.apache.http.client.protocol.HttpClientContext; //导入方法依赖的package包/类
public static void main(String[] args)throws Exception {
Registry<ConnectionSocketFactory> reg = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", new MyConnectionSocketFactory())
.build();
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(reg);
CloseableHttpClient httpclient = HttpClients.custom()
.setConnectionManager(cm)
.build();
try {
InetSocketAddress socksaddr = new InetSocketAddress("mysockshost", 1234);
HttpClientContext context = HttpClientContext.create();
context.setAttribute("socks.address", socksaddr);
HttpHost target = new HttpHost("httpbin.org", 80, "http");
HttpGet request = new HttpGet("/");
System.out.println("Executing request " + request + " to " + target + " via SOCKS proxy " + socksaddr);
CloseableHttpResponse response = httpclient.execute(target, request, context);
try {
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
EntityUtils.consume(response.getEntity());
} finally {
response.close();
}
} finally {
httpclient.close();
}
}
示例10: testGetLocationUriRelative
import org.apache.http.client.protocol.HttpClientContext; //导入方法依赖的package包/类
@Test
public void testGetLocationUriRelative() throws Exception {
final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
final HttpClientContext context = HttpClientContext.create();
context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, new HttpHost("localhost"));
final HttpGet httpget = new HttpGet("http://localhost/");
final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
response.addHeader("Location", "/stuff");
final URI uri = redirectStrategy.getLocationURI(httpget, response, context);
Assert.assertEquals(URI.create("http://localhost/stuff"), uri);
}
示例11: testGetLocationUriRelativeWithFragment
import org.apache.http.client.protocol.HttpClientContext; //导入方法依赖的package包/类
@Test
public void testGetLocationUriRelativeWithFragment() throws Exception {
final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
final HttpClientContext context = HttpClientContext.create();
context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, new HttpHost("localhost"));
final HttpGet httpget = new HttpGet("http://localhost/");
final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
response.addHeader("Location", "/stuff#fragment");
final URI uri = redirectStrategy.getLocationURI(httpget, response, context);
Assert.assertEquals(URI.create("http://localhost/stuff#fragment"), uri);
}
示例12: testGetLocationUriAbsoluteWithFragment
import org.apache.http.client.protocol.HttpClientContext; //导入方法依赖的package包/类
@Test
public void testGetLocationUriAbsoluteWithFragment() throws Exception {
final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
final HttpClientContext context = HttpClientContext.create();
context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, new HttpHost("localhost"));
final HttpGet httpget = new HttpGet("http://localhost/");
final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
response.addHeader("Location", "http://localhost/stuff#fragment");
final URI uri = redirectStrategy.getLocationURI(httpget, response, context);
Assert.assertEquals(URI.create("http://localhost/stuff#fragment"), uri);
}
示例13: testGetLocationUriNormalized
import org.apache.http.client.protocol.HttpClientContext; //导入方法依赖的package包/类
@Test
public void testGetLocationUriNormalized() throws Exception {
final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
final HttpClientContext context = HttpClientContext.create();
context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, new HttpHost("localhost"));
final HttpGet httpget = new HttpGet("http://localhost/");
final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
response.addHeader("Location", "http://localhost/././stuff/../morestuff");
final URI uri = redirectStrategy.getLocationURI(httpget, response, context);
Assert.assertEquals(URI.create("http://localhost/morestuff"), uri);
}
示例14: testGetLocationUriRelativeLocationNotAllowed
import org.apache.http.client.protocol.HttpClientContext; //导入方法依赖的package包/类
@Test(expected=ProtocolException.class)
public void testGetLocationUriRelativeLocationNotAllowed() throws Exception {
final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
final HttpClientContext context = HttpClientContext.create();
context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, new HttpHost("localhost"));
final RequestConfig config = RequestConfig.custom().setRelativeRedirectsAllowed(false).build();
context.setRequestConfig(config);
final HttpGet httpget = new HttpGet("http://localhost/");
final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
response.addHeader("Location", "/stuff");
redirectStrategy.getLocationURI(httpget, response, context);
}
示例15: testGetLocationUriAllowCircularRedirects
import org.apache.http.client.protocol.HttpClientContext; //导入方法依赖的package包/类
@Test
public void testGetLocationUriAllowCircularRedirects() throws Exception {
final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
final HttpClientContext context = HttpClientContext.create();
context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, new HttpHost("localhost"));
final RequestConfig config = RequestConfig.custom().setCircularRedirectsAllowed(true).build();
context.setRequestConfig(config);
final URI uri1 = URI.create("http://localhost/stuff1");
final URI uri2 = URI.create("http://localhost/stuff2");
final URI uri3 = URI.create("http://localhost/stuff3");
final HttpGet httpget1 = new HttpGet("http://localhost/");
final HttpResponse response1 = new BasicHttpResponse(HttpVersion.HTTP_1_1,
HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
response1.addHeader("Location", uri1.toASCIIString());
final HttpGet httpget2 = new HttpGet(uri1.toASCIIString());
final HttpResponse response2 = new BasicHttpResponse(HttpVersion.HTTP_1_1,
HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
response2.addHeader("Location", uri2.toASCIIString());
final HttpGet httpget3 = new HttpGet(uri2.toASCIIString());
final HttpResponse response3 = new BasicHttpResponse(HttpVersion.HTTP_1_1,
HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
response3.addHeader("Location", uri3.toASCIIString());
Assert.assertEquals(uri1, redirectStrategy.getLocationURI(httpget1, response1, context));
Assert.assertEquals(uri2, redirectStrategy.getLocationURI(httpget2, response2, context));
Assert.assertEquals(uri3, redirectStrategy.getLocationURI(httpget3, response3, context));
final List<URI> uris = context.getRedirectLocations();
Assert.assertNotNull(uris);
Assert.assertTrue(uris.contains(uri1));
Assert.assertTrue(uris.contains(uri2));
Assert.assertTrue(uris.contains(uri3));
Assert.assertEquals(3, uris.size());
Assert.assertEquals(uri1, uris.get(0));
Assert.assertEquals(uri2, uris.get(1));
Assert.assertEquals(uri3, uris.get(2));
}