簡體   English   中英

如何使用Android上的Jsoup庫登錄網站

[英]How to login the website by using the Jsoup library on Android

我試圖找到問題的答案。

但是,我無法在我的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個小時來找出解決方法........ :-(

不要在第一次請求時發送用戶名和密碼。 第一個請求旨在為您獲取Cookie(有時還需要登錄其他字段)。 發出第一個請求,如下所示:

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請求。 除了您使用的Cookie之外,它還有更多參數,因此看起來應該像-

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更改為“愚弄”該站點,並使其認為您不是從移動設備瀏覽-它在移動設備上看起來可能有所不同,並請求其他登錄信息。
當然,我無法登錄到您的站點,因此無法檢查以上所有內容。 希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM