使用JSoup post方法登录网站

Ami*_*mir 8 html post jsoup

我正在尝试使用JSoup post方法登录网站.我看到了一些例子,但都没有对我有用.我正在尝试登录:http://ug.technion.ac.il/Tadpis.html 为此,我有以下代码:

 String url = "http://ug.technion.ac.il/Tadpis.html";
 doc = Jsoup.connect(url).data("userid", "my_user_id")
                .data("password", "my_password").data("function","signon").data("submit", "Signon").post();
Run Code Online (Sandbox Code Playgroud)

显然我缺少一些数据(我不知道哪些).另一件我不明白的事情就是网址.检查上面的网址的html我可以看到这一行:

 <form action="http://techmvs.technion.ac.il:80/cics/wmn/wmngrad?aapmlkwi&ORD=1&s=1" method="POST" name="SignonForm"
Run Code Online (Sandbox Code Playgroud)

这是与上述不同的网址.我想将其中哪一个用作"连接"方法的url参数?

谢谢!

Alk*_*ris 7

您在地址栏中看到的网址不是您要向其发出的网址.您应该将请求发送到您在表单中看到的第二个网址.

//With this you login and a session is created
    Connection.Response res = Jsoup.connect("http://techmvs.technion.ac.il:80/cics/wmn/wmngrad?aapmlkwi&ORD=1&s=1")
        .data("username", "myUsername", "password", "myPassword")
        .method(Method.POST)
        .execute();

//This will get you cookies
Map<String, String> loginCookies = res.cookies();

//Here you parse the page that you want. Put the url that you see when you have logged in
Document doc = Jsoup.connect("urlYouNeedToBeLoggedInToAccess")
      .cookies(loginCookies)
      .get();
Run Code Online (Sandbox Code Playgroud)

PS我相信http://techmvs.technion.ac.il:80/cics/wmn/wmngrad就足够了.您不需要额外的GET参数,但请亲自检查.