【问题标题】:JSoup Throws IO Exception: Too many redirects occurred trying to load URLJSoup 抛出 IO 异常:尝试加载 URL 时发生了太多重定向
【发布时间】:2012-05-01 02:33:42
【问题描述】:

我正在尝试为 minecraft.net 制作一个用户名迁移器作为 mod,这样人们就可以在游戏中迁移他们的帐户以防止帐户被破解。为此,我需要在网站上发布表格。我设法成功获取了 cookie,因此authenticityToken 将保持不变,但是每当我尝试将数据发布回站点时,它都会抛出“java.io.IOException:尝试加载 URL https://account.mojang.com/migrate 时发生了太多重定向”

我真的不确定为什么会发生这种情况,但可能与网站有关。 authentityToken 绝对匹配。我在不发布到网站并提供相同的 cookie 时检查了这一点。这是我目前使用的代码

try {
        Response response = Jsoup.connect("https://account.mojang.com/migrate").execute(); //downloads site to get the cookies
        String auth = response.body();
        String auth2 = auth.split("name=\"authenticityToken\" value=\"")[1];
        auth = auth2.split("\">")[0];
        Map<String, String> cookies = response.cookies();
        Connection connection = Jsoup.connect("https://account.mojang.com/migrate").data("action", "/migrate/check")
                .data("authenticityToken", auth)
                .data("mcusername", "username")
                .data("password", "password")
                .method(Method.POST)
                .followRedirects(true);
        for (Entry<String, String> cookie : cookies.entrySet()) {
            connection.cookie(cookie.getKey(), cookie.getValue());
        }
        connection.execute(); //exception thrown here
        Document document = connection.get();
        String docHtml = document.html();
        System.out.println(docHtml);
    } catch (Exception e) {
        e.printStackTrace();
    }

任何帮助将不胜感激

【问题讨论】:

    标签: java http exception post jsoup


    【解决方案1】:
    final Response response = 
        Jsoup.connect("https://account.mojang.com/migrate").execute();
    // parse the response as a document, so that we can extract stuff
    final Document doc = response.parse();
    // correct way to extract parsed html
    final Element authToken = doc.select("input[name^=authenticityToken]").get(0);
    final Map<String, String> cookies = response.cookies();
    // action isn't part of the querystring. The form POST URL is our target.
    final Connection connection = 
            Jsoup.connect("https://account.mojang.com/migrate/check")
                .data("authenticityToken", authToken.val()) // correct way to extract val
                .data("mcusername", "username")
                .data("password", "password")
                .method(Method.POST)
                .followRedirects(true);
    for (final Entry<String, String> cookie : cookies.entrySet()) {
        connection.cookie(cookie.getKey(), cookie.getValue());
    }
    final Response postResponse = connection.execute(); // consume response
    System.out.println(postResponse.body());
    

    响应 JSON:

    {"error":"Invalid username or password."}
    

    你的错误:

    1. 表单操作不是查询字符串参数。因此.data("action", "/migrate/check") 是不正确的。如我上面的代码所示,表单操作是 POST URL 的一部分。

    2. Document document = connection.get(); 正在 URL 上发出 GET 请求。这是不正确的。 connection.execute() 已经发了一个帖子。只需阅读回复,final Response postResponse = connection.execute()

    3. 不需要像这样解析隐藏的输入,authentityToken。正如我所展示的,Jsoup 可以为您做到这一点。

    【讨论】:

    • 感谢您的帮助,但还有一个问题。那个问题是网络浏览器然后链接到account.mojang.com/migrate/chooseEmail,它为我返回一个 404 错误?不确定网络浏览器是如何到达那里的,那是选择迁移帐户的页面
    【解决方案2】:

    HttpConnection.Responce 具有等于 20 的 MAX_REDIRECTS 常量。 你的执行超过了这个限制,所以抛出了 IOException。

    似乎连接陷入了无限循环的重定向。尝试改变发布数据的方式。

    编辑 是的,重定向后的每个新页面都有 302 状态码。所以问题可能出在您的请求上。

    【讨论】:

    • 奇怪,我没有遇到任何重定向循环。他的请求不正确,但对我来说只是 404。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-16
    • 1970-01-01
    • 2017-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多