Android HttpClient 身份验证总是返回 401 代码

标签 android authentication post httpclient

我正在尝试使用 HTTP 身份验证在线发布玩家分数,但响应始终是 401(未经授权),尽管我非常确定用户名和密码是正确的。我做错了什么?

DefaultHttpClient client = new DefaultHttpClient();
Credentials creds = new UsernamePasswordCredentials("user", "passsword");
client.getCredentialsProvider().setCredentials(new AuthScope("http://myurl.com/score.php", 80), creds);

try {
    HttpPost post = new HttpPost("http://myurl.com/score.php");

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);   
    nameValuePairs.add(new BasicNameValuePair("score", points));
    nameValuePairs.add(new BasicNameValuePair("name", name));
    post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse response = client.execute(post);
    final int statusCode = response.getStatusLine().getStatusCode();
    if (statusCode != HttpStatus.SC_OK) {
        throw new Exception();
    }

} catch (UnsupportedEncodingException e) {
    //
} catch (IOException e) {
    //
} catch (Exception e) {
    //
}

我的代码有什么问题?

最佳答案

我解决了这个问题。我正在使用

new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT) // works

代替以前的

new AuthScope("http://myurl.com/score.php", 80) // error

关于Android HttpClient 身份验证总是返回 401 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5283996/

相关文章:

android - 无法在 recyclerview 中加载来自 picasso 的图像?

android - 在 oreo 8.1.0 中我无法获取 fcm 设备 id

python - 为 Web 应用程序使用的 Python REST API 选择适当的身份验证类

php - 从表中获取 $_POST 变量

asp.net - 从 ASP.NET 向另一个 URL 发送请求

android - 如何从命令行发送带权限的广播

android - 是否有任何支持 Android HCE 的 NFC 模块/盾牌?

azure - 来自 SharePoint Online 的外部 REST 服务身份验证

php - 有人使用过这个 Highrise API PHP Wrapper 库吗?我需要帮助验证

ajax - 传统:true option and false in Ajax call?和有什么不一样