Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
439 views
in Technique[技术] by (71.8m points)

https - Authentication Error when using HttpPost with DefaultHttpClient on Android

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...