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爬虫库的快速学习入门编写

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