最近我遇到了以下问题:我有一个 webview 以异步方式从 HttpURLConnection 加载数据,现在说您需要用户提供凭据,以便在 http 连接期间继续进行基本身份验证。您不知道 url 是否需要身份验证,您只会从响应中收到 401 错误。是否有适当的方法来暂停异步线程,以便您可以在不停止线程的情况下向用户询问凭据( px.by 一个警报对话框)?
URL requestedUrl = new URL(url);
urlConnection = (HttpURLConnection) requestedUrl.openConnection(proxy);
if (urlConnection instanceof HttpsURLConnection) {
((HttpsURLConnection) urlConnection)
.setSSLSocketFactory(sslContext.getSocketFactory());
}
String userandpass=username+":"+password;
String basicAuth = "Basic "
+ new String(Base64.encode(
userandpass
.getBytes(), Base64.NO_WRAP));
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.setRequestProperty("Authorization", basicAuth);
result = IOUtil.readFully(urlConnection.getInputStream());