Authentication Error when using HttpPost with DefaultHttpClient on Android

后端 未结 4 1443
余生分开走
余生分开走 2020-12-16 16:20

I\'m running into a strange problem using HttpClient. I am using a DefaultHttpClient() with HttpPost. I was using HttpGet with 100% success but now trying to switch to HttpP

相关标签:
4条回答
  • 2020-12-16 16:58

    Don't you have to declare the port and protocol? I'm just swagging this code so please don't be upset if it doesn't immediatley compile correctly. Also, I usually supply a UsernamePasswordCredentials to my setCredentials() but I imagine it's the same.

    HttpHost host = new HttpHost("www.foo.com", 443, "https");
    
    // assemble your GET or POST
    
    client.getCredentialsProvider().setCredentials(new AuthScope(host.getHostName(), host.getPort()));
    
    HttpResponse response = client.execute(host, [HttpPost or HttpGet]);
    

    More info about setCredentials here.

    0 讨论(0)
  • 2020-12-16 17:06

    Authentication error: Unable to respond to any of these challenges: {}

    This error message means that the server responded with 401 (Unauthorized) status code but failed to provide a single auth challenge (WWW-Authenticate header) thus making it impossible for HttpClient to automatically recover from the authentication failure.

    Most likely application expects some soft of credentials in the HTML form enclosed in the HTTP POST request.

    0 讨论(0)
  • 2020-12-16 17:12

    Not specifying a Callback URL for my Twitter App resulted in the same error for me:

    Authentication error: Unable to respond to any of these challenges: {oauth=WWW-Authenticate: OAuth realm="https://api.twitter.com"}
    

    Setting a callback URL on Twitter fixed the problem

    0 讨论(0)
  • 2020-12-16 17:14

    Here's how I ended up with similar problem:

    DefaultHttpClient client = new DefaultHttpClient();
    client.getCredentialsProvider().setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials(username, password));
    

    Thanks to Ryan for right direction.

    0 讨论(0)
提交回复
热议问题