gpt4 book ai didi

java - Content-Length 分隔消息正文的过早结束(预期为 :

转载 作者:搜寻专家 更新时间:2023-10-30 19:53:34 25 4
gpt4 key购买 nike

我正在尝试借助 apache httpclient 获取 HTTP 响应。我成功获取了 header ,但是当我尝试获取内容时它抛出异常。异常(exception)是:

 org.apache.http.ConnectionClosedException: Premature end of Content-Length delimited message body (expected: 203856; received: 1070
at org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:180)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:154)
at java.io.BufferedReader.readLine(BufferedReader.java:317)
at java.io.BufferedReader.readLine(BufferedReader.java:382)

我的代码是:

InputStream is = entity.getContent();
BufferedReader br = new BufferedReader( new InputStreamReader(is, "UTF-8"));
String line;
String str = "";
while ((line = br.readLine()) != null) {

str = str + line + "\n";

}
log.debug(str);

任何帮助将不胜感激。谢谢

最佳答案

我可能会晚点回复它。但是我也遇到了同样的问题。我得到了解决方案。在我的例子中,我在使用 HttpEntity 之前关闭了客户端。关闭客户端后,我试图下载文件。下面的代码与我所做的类似:

HttpEntity httpEntity = null;
try (final CloseableHttpClient client = createHttpClient()) {
httpEntity = getEntity(client);
}

return downloadFile(httpEntity, targetDirectory, fileName);

在关闭客户端之前调整我的代码以下载文件后,它现在对我有用。下面的代码与我现在所做的类似:

try (final CloseableHttpClient client = createHttpClient()) {
HttpEntity httpEntity = getEntity(client);
return downloadFile(httpEntity, targetDirectory, fileName);
}

关于java - Content-Length 分隔消息正文的过早结束(预期为 :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23152816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com