gpt4 book ai didi

android - 如何使用 AndroidHttpClient(API 级别 8)和 UsernamePasswordCredentials?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:30:25 25 4
gpt4 key购买 nike

目前我正在使用 DefaultHttpClient 和 ThreadSafeClientConnManager。这工作正常,但我想用 AndroidHttpClient 替换它。不幸的是,我无法添加目前对我很重要的 UsernamePasswordCredentials。任何人都可以提供提示或解决方案吗?

最佳答案

您需要使用HttpRequestInterceptor 类进行身份验证。

举个例子

HttpRequestInterceptor httpRequestInterceptor = new HttpRequestInterceptor() {
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 (authState.getAuthScheme() == null) {
AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
Credentials creds = credsProvider.getCredentials(authScope);
if (creds != null) {
authState.setAuthScheme(new BasicScheme());
authState.setCredentials(creds);
}
}
}
};

关于android - 如何使用 AndroidHttpClient(API 级别 8)和 UsernamePasswordCredentials?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4834319/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com