2011-10-06 259 views
1

我正在通過Websphere App Server(7FP19)從IBM HTTP服務器請求文件。對於大多數文件,我得到的內容長度標題,但一些,而不是。我發現,當我將請求中最後修改的值設置爲'0'時,我得到所有文件的內容長度。爲什麼HTTP頭'content-length'不總是被設置?

這對我來說似乎有點奇怪。有誰知道爲什麼這可能是或僅僅是一個巧合?

下面是一些代碼:

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

這是上Wesphere FP19運行。我回到了FP15,問題沒有了。長度總是返回。

回答

0

你剛剛獲得HTTP_NOT_MODIFIED/304,它沒有正文,沒有C-L頭文件?這似乎按預期工作。

+0

不,我得到一個200 – paul

+0

你能包含完整的請求和響應頭文件嗎? – covener

相關問題