getResponseBodyAsStream returns "Invalid byte 1 of 1-byte UTF-8 sequence"
My code is
PostMethod method = new PostMethod(TRANSLATION_SERVICE);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(method.getResponseBodyAsStream());
It produces
Invalid byte 1 of 1-byte UTF-8 sequence
When I use method.开发者_StackOverflow社区getResponseBodyAsString()
, I get the desired response, but the API clearly states that
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.
Is there an alternative to the above approach?
Content you are reading claims to use UTF-8 encoding, but does not (probably uses LATIN-1 or windows default encoding): that is, XML document is question is broken; or service you calling is returning incorrect encoding definition. Either way, service is returning you invalid information.
精彩评论