簡體   English   中英

為什么在使用 HttpClient 時分塊流意外結束?

[英]Why am I getting chunked stream ended unexpectedly when I use HttpClient?

該問題似乎只出現在我嘗試發布的“大”文件中。

我的代碼如下所示:

PostMethod method = new PostMethod(url);

File input = new File(filePathname);
RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");

method.setRequestEntity(entity);
method.setRequestHeader("Content-Disposition", "attachment; filename=xyzzy")

HttpClient client = new HttpClient();
Credentials defaultcreds = new UsernamePasswordCredentials("userid", "pw");

client.getState().setCredentials(new AuthScope("hostname", port, AuthScope.ANY_REALM), defaultcreds);

 try {
    int statusCode = client.executeMethod(method);

    if (statusCode != HttpStatus.SC_OK) {
        throw new Exception("Method failed: " + method.getStatusLine());
    }

    // Read the response body.
    byte[] responseBody = method.getResponseBody();
    return new String(responseBody);
 }
 catch (HttpException e) {
    System.err.println("Fatal protocol violation: " + e.getMessage());
    throw e;

 }
 catch (IOException e) {
    System.err.println("Fatal transport error: " + e.getMessage());
    throw e;

 }
 finally {
     // Release the connection.
     method.releaseConnection();
 }

異常文本如下所示:

Fatal transport error: chunked stream ended unexpectedly
Exception in thread "main" java.io.IOException: chunked stream ended unexpectedly
    at org.apache.commons.httpclient.ChunkedInputStream.getChunkSizeFromInputStream(ChunkedInputStream.java:252)
    at org.apache.commons.httpclient.ChunkedInputStream.nextChunk(ChunkedInputStream.java:221)
    at org.apache.commons.httpclient.ChunkedInputStream.read(ChunkedInputStream.java:176)
    at java.io.FilterInputStream.read(FilterInputStream.java:127)
    at org.apache.commons.httpclient.AutoCloseInputStream.read(AutoCloseInputStream.java:108)
    at java.io.FilterInputStream.read(FilterInputStream.java:101)
    at org.apache.commons.httpclient.AutoCloseInputStream.read(AutoCloseInputStream.java:127)
    at org.apache.commons.httpclient.HttpMethodBase.getResponseBody(HttpMethodBase.java:690)

無論我使用 getResponseBody() 還是 getResponseBodyAsStream(),我都會遇到類似的異常。

我不應該獲得太多數據,但我發布了超過 200MB 的數據。

我可以通過更改 PostMethod 的 requestHeader 中指定的文件名值的長度來解決這個問題。 我一直在請求標頭中包含完整文件路徑名的編碼版本。 通過反復試驗,我發現我“發布”的文件的成功或失敗似乎取決於它所在的文件夾。長文件夾文件路徑名不起作用,而較短的文件路徑名不起作用,盡管具有相同的文件。 所以我從請求頭中刪除了路徑名,只開始包含文件名,我不再看到問題。

可能是舊的,可​​以節省一些時間..... 我在 Server 是 Python 而 Clinet 是 Java 時遇到了這個錯誤。

第一 -

來自 Java 客戶端的錯誤“通過 http java.io.IOException 發送數據時出錯:塊末尾預期 CRLF:79/82 java.io.IOException:塊末尾預期 CRLF:79/82”

第二 -

來自 Java Clinet 的錯誤“通過 http java.io.IOException 發送數據時出錯:分塊流意外結束 java.io.IOException:分塊流意外結束”

通過使用分塊流大小更改 ok 響應來解決這兩個錯誤

一個有問題的——

HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nTransfer-Encoding: chunked\r\nServer: Jetty(6.1.26)\r\n\r\nDE\r\n

解決了 -

HTTP/1.1 200 OK\r\nContent-Length: 20000\r\nContent-Type: application/json\r\nTransfer-Encoding: chunked\r\nServer: Jetty(6.1.26)\r\n\r\n229\r\n

注意 = nDE 替換為 n229

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM