如何使用Android上的Jsoup库登录网站 [英] How to login the website by using the Jsoup library on Android

查看:196
本文介绍了如何使用Android上的Jsoup库登录网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I tried to find the answer for my question.

不过,我无法登录我的Andr​​oid应用程序下面的网站。

But, I couldn't login the below website in my android app.

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

这有什么错呢?
有谁帮我找错了code段?

What's wrong with it? Does anybody help me to find the wrong code snippet?

下面是我的查询codeS。

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

我花了3个小时左右,找出解决方法........: - (

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

现在,您可以发送POST请求。除此之外,你已经使用了饼干,它有更多的参数,因此它应该看起来像 -

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

您还可以添加如接受编码的第二次请求,以及头。大多数网站不介意。结果
你可以看到,您的浏览器与内置的开发人员工具发送exect请求。结果
最重要的,在我看来,是改变用户代理来傻瓜式的网站,并使其觉得你是不是从移动浏览 - 它可能看起来在移动设备不同,并要求其他的登录信息。结果
当然,我不能登录到你的网站,所以我不能检查所有以上。希望这有助于!

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!

这篇关于如何使用Android上的Jsoup库登录网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆