无法使用HttpClient获得“ location”标头作为响应

Ara*_*ell 5 java http apache-httpcomponents

位置标题在那里,我可以在浏览器中看到它: 在此处输入图片说明

我正在使用org.apache.httpcomponents.httpclient发送带有cookie的http请求:

```

URI uri = new URIBuilder().setScheme("https").setHost("api.weibo.com").setPath("/oauth2/authorize").setParameter("client_id","3099336849").setParameter("redirect_uri","http://45.78.24.83/authorize").setParameter("response_type", "code").build();
HttpGet req1 = new HttpGet(uri);
RequestConfig config = RequestConfig.custom().setRedirectsEnabled(false).build();
req1.setConfig(config);
req1.setHeader("Connection", "keep-alive");
req1.setHeader("Cookie", cookie);
req1.setHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36");
response = httpclient.execute(req1);
Run Code Online (Sandbox Code Playgroud)

```

我在Google上搜索了很多,并尝试启用/禁用自动重定向,但是它似乎对我不起作用。

Jan*_*Jan 2

您看不到“location”标头,因为 HttpClient 立即遵循该重定向 - 甚至在给您该响应之前。

尝试在设置 HttpClient 时禁用重定向:

HttpClient instance = HttpClientBuilder.create().disableRedirectHandling().build();
Run Code Online (Sandbox Code Playgroud)

检查此 URL,您将看到位置标头:

URI uri = new URIBuilder().setScheme("https").setHost("www.googlemail.com").setPath("/").build();
Run Code Online (Sandbox Code Playgroud)