2016-03-28 151 views
0

我一直在努力登錄到this頁面,所以我可以刮私未成功登錄到使用Jsoup

learn.sun.ac.za/my

頁的此頁現在好。我已經搜索了多個SO帖子,並試圖應用來自每一個的建議沒有任何效果。

嘗試1

String url = "https://sso-prod.sun.ac.za/cas/login?service=http%3A%2F%2Flearn.sun.ac.za%2Flogin%2Findex.php"; 
String userAgent = "Mozilla/5.0"; 

      Connection.Response response = Jsoup.connect(url) 
        .userAgent(userAgent) 
        .method(Connection.Method.GET) 
        .execute(); 

      response = Jsoup.connect(url) 
        .userAgent(userAgent) 
        .cookies(response.cookies()) 
        .data("action", "login") 
        .data("username", "MYUSERNAME") 
        .data("password", "MYPASSWORD") 
        .method(Connection.Method.POST) 
        //.followRedirects(true) 
        .execute();    

      Document doc = Jsoup.connect("http://learn.sun.ac.za/my") 
        .cookies(response.cookies()) 
        .userAgent(userAgent) 
        .get(); 

      System.out.println(doc.title()); 

輸出: 「在單點登錄|總公司」

表明它沒有登錄。

從其他職位的建議,我監控的流量通過出鉻,添加了所有從那裏頭的代碼

String url = "https://sso-prod.sun.ac.za/cas/login?service=http%3A%2F%2Flearn.sun.ac.za%2Flogin%2Findex.php"; 
String userAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36"; 

      Connection.Response response = Jsoup.connect(url) 
        .userAgent(userAgent) 
        .method(Connection.Method.GET) 
        .execute(); 

      response = Jsoup.connect(url) 
        .userAgent(userAgent) 
        .cookies(response.cookies()) 
        .header("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8") 
        .header("accept-encoding", "gzip, deflate") 
        .header("accept-language", "en-US,en;q=0.8") 
        .header("cache-control", "max-age=0") 
        .header("connection", "keep-alive") 
        .header("content-length", "114") 
        .header("content-type","application/x-www-form-urlencoded") 
        .header("dnt", "1") 
        .header("host", "sso-prod.sun.ac.za") 
        .header("origin", "https://sso-prod.sun.ac.za") 
        .header("referer", "https://sso-prod.sun.ac.za/cas/login?service=http%3A%2F%2Flearn.sun.ac.za%2Flogin%2Findex.php") 
        .header("upgrade-insecure-requests", "1") 
        .data("action", "login") 
        .data("username", "MYUSERNAME") 
        .data("password", "MYPASSWORD") 
        .data("lt", "LT-3042474-9t3oldTU1253G6HVqFffHgMWxnYXdg") 
        .data("execution", "e1s1") 
        .data("_eventId", "submit") 
        .method(Connection.Method.POST) 
        //.followRedirects(true) 
        .execute();    

      Document doc = Jsoup.connect("http://learn.sun.ac.za/my") 
        .cookies(response.cookies()) 
        .userAgent(userAgent) 
        .get(); 

      System.out.println(doc.title()); 

其中有相同的結果。 我之後做了什麼打印出了實際的html代碼,發現代碼中的任何地方都沒有登錄錯誤消息,這意味着我搞砸了某處並沒有真正提交表單?

這是一個成功的鉻登錄看起來像 enter image description here enter image description here

回答

1

那是因爲你的LT值是錯誤的 - 你應該得到的價值爲每一個新的會話,而不是像你一樣送一些價值。看網頁的HTML

LT

的選擇了包含該值是div.controls:nth-child(1)DIV
所以步驟 -
加載頁面
獲取的LT 值將它添加到您的POST請求。

+0

解決了這個問題! – SimpleJack