在 Android 上使用 HttpPost 和 DefaultHttpClient 时出现身份验证错误

发布于 2024-11-09 03:52:15 字数 1448 浏览 0 评论 0原文

我在使用 HttpClient 时遇到了一个奇怪的问题。我将 DefaultHttpClient() 与 HttpPost 一起使用。我使用 HttpGet 取得了 100% 的成功,但现在尝试切换到 HttpPost,因为我使用的 REST API 需要 POST 参数而不是 GET。 (不过,仅适用于某些 API 调用,因此我知道 GET 调用工作正常,因此这不是 API 的错误)。

另外,我尝试在我编写的一个简单的 php 脚本上使用 HttpPost,该脚本查找 POST 参数“var”并将其回显到屏幕,按如下方式传递此参数效果很好:

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);
...

问题是,当我尝试对API(但显然参数已更改为 API 参数)我收到以下错误:

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

我请求的页面是 HTTPS 页面,这可能是问题吗?

但是,对 API 上的原始 HTTP 页面执行相同类型的 POST 请求会产生相同的错误,除非我注释掉 StringEntity 部分,然后它会运行(但返回 xml,并且我想传递一个参数来请求 JSON 中的数据) 。

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

有什么想法吗?

提前致谢,

Infinitifzz

编辑:好吧,我一无所获,所以我想如果我引导你使用 API,它可能会带来一些启发,它是 8Tracks API ,正如你所看到的,你需要为所有请求传递一个开发密钥(api_key),而我所坚持的部分是使用 https 来登录用户: http://www.8tracks.com/sessions.xml" 部分。

希望这会有所帮助,因为我正处于死胡同。

谢谢,

无限菲兹

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

听,心雨的声音 2024-11-16 03:52:15

身份验证错误:无法
应对以下任何挑战:{}

此错误消息意味着服务器响应了 401(未经授权) 状态代码,但未能提供单个身份验证质询 (WWW-Authenticate header) 从而使 HttpClient 无法自动从身份验证失败中恢复。

最有可能的应用程序需要 HTTP POST 请求中包含的 HTML 表单中的一些软凭据。

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.

烟燃烟灭 2024-11-16 03:52:15

不需要声明端口和协议吗?我只是炫耀这段代码,所以如果它没有立即正确编译,请不要沮丧。另外,我通常向我的 setCredentials() 提供 UsernamePasswordCredentials 但我想它是相同的。

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]);

有关 setCredentials 的更多信息 这里

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.

随梦而飞# 2024-11-16 03:52:15

以下是我遇到类似问题的方法:

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

感谢 Ryan 的正确指导。

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.

执手闯天涯 2024-11-16 03:52:15

没有为我的 Twitter 应用程序指定回调 URL 导致了同样的错误:

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

在 Twitter 上设置回调 URL 修复了问题

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文