- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我遇到了以下问题:当通过代理使用 URLConnection
时,内容长度始终设置为 -1
。
首先,我检查了代理是否真的返回了Content-Length
(lynx
和wget
也通过代理工作;没有其他方法从本地网络访问互联网):
$ lynx -source -head ftp://ftp.wipo.int/pub/published_pct_sequences/publication/2003/1218/WO03_104476/WO2003-104476-001.zip
HTTP/1.1 200 OK
Last-Modified: Mon, 09 Jul 2007 17:02:37 GMT
Content-Type: application/x-zip-compressed
Content-Length: 30745
Connection: close
Date: Thu, 02 Feb 2012 17:18:52 GMT
$ wget -S -X HEAD ftp://ftp.wipo.int/pub/published_pct_sequences/publication/2003/1218/WO03_104476/WO2003-104476-001.zip
--2012-04-03 19:36:54-- ftp://ftp.wipo.int/pub/published_pct_sequences/publication/2003/1218/WO03_104476/WO2003-104476-001.zip
Resolving proxy... 10.10.0.12
Connecting to proxy|10.10.0.12|:8080... connected.
Proxy request sent, awaiting response...
HTTP/1.1 200 OK
Last-Modified: Mon, 09 Jul 2007 17:02:37 GMT
Content-Type: application/x-zip-compressed
Content-Length: 30745
Connection: close
Age: 0
Date: Tue, 03 Apr 2012 17:36:54 GMT
Length: 30745 (30K) [application/x-zip-compressed]
Saving to: `WO2003-104476-001.zip'
我在 Java 中写道:
URL url = new URL("ftp://ftp.wipo.int/pub/published_pct_sequences/publication/2003/1218/WO03_104476/WO2003-104476-001.zip");
int length = url.openConnection().getContentLength();
logger.debug("Got length: " + length);
我得到 -1
。我开始调试 FtpURLConnection
,结果发现必要的信息在底层 HttpURLConnection.responses
字段中,但是它从未从那里正确填充:
( header 中有 Content-Length: 30745
)。当您开始读取流时或什至在读取流后,内容长度不会更新。代码:
URL url = new URL("ftp://ftp.wipo.int/pub/published_pct_sequences/publication/2003/1218/WO03_104476/WO2003-104476-001.zip");
URLConnection connection = url.openConnection();
logger.debug("Got length (1): " + connection.getContentLength());
InputStream input = connection.getInputStream();
byte[] buffer = new byte[4096];
int count = 0, len;
while ((len = input.read(buffer)) > 0) {
count += len;
}
logger.debug("Got length (2): " + connection.getContentLength() + " but wanted " + count);
输出:
Got length (1): -1
Got length (2): -1 but wanted 30745
好像是JDK6的bug,所以新开bug#7168608 .
file:/
URL 返回正确的内容长度,我将不胜感激。最佳答案
请记住,代理通常会更改底层实体的表示。在你的情况下,我怀疑代理可能正在改变传输编码。反过来,即使提供了 Content-Length 也毫无意义。
您违反了 HTTP 1.1 规范的以下两个部分:
- ...
- ...
- If a Content-Length header field (section 14.13) is present, its decimal value in OCTETs represents both the entity-length and the transfer-length. The Content-Length header field MUST NOT be sent if these two lengths are different (i.e., if a Transfer-Encoding header field is present). If a message is received with both a Transfer-Encoding header field and a Content-Length header field, the latter MUST be ignored.
The Transfer-Encoding general-header field indicates what (if any) type of transformation has been applied to the message body in order to safely transfer it between the sender and the recipient. This differs from the content-coding in that the transfer-coding is a property of the message, not of the entity.
Transfer-Encoding = "Transfer-Encoding" ":" 1#transfer-coding
Transfer-codings are defined in section 3.6. An example is:
Transfer-Encoding: chunked
If multiple encodings have been applied to an entity, the transfer- codings MUST be listed in the order in which they were applied. Additional information about the encoding parameters MAY be provided by other entity-header fields not defined by this specification.
Many older HTTP/1.0 applications do not understand the Transfer- Encoding header.
因此,根据规范,URLConnection 将忽略 Content-Length
header ,因为它在存在分块 传输时毫无意义
在您的调试器屏幕截图中,不清楚 Transfer-Encoding
header 是否存在。请让我们知道...
进一步调查 - 似乎当您发出 lynx -head
时,lynx 并未显示返回的所有 header 。它没有显示对本次讨论至关重要的 Transfer-Encoding
header 。
这是与公开可见的网站存在差异的证据
Ξ▶ lynx -useragent='dummy' -source -head http://www.bbc.co.uk
HTTP/1.1 302 Found
Server: Apache
X-Cache-Action: PASS (non-cacheable)
X-Cache-Age: 0
Content-Type: text/html; charset=iso-8859-1
Date: Tue, 03 Apr 2012 13:33:06 GMT
Location: http://www.bbc.co.uk/mobile/
Connection: close
Ξ▶ wget -useragent='dummy' -S -X HEAD http://www.bbc.co.uk
--2012-04-03 14:33:22-- http://www.bbc.co.uk/
Resolving www.bbc.co.uk... 212.58.244.70
Connecting to www.bbc.co.uk|212.58.244.70|:80... connected.
HTTP request sent, awaiting response...
HTTP/1.1 200 OK
Server: Apache
Cache-Control: private, max-age=15
Etag: "7e0f292b2e5e4c33cac1bc033779813b"
Content-Type: text/html
Transfer-Encoding: chunked
Date: Tue, 03 Apr 2012 13:33:22 GMT
Connection: keep-alive
X-Cache-Action: MISS
X-Cache-Age: 0
X-LB-NoCache: true
Vary: Cookie
由于我显然不在您的网络中,所以我无法复制您的确切情况,但请验证您真的在通过代理时没有获得 Transfer-Encoding header 。
关于java - URLConnection 无法通过代理正确处理内容长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9607290/
在我的程序中,我需要跟踪已打开的与某些 HTTP 服务器的连接列表 - 以便在需要时立即断开连接。 我遇到了以下问题。如果我连接到 HTTP 服务器,一切正常,但如果连接到 HTTPS,则连接不会从列
我正在尝试写信给 URLConnection#getOutputStream ,但是,在我调用 URLConnection#getInputStream 之前,实际上并没有发送任何数据。 .即使我设置
我有一组对象,我试图通过 POST 发送到 API。第一个对象将按原样通过,然后我得到: java.net.ProtocolException: cannot write request body a
我在尝试发送文本时遇到 URLConnection 编码问题。 我的代码是这样的: final URL url = new URL(urlString); final URLConnection ur
我有这种方法,可以从雅虎财经下载 .csv 文件并将其保存在本地。它是在循环期间访问的,因此它从列表中下载许多文件。然而,有时符号输入不正确、不再存在或连接超时。如何修改此方法,以便重试连接超时并跳过
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import
打开 URLConnection 时,我使用以下代码来获取内容长度,但它返回 -1。 URL url = new URL(sUrl[0]); URLConnection connection = ur
默认情况下,URLConnection 的超时为 0,这是无限制的。 XXXXX 的合理值是多少? URL url = ... URLConnection uCon = url.openConnect
我无法打开具有特定网络资源的 URLConnection。我得到了 “java.net.ConnectException:连接超时:”。是因为该域阻止了直接 URL 连接吗?如果是这样,他们是如何阻止
我看过文本文件示例,但是否以与 URLConnection 相同的方式将音频文件直接保存到服务器? 最佳答案 是的,一样的。尽管确保使用二进制输出流将内容写入磁盘。 类似于: URLConne
我一直在尝试从网页获取信息,特别是此网站:http://www.ncbi.nlm.nih.gov/pubmed?term=%22pulmonary%20disease%2C%20chronic%20o
在我使用 Apache 库 (org.apach.httpclient) 向带有参数 (BasicNameValuePair) 的 Php 脚本发出请求之前, 然后现在我想删除那些库以减小 APK 大
我正在使用 android 应用程序,我正在从 url 下载文件。一切正常,但是当互联网连接介于两者之间(打开连接后)时,下载超时永远不会发生并且连接永远不会结束。 给我一个解决这个问题的方案
我只是想了解一下 JacksonJson 库。为此,我尝试将 Places API 中的 JSON 数据转换为字符串。 我的 key 有效(我在浏览器和另一个应用程序中进行了测试),但出现错误。这是代
我目前正在 Eclipse 上使用 Java 7、Maven、Spring MVC 和 Eclipselink JPA 编写 Web 服务,以访问连接到内部网络的温度/湿度传感器的值。我使用 curl
HttpsURLConnection 有问题 - 未使用代理。 这是代码: //proxy String type = "https"; System.getProperties().put(type
我目前正在 Eclipse 上使用 Java 7、Maven、Spring MVC 和 Eclipselink JPA 编写一个 Web 服务,以访问连接到内部网络的温度/湿度传感器的值。我使用cur
在我的应用程序中,我需要下载一些网页。我是这样做的 URL url = new URL(myUrl); HttpURLConnection conn = (HttpURLConnection) url
我正在尝试使用 URLConnection 获得最低级别的字节计数.我已经用 CountingInputStream 计算了两个流传递的数据和 CountingOutputStream来自 Apach
我正在使用这段代码创建一个常规的 HTTP 连接: URLConnection cn = new URL( "http://...." ).openConnection(); cn.connect()
我是一名优秀的程序员,十分优秀!