我正在向 IIS 服务器发送一些数据,该服务器使用一个简单的 ID 进行回复(此处为 1692945
):
Cache-Control: private
Content-Type: text; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNet-Version: 2.0.50727
X-Powered-By: ASP.NET
Date: Tue, 23 Aug 2011 17:08:37 GMT
Content-Length: 7
1692945
text
内容类型似乎混淆了 Jsoup,它抛出:
Exception in thread "main" java.io.IOException: Unhandled content type "null" on URL http://domain.com/svr_listing.aspx. Must be text/*, application/xml, or application/xhtml+xml
有没有办法指定响应必须被视为text/plain
?这是 jsoup 错误吗?
谢谢,
尼古拉斯
最佳答案
jsoup 默认检查响应类型,以防止您不小心将图像和 PDF 等解析为 HTML。因为它不能将 text
识别为有效的 HTML 内容类型,所以它会抛出异常。
您可以强制 jsoup 忽略内容类型并将响应解析为带有 Connection.ignoreContentType()
的 HTML方法。
例如
Document doc = Jsoup.connect(url).ignoreContentType(true).get(); // or .post();
关于java - 内容类型为 : text throws an "Unhandled content type "null""exception in Jsoup 的 IIS 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7172000/