java - android 1.6 httpresponse 不包含位置 header

标签 java android http-headers location

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

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();
 }
}

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

最佳答案

来自HTTP RFC ,第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.

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

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

关于java - android 1.6 httpresponse 不包含位置 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4467459/

相关文章:

java - XSL 多列切换

java - 从文件系统加载对象后更新 GUI

java - Firebase 单值事件监听器不返回

java - 尽管设置了必要的属性,工具栏小部件中的 TextView 选取框仍无法工作

android - 从照片 url 设置一个 ImageView

servlets - Websphere 8.5 Liberty Profile - Servlet API setLocale

java - 为什么我的 Web 服务为大多数移动浏览器生成 XML 结果而不是 HTML 结果?

java - 在 OSGI 应用程序 Mac OS X - Java 1.7 中引用时 ImageIO 挂起

java - 当我初始化扩展 fragment 时,如何停止调用父 fragment 的 onActivityCreated ?

ajax - 我可以添加 Access-Control-Allow-Origin : * to the default headers