NutzCN Logo
问答 http请求响应中没有Location字段?
发布于 2885天前 作者 Leewell1989 4535 次浏览 复制 上一个帖子 下一个帖子
标签:
@Test
	public void testHeader(){
           
           Http http = new Http();
           Response resp = http.get("http://www.jsgsj.gov.cn:58888/province/infoQueryServlet.json?pt&c=75B161B67BE862B17AE530F7EF775C053FF98D74A4DA979B34CA5EE6C2A4A474E8E3D7F41B3E9A2C48594245350BD6F5C528B8D5CCC43FCE4A6E4436E46BBAA1", 2000);
           Header header = resp.getHeader();
           System.out.println(header.get("Location"));

	}

console output:
```
null

```

21 回复

debug 的 header对象:
```json
{
"null": "HTTP/1.1 200 OK",
"Date": "Thu, 22 Dec 2016 09:19:25 GMT",
"Content-Length": "9388",
"Set-Cookie": "BIGipServerpool_gsp=3523127488.25353.0000; path=/",
"Content-Type": "text/html; charset=UTF-8",
"X-Powered-By": "Servlet/2.4 JSP/2.0",
"Cache-Control": "no-cache="set-cookie""
}

```

因为默认是跟随重定向

Response resp = Sender.create(Request.get(url)).setFollowRedirects(false).send();

debug 的 header对象:
```json
{
"null": "HTTP/1.1 200 OK",
"Date": "Thu, 22 Dec 2016 09:19:25 GMT",
"Content-Length": "9388",
"Set-Cookie": "BIGipServerpool_gsp=3523127488.25353.0000; path=/",
"Content-Type": "text/html; charset=UTF-8",
"X-Powered-By": "Servlet/2.4 JSP/2.0",
"Cache-Control": "no-cache="set-cookie""
}

```

已经给出示例代码

试下

markdown代码不支持json吗?

支持,只是需要在代码块之前空一行.

试了下,还是null

@Test
	public void testHeader() {

		String url = "http://www.jsgsj.gov.cn:58888/province/infoQueryServlet.json?pt&c=75B161B67BE862B17AE530F7EF775C053FF98D74A4DA979B34CA5EE6C2A4A474E8E3D7F41B3E9A2C48594245350BD6F5C528B8D5CCC43FCE4A6E4436E46BBAA1";
		Response resp = Sender.create(Request.get(url))
				.setFollowRedirects(false).send();
		Header header = resp.getHeader();
		System.out.println(header.get("Location"));

	}

debug:

{
   "null": "HTTP/1.1 200 OK",
   "Date": "Thu, 22 Dec 2016 09:14:27 GMT",
   "Content-Length": "9389",
   "Set-Cookie": "BIGipServerpool_gsp=3539904704.25353.0000; path=/",
   "Content-Type": "text/html; charset=UTF-8",
   "X-Powered-By": "Servlet/2.4 JSP/2.0",
   "Cache-Control": "no-cache=\"set-cookie\""
}

为什么我不能贴图片上去?只好贴文字了,chrome里边监控到的是这样的:
connection:close
Content-Type:text/html;charset=utf-8
Date:Thu, 22 Dec 2016 07:32:37 GMT
Location:http://www.jsgsj.gov.cn:58888/ecipplatform/jiangsu.jsp?org=DBBF2B3C27C5E35AC652C56F1A7BBB21&id=F140F3790F379BA49E3D577569304F94&seqId=15549DD3C4F1EF908D7C130097703D1E&activeTabId=
Transfer-Encoding:chunked
X-Powered-By:Servlet/2.4 JSP/2.0

想获得其中的Location

难道是User-Agent拦截了? 我试试

我晕,我知道了... GET请求没设置...

提交了?我拉下来试下

github是最新的代码吗?

我是拉下来最新代码,里边有些自己加入进去的东西,然后打包使用的。

@leewell1989 哦,好的,按那个commit改一下就好了

这什么鬼,我运行你的test没问题,打包出来在我自己工程下边运行test就不行...

@leewell1989 总共是删一行,加2行哦

添加回复
请先登陆
回到顶部