HttpClient Authentication Antenna Always Returns 401

I am trying to publish a player’s score online using HTTP authentication, but the response is always 401 (unauthorized), although I am sure that the username and password are 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 is wrong with my code?

+5
source share
2 answers

I solved the problem. I use

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

instead of the previous

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

Try using:

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

All Articles