Android的HttpClient的认证总是返回401 code [英] Android HttpClient authentication always returns 401 code

查看:785
本文介绍了Android的HttpClient的认证总是返回401 code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用HTTP认证后球员得分上线,但反应总是401,虽然我pretty的确认用户名和密码是正确的。我究竟做错了什么?

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

有什么毛病我的code?

What's wrong with my code?

推荐答案

我解决了这个问题。我使用的是

I solved the problem. I'm using

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

代替previous

instead of the previous

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

这篇关于Android的HttpClient的认证总是返回401 code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆