我试图找到我的问题的答案。
但是,我无法在我的 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/