Android HttpClient 身份验证总是返回 401 代码 [英] Android HttpClient authentication always returns 401 code
本文介绍了Android HttpClient 身份验证总是返回 401 代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 HTTP 身份验证在线发布玩家得分,但响应始终为 401(未经授权),尽管我很确定用户名和密码是正确的.我做错了什么?
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) {
//
}
我的代码有什么问题?
推荐答案
我解决了这个问题.我正在使用
I solved the problem. I'm using
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT) // works
代替上一个
new AuthScope("http://myurl.com/score.php", 80) // error
这篇关于Android HttpClient 身份验证总是返回 401 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文