12

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 HttpPost as the REST API I'm using wants POST parameters rather than GET. (Only for some API calls though so I know that the GET calls were working fine so it's not a fault of the API).

Also, I tried using HttpPost on a simple php script I wrote that looks for a POST parameter 'var' and echoes it to screen, passing this parameters as follows worked fine:

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

    postMethod = new HttpPost("http://www.examplewebsite.com");

    nameValuePairs.add(new BasicNameValuePair("var", "lol"));

    try {
        postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        response = httpClient.execute(postMethod, responseHandler);
        Log.i("RESTMethod", response);
...

The problem is that when I tried and do the same call to the API (but with the params changed to the API params obviously) I get the following error:

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

The page I am requesting is an HTTPS page, could this be the problem?

But doing the same type of POST request to a raw HTTP page on the API gives the same error, unless I comment out the StringEntity part and then it runs (but returns xml and I want to pass a parameter to request the data in JSON).

This seems like a really strange problem (the non-https part) but couldn't really find any help on this problem so sorry if the answer is out there.

Any ideas?

Thanks in advance,

Infinitifzz

EDIT: Okay I'm getting nowhere so I thought if I directed you to the API it might shed some light, it's the 8Tracks API and as you can see you need to pass a dev key (api_key) for all requests and I the part I'm stuck on is using https to log a user in with: http://www.8tracks.com/sessions.xml" part.

Hope this helps somehow because I am at a dead end.

Thanks,

Infinitifizz

Infiniti Fizz
  • 1,726
  • 4
  • 24
  • 40
  • Shouldn't your url start with https for a start. Then, if it is https, it will likely be secured with a certificate or some other authentication challenge - as indicated in the error message. – John J Smith May 24 '11 at 18:06
  • I just put that url in as an example but yes it should. But as I said I tried to HttpPost my own script which was fine and if I tried to HttpPost a non-https part of the api as in their example called to an http page not https, I only got the error if I passed the nameValuePairs, if I didn't I didn't get the authentication error. – Infiniti Fizz May 24 '11 at 18:25
  • Can you show us your HttpClient constructor? – Ryan May 24 '11 at 18:27
  • It's just HttpClient httpClient = new DefaultHttpClient(); But I tried setting it up for SSL using the code here: http://thinkandroid.wordpress.com/2009/12/31/creating-an-http-client-example/ But I still got the same error. – Infiniti Fizz May 24 '11 at 18:38

4 Answers4

13

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.

ok2c
  • 26,450
  • 5
  • 63
  • 71
3

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.

Ryan
  • 392
  • 2
  • 11
  • Hmm okay I'll have a go, I'm just mainly baffled because using HttpGet it worked fine (the non-https stuff) and then suddenly it's talking about authentication when I use HttpPost. Thanks I'll give this a try though. – Infiniti Fizz May 24 '11 at 19:27
  • Hmm that doesn't seem to work either but thanks for trying. I suppose I'll contact the API authors and see what they suggest. Thanks anwyay. – Infiniti Fizz May 28 '11 at 17:59
  • Sorry to hear that. I see you posted the API which obviously shows https is supported so I'm not sure what to say. I built a Java library that lets users connect to CouchDB databases via http or https which uses a lot of the same concepts. If it helps, you can poke through it [here](https://github.com/ryancutter/BarcaJolt/blob/master/BarcaJolt.java). – Ryan Jun 05 '11 at 14:04
3

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.

ernazm
  • 9,208
  • 4
  • 44
  • 51
1

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

Sarp Centel
  • 1,283
  • 2
  • 14
  • 23