我正在尝试使用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 nameValuePairs = new ArrayList(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) {

//

}

我的代码出了什么问题?

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐