Java JSoup引发IO异常:尝试加载URL时发生了太多重定向
我正在尝试为minecraft.net制作一个用户名迁移器作为一个mod,这样人们就可以将他们的帐户迁移到ingame,以防止帐户被破解。为了做到这一点,我需要张贴一个表格到网站上。我成功地获取了cookie,因此authenticityToken将保持不变,但每当我尝试将数据发布回站点时,它就会抛出“java.io.IOException:尝试加载URL时发生了太多重定向” 我真的不知道为什么会发生这种情况,但这可能与网站有关。authentityToken绝对匹配。我在没有发布到站点并提供相同的cookies时检查了这一点。这是我目前使用的代码Java JSoup引发IO异常:尝试加载URL时发生了太多重定向,java,http,exception,post,jsoup,Java,Http,Exception,Post,Jsoup,我正在尝试为minecraft.net制作一个用户名迁移器作为一个mod,这样人们就可以将他们的帐户迁移到ingame,以防止帐户被破解。为了做到这一点,我需要张贴一个表格到网站上。我成功地获取了cookie,因此authenticityToken将保持不变,但每当我尝试将数据发布回站点时,它就会抛出“java.io.IOException:尝试加载URL时发生了太多重定向” 我真的不知道为什么会发生这种情况,但这可能与网站有关。authentityToken绝对匹配。我在没有发布到站点并提供相
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();
}
试试看{
响应=Jsoup.connect(“https://account.mojang.com/migrate“”。execute();//下载站点以获取cookie
字符串auth=response.body();
字符串auth2=auth.split(“名称=\”authenticityToken\“值=\”)[1];
auth=auth2.split(“\”>”)[0];
映射cookies=response.cookies();
连接=Jsoup.connect(“https://account.mojang.com/migrate)数据(“操作”,“迁移/检查”)
.data(“authenticityToken”,auth)
.data(“mcusername”、“username”)
.数据(“密码”、“密码”)
.method(method.POST)
。跟随重定向(true);
对于(条目cookie:cookies.entrySet()){
cookie(cookie.getKey(),cookie.getValue());
}
connection.execute();//此处引发异常
Document=connection.get();
字符串docHtml=document.html();
系统输出打印LN(DOCTML);
}捕获(例外e){
e、 printStackTrace();
}
如果有任何帮助,我们将不胜感激。
HttpConnection.response
haveMAX\u重定向
常数等于20。
您的执行超出了此限制,因此引发了IOException
似乎连接卡在了无止境的重定向循环中。尝试更改过帐数据的方法
编辑
是的,重定向后的每个新页面都有302状态代码。因此,问题可能出在您的请求上。
HttpConnection.response
haveMAX\u重定向等于20的常量。
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());
您的执行超出了此限制,因此引发了IOException
似乎连接卡在了无止境的重定向循环中。尝试更改过帐数据的方法
编辑
是的,重定向后的每个新页面都有302状态代码。所以问题可能出在你的要求上
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());
你的错误:
表单操作不是查询字符串参数。因此,.data(“action”、“/migrate/check”)
是不正确的。表单操作是POST URL的一部分,如上面我的代码所示
documentdocument=connection.get()
正在URL上发出GET请求。这是不正确的connection.execute()
已经发表了一篇文章。只需阅读响应,final response postResponse=connection.execute()
不需要解析隐藏的输入,authenticityToken就是这样。正如我已经演示的那样,Jsoup可以为您做到这一点
你的错误:
表单操作不是查询字符串参数。因此,.data(“action”、“/migrate/check”)
是不正确的。表单操作是POST URL的一部分,如上面我的代码所示
documentdocument=connection.get()
正在URL上发出GET请求。这是不正确的connection.execute()
已经发表了一篇文章。只需阅读响应,final response postResponse=connection.execute()
不需要解析隐藏的输入,authenticityToken就是这样。正如我已经演示的那样,Jsoup可以为您做到这一点
奇怪的是,我没有遇到任何重定向循环。他的请求不正确,但对我来说只是404。奇怪的是,我没有遇到任何重定向循环。他的请求不正确,但对我来说只是404。谢谢你的帮助,但还有一个问题。问题是webbrowser然后链接到哪个为我返回404错误?不确定webbrowser是如何到达那里的,这是一个可以选择在Ankyou上迁移帐户以获得帮助的页面,但还有一个问题。问题是webbrowser然后链接到哪个为我返回404错误?不确定webbrowser是如何到达那里的,这是可以选择迁移帐户的页面