Java學習-063-Jsoup 請求頁面 Content-Type 解析失敗:org.jsoup.UnsupportedMimeTypeException: Unhandled content type. Must be text/*, application/xml, or application/xhtml+xml . Mimetype=application/javascript
阿新 • • 發佈:2020-11-29
使用 Jsoup 獲取網站查詢資料時,一直提示 Content-Type 失敗,詳細錯誤資訊如下所示:
1 org.jsoup.UnsupportedMimeTypeException: Unhandled content type. Must be text/*, application/xml, or application/xhtml+xml 2 . Mimetype=application/javascript; charset=utf-8, URL=url 3 at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:770)4 at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:705) 5 at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:295) 6 at com.example.helper.VerifyData.test(VerifyData.java:14) 7 at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)8 at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 9 at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 10 at java.base/java.lang.reflect.Method.invoke(Method.java:567) 11 at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)12 at org.testng.internal.Invoker.invokeMethod(Invoker.java:583) 13 at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719) 14 at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989) 15 at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125) 16 at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109) 17 at org.testng.TestRunner.privateRun(TestRunner.java:648) 18 at org.testng.TestRunner.run(TestRunner.java:505) 19 at org.testng.SuiteRunner.runTest(SuiteRunner.java:455) 20 at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450) 21 at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415) 22 at org.testng.SuiteRunner.run(SuiteRunner.java:364) 23 at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) 24 at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84) 25 at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208) 26 at org.testng.TestNG.runSuitesLocally(TestNG.java:1137) 27 at org.testng.TestNG.runSuites(TestNG.java:1049) 28 at org.testng.TestNG.run(TestNG.java:1017) 29 at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:73) 30 at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)
檢視 Content-Type 設定如下所示:
Jsoup.connect(url).header("Content-Type", "application/javascript; charset=utf-8")
更換設定資訊,錯誤依然存在;重查 Jsoup 的文件時,發現有個忽略 ContentType 的設定,更改後如下所示:
Jsoup.connect(url).ignoreContentType(true)
再次,請求地址後,錯誤資訊消失,即可成功獲取相應的資料資訊。