无法使用 HttpClient 获得 'location' header 作为响应

Cannot get 'location' header in response using HttpClient

位置header在那里,我可以在浏览器中看到它:

我正在使用 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);

```

我在谷歌上搜索了很多并尝试了 enable/disable 自动重定向,但它似乎不适用于 me.So 谁能告诉我如何获取位置 header 作为响应就像浏览器一样?

您看不到 'location' header,因为 HttpClient 会立即跟随该重定向 - 甚至 给您该响应之前。

在设置 HttpClient 时尝试disabling redirect

HttpClient instance = HttpClientBuilder.create().disableRedirectHandling().build();

选中此 URL,您将 看到 位置 Header:

URI uri = new URIBuilder().setScheme("https").setHost("www.googlemail.com").setPath("/").build();

我发现了我真正的问题...我没有在我的代码中通过身份验证过程,所以我一直得到 oauth2 page.After 我在我的请求中设置了所有 headers 就像浏览器做到了,最后我得到了正确的响应。