简体   繁体   中英

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.

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........ :-(

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). 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. Besides the cookies that you have used, it has many more parameters, so it should look like -

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.
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.
Of course that I can't log in to your site, so I couldn't check all the above. Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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