gpt4 book ai didi

android - 需要帮助为 Android 创建摘要式身份验证

转载 作者:太空宇宙 更新时间:2023-11-03 11:52:19 24 4
gpt4 key购买 nike

到目前为止我有这段代码:

private class DownloadWebPageTask extends AsyncTask<String, Void, String> 
{
@Override
protected String doInBackground(String... theParams)
{
String myUrl = theParams[0];
String myEmail = theParams[1];
String myPassword = theParams[2];

HttpPost post = new HttpPost(myUrl);
post.addHeader("Authorization","Basic "+ Base64.encodeToString((myEmail+":"+myPassword).getBytes(), 0 ));
ResponseHandler<String> responseHandler = new BasicResponseHandler();

String response = null;

try
{
response = client.execute(post, responseHandler);
InputStream content = execute.getEntity().getContent();

BufferedReader buffer = new BufferedReader(
new InputStreamReader(content));
String s = "";
while ((s = buffer.readLine()) != null)
{
response += s;
}
}
catch (Exception e)
{
e.printStackTrace();
}

return response;
}


@Override
protected void onPostExecute(String result)
{
}

}

此代码无法编译,因为我在以下方面遇到了困惑:

                response = client.execute(post, responseHandler);
InputStream content = execute.getEntity().getContent();

我通过修改各种示例获得了该代码,但不确定客户端应该是什么对象,也不确定第一行是否只会让我得到服务器响应,或者我必须走获取 InputStream 和读取服务器响应?

请帮助我了解如何正确执行此操作。

谢谢!

最佳答案

我已经设法通过 OkHttp 使用摘要式身份验证.在此代码示例中,我还使用了 Dagger 和 Robospice-retrofit。我所做的是创建一个 OkHttp Authenticator并将其分配给我的自定义 OkHttp 客户端。

身份验证器类实现了一个authenticate 方法,只要服务器遇到 401 错误并期望返回一个Authorization header (如果它期望Proxy-授权您应该实现authenticateProxy 方法。

它基本上所做的是包装对 HttpClient DigestScheme 的调用并使其可用于 OkHttp。目前它不增加 nc 计数器。这可能会导致您的服务器出现问题,因为它可能被解释为重放攻击。

public class DigestAuthenticator implements com.squareup.okhttp.Authenticator {
@Inject DigestScheme mDigestScheme;
@Inject org.apache.http.auth.Credentials mCredentials;

@Override
public Request authenticate(Proxy proxy, Response response) throws IOException {
String authHeader = buildAuthorizationHeader(response);
if (authHeader == null) {
return null;
}
return response.request().newBuilder().addHeader("Authorization", authHeader).build();
}

@Override
public Request authenticateProxy(Proxy proxy, Response response) throws IOException {
return null;
}

private String buildAuthorizationHeader(Response response) throws IOException {
processChallenge("WWW-Authenticate", response.header("WWW-Authenticate"));
return generateDigestHeader(response);
}

private void processChallenge(String headerName, String headerValue) {
try {
mDigestScheme.processChallenge(new BasicHeader(headerName, headerValue));
} catch (MalformedChallengeException e) {
Timber.e(e, "Error processing header " + headerName + " for DIGEST authentication.");
}
}

private String generateDigestHeader(Response response) throws IOException {
org.apache.http.HttpRequest request = new BasicHttpRequest(
response.request().method(),
response.request().uri().toString()
);

try {
return mDigestScheme.authenticate(mCredentials, request).getValue();
} catch (AuthenticationException e) {
Timber.e(e, "Error generating DIGEST auth header.");
return null;
}
}
}

验证器将在使用提供者构建的 OkHttpClient 中使用:

public class CustomClientProvider implements Client.Provider {
@Inject DigestAuthenticator mDigestAuthenticator;

@Override
public Client get() {
OkHttpClient client = new OkHttpClient();
client.setAuthenticator(mDigestAuthenticator);
return new OkClient(client);
}
}

最后在函数createRestAdapterBuilder中将客户端设置为RetrofitRobospice服务端:

public class ApiRetrofitSpiceService extends RetrofitJackson2SpiceService {
@Inject Client.Provider mClientProvider;

@Override
public void onCreate() {
App.get(this).inject(this);
super.onCreate();
addRetrofitInterface(NotificationRestInterface.class);
}

@Override
protected String getServerUrl() {
return Constants.Url.BASE;
}

@Override
protected RestAdapter.Builder createRestAdapterBuilder() {
return super.createRestAdapterBuilder()
.setClient(mClientProvider.get());
}
}

关于android - 需要帮助为 Android 创建摘要式身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9484571/

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