当发生HTTP重定向时,apache HttpClient从第一个响应而不是最后一个响应返回标头

时间:2019-06-20 02:42:33

标签: java apache http https

这是我的Java程序

DT$ID2 <- sapply(regmatches(DT$ID, gregexpr("\\bUS\\S*", DT$ID)), toString)

查看Java程序输出的代码段。状态行显示为HTTP / 1.1 200 OK。但是,打印的标题字段与手动运行curl时得到的内容不匹配。它似乎是从第一个响应中获取标头值,而不是从最后一个响应中获取标头值。最后一个响应中出现的甚至更重要的“内容长度”字段也未设置响应结构。

private static final String SAMPLE_URL = "https://www.dropbox.com/s/<something>/test_out4.mp4"; public static void main(String[] args) throws IOException, URISyntaxException { HttpClient client = HttpClientBuilder.create().build(); HttpHead request = new HttpHead(new URI(SAMPLE_URL)); HttpResponse response = client.execute(request); System.out.println(response.getStatusLine()); for (Header header : response.getAllHeaders()) { System.out.println(header.getName() + ": " + header.getValue()); } }

当我运行curl时,输出正确。 HttpClient中是否有任何设置可以返回最新的标头?

HTTP/1.1 200 OK <<< Status is 200 Server: nginx Date: Thu, 20 Jun 2019 02:22:58 GMT Content-Type: text/html; charset=utf-8 << Content type is char

....

curl -I https://www.dropbox.com/s/<something>/test_out4.mp4 HTTP/1.1 301 Moved Permanently <<< Status 301 Server: nginx Date: Thu, 20 Jun 2019 02:20:50 GMT Content-Type: text/html; charset=utf-8 <<< Content type text Connection: keep-alive

....

HTTP/1.1 302 Found << second redirect Server: nginx Date: Thu, 20 Jun 2019 02:36:03 GMT Content-Type: text/html; charset=utf-8

0 个答案:

没有答案