Android HttpClient 身份验证始终返回 401 代码

发布于 2024-10-22 04:32:35 字数 1013 浏览 3 评论 0原文

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

我的代码有什么问题吗?

I'm trying to post the player score on-line using HTTP authentication but the response is always 401 (Unauthorized), although I'm pretty sure the username and password is correct. What am I doing wrong?

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) {
    //
}

What's wrong with my code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

假装爱人 2024-10-29 04:32:35

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

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

而不是以前的

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

I solved the problem. I'm using

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

instead of the previous

new AuthScope("http://myurl.com/score.php", 80) // error
橘亓 2024-10-29 04:32:35

尝试使用:

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

Try using:

new AuthScope("myurl.com", 80);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文