org.jsoup.UnsupportedMimeTypeException: Unhandled content type. Must be text/*, application/xml, or application/xhtml+xml. Mimetype=application/json;charset=UTF-8, URL=
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:600)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:540)
at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:227)
at org.jsoup.helper.HttpConnection.get(HttpConnection.java:216)
Unhandled content type. Must be text/*, application/xml, or application/xhtml+xml. Mimetype=application/json;charset=UTF-8,
看到這段之後直接定位錯誤問題,未知 content 類型.
有兩個解決辦法.
- 在header指定"content-type"字段的類型
- 添加ignoreContentType(true) 直接無視content類型
String res = Jsoup.connect(url)
.ignoreContentType(true)
.get()
.toString();
方法二更好用
Jsoup入門可以看我的另一篇文章 使用Jsoup庫編寫Java爬蟲 Java爬蟲庫的快速學習入門編寫