gpt4 book ai didi

java - getResponseBodyAsStream 返回 "Invalid byte 1 of 1-byte UTF-8 sequence"

转载 作者:行者123 更新时间:2023-12-01 05:48:12 26 4
gpt4 key购买 nike

我的代码是

PostMethod method = new PostMethod(TRANSLATION_SERVICE);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(method.getResponseBodyAsStream());

它产生

Invalid byte 1 of 1-byte UTF-8 sequence

当我使用 method.getResponseBodyAsString() 时,我得到了所需的响应,但是 the API明确指出

Note: This will cause the entire response body to be buffered in memory. A malicious server may easily exhaust all the VM memory. It is strongly recommended, to use getResponseAsStream if the content length of the response is unknown or resonably large.

上述方法有替代方法吗?

最佳答案

您正在阅读的内容声称使用 UTF-8 编码,但实际上并非如此(可能使用 LATIN-1 或 Windows 默认编码):也就是说,XML 文档有问题;或者您调用的服务返回不正确的编码定义。无论哪种方式,服务都会返回给您无效的信息。

关于java - getResponseBodyAsStream 返回 "Invalid byte 1 of 1-byte UTF-8 sequence",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5375639/

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