【发布时间】:2014-07-23 00:55:26
【问题描述】:
对于特定的 HTTPS URL,以下 java 代码无法正确检索 Content-Type:
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
String mimeType = connection.getContentType();
if (mimeType == null) throw new Exception("Could not detemine mimetype for remote URL.");
但是,如果我使用 -I 卷曲 URL,我会得到以下信息:
HTTP/1.1 200 OK
Date: Wed, 23 Jul 2014 00:50:01 GMT
Server: Apache/2.4.7 (Ubuntu)
Cache-Control: max-age=0, no-cache
Connection: close
Content-Type: text/html
此外,如果我在 URL 不是 HTTPS 的情况下运行 Java 代码,getContentType 会成功检索 mime 类型。最后,Java 代码确实适用于通过 HTTPS 连接的其他服务器。
【问题讨论】:
-
所以那个特定的 URL 有一些奇怪的地方。服务器配置。除了处理缺少的内容类型之外,您在客户端无能为力。
-
对,但是服务器上的什么会导致这种情况?还有为什么 curl 可以看到 header 而 java.net 看不到?
-
您是否使用过wireshark(或类似工具)来查看线路上发生了什么?您是否尝试过在获取内容类型之前获取响应代码? curl 和 java provide 是否可能在请求的 Accept 标头中提供不同的值?