带有 Content-Type: text 的 IIS 响应抛出“未处理的内容类型“null”” Jsoup 中的异常
我正在将一些数据发布到 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
Is there a指定响应必须被视为 text/plain
的方法?这是一个 jsoup 错误吗?
谢谢,
尼古拉斯
I'm POSTing some data to an IIS server which replies with an simple id (here 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
The text
content type seems to confuse Jsoup which throws an:
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
Is there a way to specify that the response must be trated as text/plain
? Is this a jsoup bug?
Thanks,
Nicolas
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
jsoup 默认检查响应类型,以防止您意外尝试将图像和 PDF 等解析为 HTML。由于它无法将
text
识别为有效的 HTML 内容类型,因此会引发异常。您可以强制 jsoup 忽略内容类型并将响应解析为 HTML,使用
Connection.ignoreContentType()
方法。例如
jsoup checks the response type by default, to protect you from accidently trying to parse images and PDFs etc as HTML. Because it doesn't recognise
text
as a valid HTML content-type, it is throwing an exception.You can force jsoup to ignore the content-type and parse the response as HTML with the
Connection.ignoreContentType()
method.E.g.
Jsoup 可以从 String 对象读取数据。为什么不使用 InputStreamReader 将服务器响应读入字符串,然后让 JSoup 解析响应?
Jsoup can read data from String objects. Why not use an InputStreamReader to read the server response into a String, then have JSoup parse the response?