2015-06-03 70 views
0

我试图找到我的问题的答案。如何使用Android上的Jsoup库登录网站

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

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

这有什么错呢? 有没有人帮我找到错误的代码片段?

下面是我的查询码。

  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个小时左右,找出解决办法........ :-(

回答

1

不要在第一次发送请求的用户名和密码。第一个请求的目的让你(你需要登录,有时另一场)的饼干制作的第一个请求如下:。

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请求,除此之外你已经使用了饼干,它有更多的参数,所以它应该看起来像 -

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请求。
正如我所看到的,最重要的是将user agent更改为“愚弄”该网站,并使其认为您未从移动设备上浏览 - 它可能在移动设备上看起来不同并请求其他登录信息。
当然,我无法登录到您的网站,所以我无法检查以上所有内容。希望这可以帮助!

+0

哦〜!非常感谢....你让我节省时间。^^ – allsoft