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

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

问题描述

我正在尝试使用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();

显然我丢失了一些数据(我不知道哪个).网址对我来说还不够清楚.当检查上述网址的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();

P.S.我相信 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发布方法登录网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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