为什么 HTTP 标头是“内容长度”?不总是设置?

发布于 2024-12-08 08:56:31 字数 1322 浏览 1 评论 0原文

我正在通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不离久伴 2024-12-15 08:56:31

您是否只是收到没有正文和 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文