Cannot login to website using jsoup

落爺英雄遲暮 提交于 2020-01-11 07:43:12

问题


I am trying to login to this website.
Here is what I have tried so far, but it doesn't seem to work:

try{
       Connection.Response login = Jsoup.connect("login_url").method(Connection.Method.GET).execute(
       Connection.Response doc = Jsoup.connect("https://ecampus.psgtech.ac.in/studzone/")
              .data("Txtstudid","id")
              .data("TxtPasswd","password")
              .data("btnlogin","Login")
              .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0")
              .method(Connection.Method.POST).execute(); //doesn't seem to work.
       Document docs=Jsoup.connect("https://ecampus.psgtech.ac.in/studzone/AttWfPercView.aspx/") //after login
                                .cookies(doc.cookies())
                                .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0").get();
       test=docs.title();
   }catch (IOException e){
       test=e.getMessage();
   }
   out.setText(docs.title());

What am I doing wrong?

Edit 1 Finally with help of TDG i found the flaw.

For users seeking solution for this, the actual issue is you are not passing enough data to login.

STEP 1: Go to Chrome load the page then, Options>Tools>Developer Tools

STEP 2: In the console type $("input") This will return all the inputs required in the form STEP 3: Append the data to your Response like this.

Jsoup.connect("login_url")
              .data("username","user")
              .data("password","password")
              .data("btnlogin","Login")
              .data("id_of_the_data_required","value")
              .data("...and so on like this...)
              .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0")
              .method(Connection.Method.POST).execute(); 

STEP 4: Crawl the webpage's html code and find whether you have to use POST or GET method and its recommend to use userAgent.

STEP 5: Once you find the login event successful. Use the Response_name. cookies() to go the other urls in the page.

Hope this helps.

来源:https://stackoverflow.com/questions/46205455/cannot-login-to-website-using-jsoup

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!