下面列出了org.apache.http.client.protocol.HttpClientContext#getAttribute ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public CloseableHttpResponse execute(HttpRoute route, HttpRequestWrapper request, HttpClientContext clientContext,
HttpExecutionAware execAware) throws IOException, HttpException {
Proxy proxy = (Proxy) clientContext.getAttribute(VSCrawlerConstant.VSCRAWLER_AVPROXY_KEY);
if (proxy != null) {
proxy.recordUsage();
}
try {
return delegate.execute(route, request, clientContext, execAware);
} catch (IOException ioe) {
if (proxy != null) {
proxy.recordFailed();
}
throw ioe;
}
}
@Override
public void authSucceeded(final HttpHost authhost, final AuthScheme authScheme, final HttpContext context) {
final HttpClientContext clientContext = HttpClientContext.adapt(context);
final Credentials credentials = clientContext.getAttribute(PROXY_CREDENTIALS_INPUT_ID, Credentials.class);
if(null != credentials) {
clientContext.removeAttribute(PROXY_CREDENTIALS_INPUT_ID);
if(log.isInfoEnabled()) {
log.info(String.format("Save passphrase for proxy %s", authhost));
}
keychain.addCredentials(authhost.toURI(), credentials.getUsername(), credentials.getPassword());
}
super.authSucceeded(authhost, authScheme, context);
}
public void setRetryCount(HttpClientContext context, int value) {
RetryCount count = context.getAttribute(attributeName, RetryCount.class);
if (count == null) {
count = new RetryCount();
context.setAttribute(attributeName, count);
}
count.value = value;
}
private void setRetryCount(HttpClientContext context, int value) {
RetryCount count = context.getAttribute(RETRY_COUNT_ATTRIBUTE, RetryCount.class);
if (count == null) {
count = new RetryCount();
context.setAttribute(RETRY_COUNT_ATTRIBUTE, count);
}
count.value = value;
}
public boolean isIdFetchContext(HttpClientContext context) {
return context.getAttribute(ID_FETCH_CONTEXT_ATTRIBUTE) == Boolean.TRUE;
}
public int getRetryCount(HttpClientContext context) {
RetryCount count = context.getAttribute(attributeName, RetryCount.class);
return count != null ? count.value : 0;
}
public final InetAddress getLastAddress(final HttpClientContext context) {
return context.getAttribute(CHOSEN_IP_ATTRIBUTE, InetAddress.class);
}
private int getRetryCount(HttpClientContext context) {
RetryCount count = context.getAttribute(RETRY_COUNT_ATTRIBUTE, RetryCount.class);
return count != null ? count.value : 0;
}