如何使用 Jsoup 将数据发布到网站 [英] How to POST Data into website using Jsoup

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

问题描述

我正在尝试将数据发布到网站以使用 Jsoup 登录网站,但它不起作用?

I am trying to POST data into website to make a login into the site using Jsoup , but its not working ?

我正在尝试代码

    Document docs = Jsoup.connect("http://some.com/login")
        .data("cmd", "login","username", "xxxx","password", "yyyyy")
        .referrer("http://some.com/login/").post();

这里是在pagesource中给出正常的登录页面

here it is giving normal page of login in pagesource

我也试过代码

 Document docs = (Document) Jsoup.connect("http://some.com/login")
    .data("cmd", "login","username", "xxxx","password", "yyyyy")
    .referrer("http://some.com/login/").method(Method.POST).execute().parse();

这里也是在pagesource中再次给出正常的登录页面.

here also it is giving normal page of login again in pagesource.

任何有关相同的建议将不胜感激!

Any suggestions regarding the same would be highly appreciated !!

谢谢....

推荐答案

我会以一个例子来回答你的问题.假设您想登录 Facebook.

I will give the answer of your question by taking an example. Suppose you want to login to facebook.

然后除了用户名和密码之外,还有许多其他参数也通过POST 请求传递.这些所有参数都是隐藏的,并且像用户名和密码一样传递.例如:

Then apart from username and password there are many other parameters that are also passed through POST request. Those all parameters are hidden and are passed similarly like username and password. For Example :

如果你打开facebook的html source,你会看到有一个隐藏的参数是lgnrnd,它的值为071129_5D7M代码>.

If you will open the html source of facebook , then you can see there is one parameter which is hidden is lgnrnd and its value is 071129_5D7M.

所以还有很多其他的类似这样的参数.你需要传递所有的参数.您还应该指定 userAgent.

So there are many other parameter similar like this.You need to pass all the parameters. You should also specify the userAgent.

Document doc = Jsoup.connect("http://www.facebook.com")
.data("email", "myemailid")
.data("pass", "mypassword")
// and other hidden fields which are being passed in post request.
.userAgent("Mozilla")
.post();
System.out.println(doc); // will print html source of homepage of facebook.

这篇关于如何使用 Jsoup 将数据发布到网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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