2015-06-03 69 views
0

我試圖找到我的問題的答案。如何使用Android上的Jsoup庫登錄網站

但是,我無法登錄我的Android應用程序下面的網站。

http://www.ddanzi.com/index.php?act=dispMemberLoginForm

這有什麼錯呢? 有沒有人幫我找到錯誤的代碼片段?

下面是我的查詢碼。

  Connection.Response res = Jsoup.connect("http://www.ddanzi.com/index.php?mid=free&act=dispMemberLoginForm") 
       .followRedirects(true) 
       .data("user_id", "myid") 
       .data("password", "mypassword") 
       .method(Connection.Method.POST) 
       .execute(); 

     Document document = Jsoup.connect("http://www.ddanzi.com/index.php?act=dispMemberInfo") 
       .followRedirects(true) 
       .cookies(res.cookies()) 
       .method(Connection.Method.POST) 
       .post(); 

我花了3個小時左右,找出解決辦法........ :-(

回答

1

不要在第一次發送請求的用戶名和密碼。第一個請求的目的讓你(你需要登錄,有時另一場)的餅乾製作的第一個請求如下:。

Connection.Response res = Jsoup.connect("http://www.ddanzi.com/index.php?mid=free&act=dispMemberLoginForm") 
      .followRedirects(true) 
      .userAgent("Mozilla/5.0") //I use FF, and I want that the app will get exectly the same page as I do 
      //you may add more headers, as "Accept Encoding" - check it 
      .method(Connection.Method.GET) 
      .execute(); 

現在,您可以發送POST請求,除此之外你已經使用了餅乾,它有更多的參數,所以它應該看起來像 -

Document document = Jsoup.connect("http://www.ddanzi.com/index.php?act=dispMemberInfo") 
      .followRedirects(true) 
      .cookies(res.cookies()) 
      .method(Connection.Method.POST) 
      .data("error_return_url", "/index.php?mid=free&act=dispMemberLoginForm") 
      .data("mid", "free") 
      .data("vid", "") 
      .data("ruleset", "@login") 
      .data("success_return_url", "") 
      .data("act", "procMemberLogin") 
      .data("user_id" ,"user") 
      .data("password", "password") 
      .referrer("http://www.ddanzi.com/index.php?mid=free&act=dispMemberLoginForm") 
      .post(); 

您也可以在第二個請求中添加「接受編碼」等標題。大多數網站並不介意。
您可以看到您的瀏覽器使用內置的開發人員工具發送的exect請求。
正如我所看到的,最重要的是將user agent更改爲「愚弄」該網站,並使其認爲您未從移動設備上瀏覽 - 它可能在移動設備上看起來不同並請求其他登錄信息。
當然,我無法登錄到您的網站,所以我無法檢查以上所有內容。希望這可以幫助!

+0

哦〜!非常感謝....你讓我節省時間。^^ – allsoft