我在使用此示例代码获取网站的 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
发布评论
评论(4)
当我去年解决这个问题时,我放弃了 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.
替代方案1:请阅读 Android 的 Http Basic Authentication 提出了一个基于 的解决方案HttpClient 4 官方文档。我还没有亲自测试过,所以我很高兴知道它是否真的有效。
编辑:我刚刚尝试过,它的效果就像一个魅力。
替代方案 2:
您还可以按照 @CommonsWare 的建议添加“授权”HTTP 标头:
在这种情况下,您需要 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:
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.
对我来说,上面的例子在 Android 上不起作用。我必须执行以下操作:
For me the example above didn't work on Android. I had to do the following:
谢谢珍妮克斯。
我必须在 Android 上做同样的事情。
干杯
Thanks janex.
I had to do the same on Android.
cheers