连接错误:“org.jsoup.UnsupportedMimeTypeException:未处理的内容类型”

Posted

技术标签:

【中文标题】连接错误:“org.jsoup.UnsupportedMimeTypeException:未处理的内容类型”【英文标题】:Connection error: "org.jsoup.UnsupportedMimeTypeException: Unhandled content type" 【发布时间】:2013-04-25 23:30:43 【问题描述】:

当我尝试打开一个链接以使用 jsoup 进行解析时出现错误。

连接命令:

Document doc = Jsoup.connect("http://www.rfi.ro/podcast/emisiune/174/feed.xml")
                .timeout(10 * 1000).get();

抛出的错误:

Exception in thread "main" org.jsoup.UnsupportedMimeTypeException: Unhandled content type. Must be text/*, application/xml, or application/xhtml+xml. Mimetype=application/rss+xml; charset=utf-8, URL=http://www.rfi.ro/podcast/emisiune/174/feed.xml
    at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:453)
    at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:410)
    at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:164)
    at org.jsoup.helper.HttpConnection.get(HttpConnection.java:153)
    at podcast.Pods.main(Pods.java:41)

【问题讨论】:

【参考方案1】:

使用ignoreContentType() (see doc here):

String myURL = "http://www.rfi.ro/podcast/emisiune/174/feed.xml";
Document pod = Jsoup.connect(myURL).ignoreContentType(true).get();

【讨论】:

优秀,像魅力一样工作【参考方案2】:
// Initialize UnSupportedMimeTypeExeception class 
UnsupportedMimeTypeException mimeType = new UnsupportedMimeTypeException("Hey this is Mime",  "application/json", "http://dictionary.cambridge.org/dictionary/english/reality");
String mime = mimeType.getMimeType();

Document doc = Jsoup.connect("https://translate.google.com/translate_a/single?client=t&sl=en&tl=hi&hl=en&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&ie=UTF-8&oe=UTF-8&source=bh&ssel=0&tsel=0&kc=1&tk=2546.416541&q=consume")
    .requestBody("JSON")
    .header("Content-Type", mime)
    .cookies(response.cookies())
    .ignoreContentType(true)
    .post();
System.out.println(doc);

【讨论】:

就我而言,requestBody 属性不在连接对象中。

以上是关于连接错误:“org.jsoup.UnsupportedMimeTypeException:未处理的内容类型”的主要内容,如果未能解决你的问题,请参考以下文章

如何轻松解决MYSQL数据库连接过多的错误

宽带连接错误代码

Win10宽带连接拨号错误651怎么办?

概要设计说明书

连接失败 错误为651 已拒绝远程链接

连接mysql错误2013