Jsoup報錯:org.jsoup.UnsupportedMimeTypeException: Unhandled content type. jsoup.helper.HttpConnection

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 類型.
有兩個解決辦法.

  1. 在header指定"content-type"字段的類型
  2. 添加ignoreContentType(true) 直接無視content類型
String res = Jsoup.connect(url)
				  .ignoreContentType(true)
				  .get()
				  .toString();

方法二更好用
Jsoup入門可以看我的另一篇文章 使用Jsoup庫編寫Java爬蟲 Java爬蟲庫的快速學習入門編寫

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章