2012-04-30 54 views
1

我想爲minecraft.net製作一個用戶名migrater作爲mod,這樣人們將能夠遷移他們的帳戶遊戲以阻止帳戶的破解。爲了做到這一點,我需要將表單發佈到網站上。我設法成功獲取cookie,以便authenticityToken保持不變,但每當我嘗試將數據發回網站時,都會拋出'java.io.IOException:嘗試加載URL時發生太多重定向https://account.mojang.com/migrate'JSoup引發IO異常:嘗試加載URL時出現太多重定向

我真的不知道爲什麼會發生這種情況,但可能與網站有關。 authenticityToken絕對匹配。我沒有發佈到網站並提供相同的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

HttpConnection.ResponceMAX_REDIRECTS常數等於20. 你執行超出此限制,因此IOException異常被拋出。

似乎連接陷入無止境的重定向循環。嘗試更改發佈數據的方法。

編輯 也是,重定向後的每個新頁面都有302個狀態碼。所以問題可能在於你的要求。

+0

奇怪的是,我沒有遇到任何重定向循環。他的要求是不正確的,但是對我來說這只是一個404。 –

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()已經發布了POST。剛剛閱讀回覆,final Response postResponse = connection.execute()

  3. 沒有必要像解析隱藏的輸入,authenticityToken。我已經證明,Jsoup可以爲你做到這一點。

+0

謝謝你的幫助,但還有一個問題。那個問題是,webbrowser然後鏈接到https://account.mojang.com/migrate/chooseEmail,它會爲我返回一個404錯誤?不確定webbrowser如何到達那裏,那是可以選擇遷移帳戶的頁面 –

相關問題