为什么 HTTP 标头是“内容长度”?不总是设置?
我正在通过 Websphere App Server (7FP19) 从 IBM HTTP 服务器请求文件。对于大多数文件,我得到了内容长度标头,但对于某些文件,却没有。我发现当我将请求中的最后修改值设置为“0”时,我会获得所有文件的内容长度。
这对我来说似乎有点奇怪。有谁知道为什么会这样或者这只是巧合?
以下是一些代码:
connection = (HttpURLConnection) url.openConnection();
for (String value : cookies.values()) {
connection.addRequestProperty("Cookie", value); //$NON-NLS-1$
}
connection.setDoOutput(true);
connection.setRequestProperty("User-Agent", USER_AGENT); //$NON-NLS-1$
//connection.setIfModifiedSince(localLastModified);
connection.setIfModifiedSince(0);
OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
wr.write(post);
wr.flush();
wr.close();
....
// set file attributes
long remoteDate = connection.getLastModified();
if(rc == 304)
data.lastModified = localLastModified;
else
data.lastModified = remoteDate;
data.retCode = connection.getResponseCode();
data.contentType = connection.getContentType();
data.contentEncoding = connection.getContentEncoding();
int expectedLength = connection.getContentLength();
if(expectedLength < 0) {
log.warn("Expected length: " + expectedLength);
}
更新,
该代码在 Wesphere FP19 上运行。我回到FP15,问题就消失了。始终返回长度。
I am requesting files from an IBM HTTP server via a Websphere App Server (7FP19). For most files I get the content-length header but for some, not. I discovered that when I set the last-modified value in the request to '0' then I get the content-length for all files.
This seems a bit wierd to me. Does anyone know why this might be or is it just a coincidence?
Here is some code:
connection = (HttpURLConnection) url.openConnection();
for (String value : cookies.values()) {
connection.addRequestProperty("Cookie", value); //$NON-NLS-1$
}
connection.setDoOutput(true);
connection.setRequestProperty("User-Agent", USER_AGENT); //$NON-NLS-1$
//connection.setIfModifiedSince(localLastModified);
connection.setIfModifiedSince(0);
OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
wr.write(post);
wr.flush();
wr.close();
....
// set file attributes
long remoteDate = connection.getLastModified();
if(rc == 304)
data.lastModified = localLastModified;
else
data.lastModified = remoteDate;
data.retCode = connection.getResponseCode();
data.contentType = connection.getContentType();
data.contentEncoding = connection.getContentEncoding();
int expectedLength = connection.getContentLength();
if(expectedLength < 0) {
log.warn("Expected length: " + expectedLength);
}
UPDATE
this was running on Wesphere FP19. I returned to FP15 and the problem was gone. The length is always returned.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否只是收到没有正文和 CL 标头的 HTTP_NOT_MODIFIED/304?这似乎按预期工作。
Are you just getting HTTP_NOT_MODIFIED/304 back which has no body and no C-L header? This seems to be working as expected.