我正在尝试为minecraft.net创建一个用户名migrater作为mod,这样人们就可以迁移他们的帐户,以阻止破解帐户。为了做到这一点,我需要将一个表格发布到网站上。我设法成功获取cookie,以便authenticityToken保持不变,但每当我尝试将数据发回到站点时,它会抛出'java.io.IOException:尝试加载URL https://account.mojang.com/migrate'时发生了太多重定向
我真的不确定为什么会这样,但可能与网站有关。 authenticntityToken绝对匹配。我没有发布到网站并提供相同的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();
}
任何帮助都将受到极大的赞赏
答案 0 :(得分: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."}
你的错误:
表单操作不是查询字符串参数。因此.data("action", "/migrate/check")
不正确。表单操作是POST URL的一部分,如上面的代码所示。
Document document = connection.get();
正在网址上发出GET请求。这是不正确的。 connection.execute()
已经发布了POST。只需阅读回复final Response postResponse = connection.execute()
即可。
没有必要像这样解析隐藏的输入,authenticityToken。正如我所展示的,Jsoup可以为你做到这一点。
答案 1 :(得分:0)
HttpConnection.Responce
的{{1}}常数等于20。
您的执行超出了此限制,因此抛出了IOException。
似乎连接陷入了无限循环的重定向。尝试更改发布数据的方法。
修改强> 是的,重定向后的每个新页面都有302个状态码。所以问题在于你的要求。