java - Android上如何使用Jsoup库登录网站

标签 java android authentication jsoup

我试图找到我的问题的答案。

但是,我无法在我的 Android 应用程序中登录以下网站。

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

这是怎么回事? 有人帮我找到错误的代码 fragment 吗?

下面是我的查询代码。

            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();

您还可以将“接受编码”之类的 header 添加到第二个请求中。大多数网站并不介意。
您可以通过内置的开发者工具查看浏览器发送的 EXCE 请求。
在我看来,最重要的是更改用户代理以“愚弄”网站并使其认为您不是从移动设备上浏览 - 它在移动设备和请求上可能看起来不同其他登录信息。
当然,我无法登录您的网站,因此无法检查上述所有内容。希望这有帮助!

关于java - Android上如何使用Jsoup库登录网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30621847/

相关文章:

java - ActionBarActivity 返回 api10f 的 ListFragment 时出现问题

java - Android 线程/处理程序错误 IllegalStateException : The specified message queue synchronization barrier token has not been posted

authentication - X.509 : Server not able to extract user information from client certificate

php - 'remember me' 功能的最佳实践?

MongoDB 身份验证数据库用途

java - 更改 MySQL 时区 Windows

java - 具有子类泛型的子类

c# - 如何用 c sharp 解密 "encrypted string with java"?

android - QuickBlox Android SDK 1.2 不再在 14 之前的 Android API 上运行

Java Collection、Set、Map 或 Array 来保存唯一排序的非空白字符串