android 1.6 httpresponse 不包含位置标头

发布于 2024-10-08 05:18:23 字数 587 浏览 5 评论 0原文

我有一个 android 1.6 应用程序,我正在尝试获取 HTTP GET 响应的位置标头。但是,当我调用 getLastHeader("location") 时,它返回 null。我什至通过调试器进入响应变量,并且没有发送位置标头。我需要在任意网站上执行此操作,但使用谷歌进行测试仍然会产生事故。我认为位置是要读取的默认标题? android 1.6不读取位置标头吗?我的代码如下:

HttpClient httpClient = DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://google.com");
try
{
 HttpResponse response = httpClient.execute(httpGet);
 if(response.getStatusLine().getStatusCode() == 200)
 {
  Header h = response.getLastHeader("location");
  String location = h.getValue();
 }
}

非常感谢帮助:)我要么寻找解决方案来获取位置,要么解释为什么我没有得到它:/

i have an android 1.6 application that i'm trying to get the location header of a HTTP GET response. however, when i call getLastHeader("location") it returns null. i even go into the response variable via debugger and there is no location header sent. i need to do this on an arbitrary site, but using google for testing purposes still produces the mishap. i thought location was a default header to be read? does android 1.6 not read the location header? my code is as followed:

HttpClient httpClient = DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://google.com");
try
{
 HttpResponse response = httpClient.execute(httpGet);
 if(response.getStatusLine().getStatusCode() == 200)
 {
  Header h = response.getLastHeader("location");
  String location = h.getValue();
 }
}

help is greatly appreciated :) i'm either looking for a solution to get the location or an explanation why i'm not getting it :/

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

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

发布评论

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

评论(1

要走就滚别墨迹 2024-10-15 05:18:23

来自 HTTP RFC,第 14.30 点

位置响应头字段是
用于将收件人重定向到
请求 URI 以外的位置
为了完成请求或
新资源的识别。为了
201(已创建)响应,位置
是新资源的资源
由请求创建。对于 3xx
响应,位置应该
指示服务器的首选 URI
自动重定向到
资源。该字段值包括
单个绝对 URI。

因此,如果您的响应状态为 200,则不应设置 Location 标头,因此您的值为 null。

您还可以使用 Firebug 检查返回的标头进行检查。

From the HTTP RFC, Point 14.30

The Location response-header field is
used to redirect the recipient to a
location other than the Request-URI
for completion of the request or
identification of a new resource. For
201 (Created) responses, the Location
is that of the new resource which was
created by the request. For 3xx
responses, the location SHOULD
indicate the server's preferred URI
for automatic redirection to the
resource. The field value consists of
a single absolute URI.

So if your response status is 200, the Location header should not be set, hence the null you have.

You can also check by using Firebug to check the header returned.

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