验证错误时DefaultHttpClient在Android上使用HttpPost [英] Authentication Error when using HttpPost with DefaultHttpClient on Android

查看:143
本文介绍了验证错误时DefaultHttpClient在Android上使用HttpPost的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个奇怪的问题使用HttpClient的。我使用的是DefaultHttpClient()与HttpPost。我使用HTTPGET用100%的成功,但现在尝试切换到HttpPost因为我使用REST API想POST参数而不是GET。 (仅适用于某些API调用,虽然让我知道,所以它不是该API的故障GET电话都是工作的罚款)。

另外,我尝试了一个简单的PHP脚本我写了寻找一个POST参数'变种'和它相呼应筛选,通过如下工作正常此参数使用HttpPost:

 名单,其中,的NameValuePair> namevaluepairs中=新的ArrayList<的NameValuePair>();

    的PostMethod =新HttpPost(http://www.examplewebsite.com);

    nameValuePairs.add(新BasicNameValuePair(变种,笑));

    尝试 {
        postMethod.setEntity(新UrlEn codedFormEntity(namevaluepairs中));

        响应= httpClient.execute(后方法,ResponseHandler所);
        Log.i(RESTMethod,响应);
...
 

现在的问题是,当我试图做同样的调用API(但与PARAMS改为API PARAMS明显)我得到以下错误:

 认证错误:无法对任何应对这些挑战:{}
 

我请求的页面是HTTPS页面,可这是什么问题?

但在做同一类型的POST请求到原始的HTTP页面上的API给出了同样的错误,除非我注释掉StringEntity一部分,然后它运行(但返回XML,我想传递一个参数来请求数据在JSON)。

这似乎是一个很奇怪的问题(非HTTPS一部分),但无法真正找到在这个问题上非常抱歉任何帮助,如果答案是有。

任何想法?

在此先感谢,

Infinitifzz

编辑:好的,我一事无成,所以我想,如果我针对你可能闪耀光芒的API,它的 8Tracks API 正如你可以看到你需要传递一个开发密钥(API_KEY)的所有请求,我的部分我卡上使用HTTPS与登录用户: http://www.8tracks.com/sessions.xml 的一部分。

希望这有助于以某种方式,因为我在一个死胡同。

谢谢

Infinitifizz

解决方案
  

认证错误:无法   任何这些挑战:{}

此错误消息意味着服务器回应 401 状态code,但未能提供一个权威性的挑战( WWW -Authenticate 头)因此,根本不可能进行的HttpClient从认证失败中自动恢复。

最有可能的应用程序期望一些软的封装在HTTP POST请求的HTML表单的凭据。

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

解决方案

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.

这篇关于验证错误时DefaultHttpClient在Android上使用HttpPost的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆