下面列出了怎么用org.apache.http.client.AuthenticationHandler的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
protected RequestDirector createClientRequestDirector(
HttpRequestExecutor requestExec,
ClientConnectionManager conman,
ConnectionReuseStrategy reustrat,
ConnectionKeepAliveStrategy kastrat,
HttpRoutePlanner rouplan,
HttpProcessor httpProcessor,
HttpRequestRetryHandler retryHandler,
RedirectHandler redirectHandler,
AuthenticationHandler targetAuthHandler,
AuthenticationHandler proxyAuthHandler,
UserTokenHandler stateHandler,
HttpParams params) {
return new RequestDirector() {
@Beta
public HttpResponse execute(HttpHost target, HttpRequest request, HttpContext context)
throws HttpException, IOException {
return new BasicHttpResponse(HttpVersion.HTTP_1_1, responseCode, null);
}
};
}
private void processChallenges(
final Map<String, Header> challenges,
final AuthState authState,
final AuthenticationHandler authHandler,
final HttpResponse response,
final HttpContext context)
throws MalformedChallengeException, AuthenticationException {
AuthScheme authScheme = authState.getAuthScheme();
if (authScheme == null) {
// Authentication not attempted before
authScheme = authHandler.selectScheme(challenges, response, context);
authState.setAuthScheme(authScheme);
}
String id = authScheme.getSchemeName();
Header challenge = challenges.get(id.toLowerCase(Locale.ENGLISH));
if (challenge == null) {
throw new AuthenticationException(id +
" authorization challenge expected, but not found");
}
authScheme.processChallenge(challenge);
if (DEBUG) {
Logger.debug("Authorization challenge processed");
}
}
@Override
protected RequestDirector createClientRequestDirector(
final HttpRequestExecutor requestExec,
final ClientConnectionManager conman,
final ConnectionReuseStrategy reustrat,
final ConnectionKeepAliveStrategy kastrat,
final HttpRoutePlanner rouplan,
final HttpProcessor httpProcessor,
final HttpRequestRetryHandler retryHandler,
final RedirectHandler redirectHandler,
final AuthenticationHandler targetAuthHandler,
final AuthenticationHandler proxyAuthHandler,
final UserTokenHandler stateHandler,
final HttpParams params) {
return new LibRequestDirector(
requestExec,
conman,
reustrat,
kastrat,
rouplan,
httpProcessor,
retryHandler,
redirectHandler,
targetAuthHandler,
proxyAuthHandler,
stateHandler,
params);
}
public LibRequestDirector(
final HttpRequestExecutor requestExec,
final ClientConnectionManager conman,
final ConnectionReuseStrategy reustrat,
final ConnectionKeepAliveStrategy kastrat,
final HttpRoutePlanner rouplan,
final HttpProcessor httpProcessor,
final HttpRequestRetryHandler retryHandler,
final RedirectHandler redirectHandler,
final AuthenticationHandler targetAuthHandler,
final AuthenticationHandler proxyAuthHandler,
final UserTokenHandler userTokenHandler,
final HttpParams params) {
if (requestExec == null) {
throw new IllegalArgumentException("Request executor may not be null.");
}
if (conman == null) {
throw new IllegalArgumentException("Client connection manager may not be null.");
}
if (reustrat == null) {
throw new IllegalArgumentException("Connection reuse strategy may not be null.");
}
if (kastrat == null) {
throw new IllegalArgumentException("Connection keep alive strategy may not be null.");
}
if (rouplan == null) {
throw new IllegalArgumentException("Route planner may not be null.");
}
if (httpProcessor == null) {
throw new IllegalArgumentException("HTTP protocol processor may not be null.");
}
if (retryHandler == null) {
throw new IllegalArgumentException("HTTP request retry handler may not be null.");
}
if (redirectHandler == null) {
throw new IllegalArgumentException("Redirect handler may not be null.");
}
if (targetAuthHandler == null) {
throw new IllegalArgumentException("Target authentication handler may not be null.");
}
if (proxyAuthHandler == null) {
throw new IllegalArgumentException("Proxy authentication handler may not be null.");
}
if (userTokenHandler == null) {
throw new IllegalArgumentException("User token handler may not be null.");
}
if (params == null) {
throw new IllegalArgumentException("HTTP parameters may not be null");
}
this.requestExec = requestExec;
this.connManager = conman;
this.reuseStrategy = reustrat;
this.keepAliveStrategy = kastrat;
this.routePlanner = rouplan;
this.httpProcessor = httpProcessor;
this.retryHandler = retryHandler;
this.redirectHandler = redirectHandler;
this.targetAuthHandler = targetAuthHandler;
this.proxyAuthHandler = proxyAuthHandler;
this.userTokenHandler = userTokenHandler;
this.params = params;
this.managedConn = null;
this.execCount = 0;
this.redirectCount = 0;
this.maxRedirects = this.params.getIntParameter(ClientPNames.MAX_REDIRECTS, 100);
this.targetAuthState = new AuthState();
this.proxyAuthState = new AuthState();
}