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

查看:102
本文介绍了如何使用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();

此处它还在页面源中再次提供正常的登录页面。

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源,那么你可以在那里看到隐藏的一个参数是 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天全站免登陆