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

5
我正在尝试使用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) {
    //
}

我的代码有什么问题?

2个回答

12

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

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

与之前不同的是

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

太好了!您可以接受自己的答案,这样问题就会显示为已解决 :) - Alex Florescu
感谢您发布解决方案。这帮助我解决了我的问题 :-) - Lasse Samson
很遗憾,这个解决方案不适用于SSL连接。 - IgorGanapolsky

0

尝试使用:

new AuthScope("myurl.com", 80);

请尝试在您的答案中提供一些解释和/或链接,以解释为什么您的解决方案应该有效。 - codeMagic

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接