Android httpclient - 通过抢占式身份验证获取文件

发布于 2024-09-03 13:11:09 字数 661 浏览 2 评论 0 原文

我在使用此示例代码获取网站的 HTML 代码时遇到问题。

http://svn.apache.org/repos/asf/httpcomponents/httpclient/branches/4.0.x/httpclient/src/examples/org/apache/http/examples/client/ClientPreemptiveBasicAuthentication.java

我必须使用上面的,因为我需要在 Android 中对我的应用程序进行抢先身份验证。

我认为解决方案可能是 httpget 对象,但我仍然只得到错误:( cf HttpGet httpget = new HttpGet("/index.html");

有谁知道如何获取文件的内容,该内容是使用链接的示例代码在上面的 httpget 对象中指定的。它肯定打电话了,但现在我只能获取状态代码等等...

谢谢 4 帮助

I have problems getting the HTML code of a website by using this example code.

http://svn.apache.org/repos/asf/httpcomponents/httpclient/branches/4.0.x/httpclient/src/examples/org/apache/http/examples/client/ClientPreemptiveBasicAuthentication.java

I have to use the above one, as I need preemptive authentication for my application in Android.

I thought the solution might be the httpget object, but I still get only errors :(
c.f. HttpGet httpget = new HttpGet("/index.html");

Does anybody have an idea how to get the content of the file, which is specified in the httpget object above using the example code of the link. It definitely called, but for now I can only get status code and so on ...

Thanks 4 help

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

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

发布评论

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

评论(4

深府石板幽径 2024-09-10 13:11:09

当我去年解决这个问题时,我放弃了 HttpClient 的本机抢占式 HTTP 身份验证,只是 我自己滚动了标题

When I tackled this last year, I gave up on HttpClient's native pre-emptive HTTP authentication and just rolled the header myself.

双手揣兜 2024-09-10 13:11:09

替代方案1:请阅读 Android 的 Http Basic Authentication 提出了一个基于 的解决方案HttpClient 4 官方文档。我还没有亲自测试过,所以我很高兴知道它是否真的有效。

编辑:我刚刚尝试过,它的效果就像一个魅力。

替代方案 2
您还可以按照 @CommonsWare 的建议添加“授权”HTTP 标头:

post.addHeader("Authorization", "Basic " + Base64.encode(username+":"+password));

在这种情况下,您需要 Base64 编码器来对包含用户名和密码的字符串进行编码。你可以在互联网上找到很多实现。

Alternative 1: Please read Http Basic Authentication with Android that proposes a solution based on the HttpClient 4 official docs. I've not tested it by myself, so I'd be happy to know if it really works.

Edit: I've just tried it and it works like a charm.

Alternative 2:
You can also add the "Authorization" HTTP header as proposed by @CommonsWare:

post.addHeader("Authorization", "Basic " + Base64.encode(username+":"+password));

In this case you need a Base64 encoder to encode the string containing the username and the password. You can find a lot of implementations in the Internet.

冷心人i 2024-09-10 13:11:09

对我来说,上面的例子在 Android 上不起作用。我必须执行以下操作:

post.addHeader("Authorization", "Basic " + Base64.encodeToString((username+":"+password).getBytes(),Base64.NO_WRAP));

For me the example above didn't work on Android. I had to do the following:

post.addHeader("Authorization", "Basic " + Base64.encodeToString((username+":"+password).getBytes(),Base64.NO_WRAP));
破晓 2024-09-10 13:11:09

谢谢珍妮克斯。

我必须在 Android 上做同样的事情。

post.addHeader("Authorization", "Basic " + Base64.encodeToString((username+":"+password).getBytes(),Base64.NO_WRAP));

干杯

Thanks janex.

I had to do the same on Android.

post.addHeader("Authorization", "Basic " + Base64.encodeToString((username+":"+password).getBytes(),Base64.NO_WRAP));

cheers

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