简体   繁体   English

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

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

I tried to find the answer for my question. 我试图找到问题的答案。

But, I couldn't login the below website in my android app. 但是,我无法在我的android应用中登录以下网站。

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

What's wrong with it? 它出什么问题了? Does anybody help me to find the wrong code snippet? 有人可以帮助我找到错误的代码段吗?

below is my querying codes. 下面是我的查询代码。

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

I spent about 3 hours to find out the workaround........ :-( 我花了大约3个小时来找出解决方法........ :-(

Don't send the user and password at the first request. 不要在第一次请求时发送用户名和密码。 The first request is intended to get you the cookies (and sometimes another fields that you need to log on). 第一个请求旨在为您获取Cookie(有时还需要登录其他字段)。 Make the first request as follows: 发出第一个请求,如下所示:

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

Now you can send the POST request. 现在,您可以发送POST请求。 Besides the cookies that you have used, it has many more parameters, so it should look like - 除了您使用的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();

You may also add headers like "accept encoding" to the second request as well. 您也可以在第二个请求中添加诸如“接受编码”的标头。 Most sites don't really mind. 大多数网站并不介意。
You can see the exect request that your browser sends with the built in developer tools. 您可以看到浏览器使用内置的开发人员工具发送的exect请求。
The most important, as I see it, is to change the user agent to "fool" the site and make it think that you are not browsing from the mobile - it may look different on mobile device and request other login info. 正如我所看到的,最重要的是将user agent更改为“愚弄”该站点,并使其认为您不是从移动设备浏览-它在移动设备上看起来可能有所不同,并请求其他登录信息。
Of course that I can't log in to your site, so I couldn't check all the above. 当然,我无法登录到您的站点,因此无法检查以上所有内容。 Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM