【问题标题】:Cannot get 'location' header in response using HttpClient无法使用 HttpClient 获取“位置”标头作为响应
【发布时间】:2015-12-22 10:43:56
【问题描述】:

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

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

```

我搜索了很多并尝试启用/禁用自动重定向,但它似乎对我不起作用。那么有人可以告诉我如何像浏览器一样获取位置标头作为响应吗?

【问题讨论】:

  • 请分享您用于尝试禁用重定向的代码。

标签: java http apache-httpcomponents


【解决方案1】:

您看不到“位置”标头,因为 HttpClient 立即跟随该重定向 - 甚至 给您该响应之前。

在设置 HttpClient 时尝试disabling redirect

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

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

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

【讨论】:

  • api.weibo.com/oauth2/…我得到了这个页面。我使用HttpClient发送请求时好像没有授权。
  • 好的 - 我用浏览器和代码试过你的代码,两次都得到响应 200。
  • 所以不是 HttpClient 没有接收到location Header,而是服务器没有发送它
  • 所以我不能使用 cookie 跳过身份验证过程?
  • 这是一个全新的问题...尝试设置 all 浏览器在代码中发送的标头。
【解决方案2】:

我发现了我真正的问题......我没有在我的代码中通过身份验证过程,所以我不断得到 oauth2 页面。在我像浏览器一样设置我的请求中的所有标题之后,最后我得到了正确的反应。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-12
    • 1970-01-01
    • 2019-03-04
    • 2020-01-02
    • 1970-01-01
    • 2019-10-27
    • 1970-01-01
    相关资源
    最近更新 更多