使用 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)
再次,请求地址后,错误信息消失,即可成功获取相应的数据信息。