【发布时间】:2016-12-07 09:15:52
【问题描述】:
我正在使用 HttpURLConnection 类来管理我的 Android 应用程序中的 HTTP 连接。问题是,在某些服务器上,我有一个代码 401 使用在 Internet 导航器上工作的 URL。我使用基本身份验证初始化连接,但我认为这些服务器需要其他模式。我知道它适用于 libCURL 和以下指令:
curl_easy_setopt(pCurlHandle, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
但是有没有办法在 Android 上做类似的事情?目前,我这样做:
...
connection.setRequestProperty("Authorization", "Basic " + Base64.encode("username:password"));
...
我也试过Authenticator:
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("username", "password".toCharArray());
}
});
但我的应用中仍有 401。有什么想法吗?
【问题讨论】:
标签: java httpurlconnection http-authentication