Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to POST Data into website using Jsoup

Tags:

java

post

jsoup

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

I am trying the code

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

here it is giving normal page of login in pagesource

i have also tried the code

 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 !!

Thanks....

like image 708
Aspirant Avatar asked Apr 04 '12 13:04

Aspirant


People also ask

What is the use of Jsoup in Java?

What It Is. jsoup can parse HTML files, input streams, URLs, or even strings. It eases data extraction from HTML by offering Document Object Model (DOM) traversal methods and CSS and jQuery-like selectors. jsoup can manipulate the content: the HTML element itself, its attributes, or its text.

What does Jsoup clean do?

clean. Creates a new, clean document, from the original dirty document, containing only elements allowed by the safelist. The original document is not modified. Only elements from the dirty document's body are used.


2 Answers

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

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 :

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.

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. 
like image 113
vikiiii Avatar answered Sep 17 '22 13:09

vikiiii


If the issue is a javascript redirect, you could try going into the javascript and checking if the URL it redirects to is static, and then use the redirection to gain access. I did that to access a popup box made by javascript once.

like image 36
Ethan Avatar answered Sep 21 '22 13:09

Ethan