0
点赞
收藏
分享

微信扫一扫

Jsoup出错

酷子腿长一米八 2022-04-25 阅读 35
java

使用Jsoup过程中遇到的问题和改正

第一次使用Jsoup时,可以学习Jsoup的官方文档https://www.open-open.com/jsoup/,写出如下代码

// 初次使用时,如下
String response = Jsoup.connect(url).method(Connection.Method.POST)
.header("Content-Type","application/json")
.requestBody(JSON.toJSONString(requestBody))
.post().body().text();

报错,org.jsoup.UnsupportedMimeTypeException: Unhandled content type. Must be text/*, application/xml, or application/xhtml+xml. 通过百度后,查到应该忽略contentType的检查,于是添加如下代码:ignoreContentType(true)。

但是不能不写后面的header(“contentType”,“application/json”),还是要设定contentType,不设定的话,会导致接口使用时数据格式错误,ignoreContentType(true)只是不进行检查。于是最终的结果是:

// 初次使用时,如下
String response = Jsoup.connect(url).ignoreContentType(true)
.method(Connection.Method.POST).header("Content-Type","application/json")
.requestBody(JSON.toJSONString(requestBody))
.post().body().text();
举报

相关推荐

0 条评论