使用 JSoup post 方法登录网站 [英] Login a website with JSoup post method

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

问题描述

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

I'm trying to login into a website with JSoup post method. I saw some examples but neither are working to me. I'm trying to login to: http://ug.technion.ac.il/Tadpis.html For that i have the following code:

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

显然我遗漏了一些数据(我不知道是哪个).对我来说还不够清楚的另一件事是网址.检查上述 url 的 html 时,我可以看到这一行:

Apparently I'm missing some data (I don't know which). Another thing that isn't clear enought to me is the url. When examinig the html of the above url i can see this line:

 <form action="http://techmvs.technion.ac.il:80/cics/wmn/wmngrad?aapmlkwi&ORD=1&s=1" method="POST" name="SignonForm"

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

which is a different url from the one stated above. Which one of these do i suppose to use as the url parameter to "connect" method?

谢谢!

推荐答案

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

The url that you see in the address bar is not the one that you want to make the request to. You should make the request to the second url that you see in the form.

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

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

P.S. I believe that http://techmvs.technion.ac.il:80/cics/wmn/wmngrad is enough. You don't need the extra GET parameters, but check it for yourself.

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

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