Jsoup problem --- failed to get http request org. jsoup. UnsupportedMimeTypeException: Unhandled content type. Must be text/*, application/xml, or application/xhtml + xml., jsoupxhtml
Jsoup problem --- failed to get http Request
1. Problem: When Jsoup is used to obtain data from some websites, it was obtained smoothly at first. However, when a wave of data is accessed, Jsoup reports an error. It should be the request type (ContextType) in the request header) does not meet the requirements.
Error message:
Exception in thread "main" 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:547)at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:493)at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:205)at org.jsoup.helper.HttpConnection.get(HttpConnection.java:194)at com.Interface.test.JsoupUtil.httpGet(JsoupUtil.java:30)at com.Interface.test.test.main(test.java:23)
Request Method:
Public static String httpGet (String url, String cookie) throws IOException {// GET request Connection con = Jsoup. connect (url); // Request Header settings, especially cookie settings con. header ("Accept", "text/html, application/xhtml + xml, */*"); con. header ("Content-Type", "application/x-www-form-urlencoded"); con. header ("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)"); con. header ("Cookie", cookie); // parse the request result Document doc = con. get (); // get the title System. out. println (doc. title (); return doc. toString ();}
2. Solution: Add ignoreContentType (true) to Connection con = Jsoup. connect (url);. ignoreContentType (true) indicates that ContextType check is ignored.
After adding
// Obtain the request Connection con = Jsoup. connect (url ).IgnoreContentType (true );