尝试在 Jsoup 中加载 URL 时发生太多重定向

Too many redirects occurred trying to load URL in Jsoup

我正在尝试抓取一些 mediafire 链接并面临重定向过多的问题我已经在互联网上彻底搜索过但我面临的问题是某些 URL 没有得到 "Too many redirects"错误,而其中一些错误你能帮我看看我做错了什么吗?

try{
        String url = "http://www.mediafire.com/file/110n342iorl685e/Guns-n%27-Roses_Paradise-City-TS_v1_2_DD_p.psarc";
        Document doc3 = Jsoup.connect(url).userAgent("Mozilla/5.0").timeout(0).get();
            for (Element sub3childrow : doc3.select("div.download_link")) {
                String link=sub3childrow.select("a").attr("href");
                System.out.println(link);
            }
    }catch(Exception ex){
            ex.printStackTrace();
        }

这是堆栈跟踪:

java.io.IOException: Too many redirects occurred trying to load URL http://www.mediafire.com/file/110n342iorl685e/Guns-n%2527-Roses_Paradise-City-TS_v1_2_DD_p.psarc
at org.jsoup.helper.HttpConnection$Response.<init>(HttpConnection.java:623)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:656)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:676)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:676)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:676)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:676)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:676)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:676)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:676)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:676)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:676)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:676)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:676)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:676)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:676)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:676)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:676)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:676)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:676)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:676)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:676)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:676)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:628)
at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:260)
at org.jsoup.helper.HttpConnection.get(HttpConnection.java:249)
at custom_file_downloader.RedirectExample.main(RedirectExample.java:23)

我已经尝试设置 followRedirects(true),反之亦然,但无论如何都无济于事。你能在这方面指导我吗?

org.jsoup jar 的 1.10.2 版本存在问题。引用错误修复提交消息。

  • Bugfix: In Jsoup.Connection, if a redirect contained a query string with %xx escapes, they would be double escaped before the redirect was followed, leading to fetching an incorrect location.

阅读此 https://github.com/jhy/jsoup/issues/826 了解更多信息。后来在 1.10.3 版本中解决了上述问题。所以只好更新版本再试。

    <dependency>
        <groupId>org.jsoup</groupId>
        <artifactId>jsoup</artifactId>
        <version>1.10.3</version>
    </dependency>

您将获得具有上述依赖关系的输出。