Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a session using jsoup and how to post the data using jsoup

Tags:

jsoup

I can't create a session using jsoup and how to post the data using jsoup. Please help me, I'm new to jsoup api, actually my code is:

Connection.Response res = Jsoup.connect("https://wiki.my---------------")
    .userAgent("Mozila")
    .timeout(0)
    .method(Method.GET)
    .execute();

Document docu = res.parse();
Map<String, String> cookies = afterLogin.cookies();
Document doc2 = (Document) Jsoup.connect("https://wiki.my------------------")
    .data("os_username", "A57", "os_password", "pass")
    .data("login", "Log on")
    .cookies(cookies)
    .timeout(0)
    .post();

I get one webpage (doc2)and then add to some table to that webpage (doc2)?

How to add new data already existing web page doc2 and then how to post doc2 to anther url. Already tried a lot, please help me.

like image 815
sank Avatar asked Dec 30 '13 13:12

sank


1 Answers

For using jsoup to login to any website and retrieving and Parse the data from this website, you just need to follow the bellow instructions:

First point you need to know what is the name of cookies ( Session Name) that you will use it to login to all pages of site.

NOTE: you can know the Session Name of any website as bellow: Login to your website that you want to know the Session Name or cookies for it Then write in URL field this command

javascript:void(alert(document.cookie))

copy the session name then do this

Response res = Jsoup.connect("login Site URL")
            .method(Method.GET)
            .timeout(10000)
            .execute();

   String sessionID = res.cookie("SESSION ID for site");//here put the Session Name for website 

now you have the Session Name of website but still you need to fill it with your login info

String username="your username";
String password="your pass";

Jsoup.connect("login Site URL")
            .data("login:username", username, "login:password", password, "login:loginImg", "", "login", "login")
            .cookie("SESSIONID", sessionID)
            .method(Method.POST)
            .timeout(10000)
            .execute();// now you have filled the Session Name with your login info and you can use it for any page in website


Document doc = Jsoup.connect("any page of site")
            .cookie("SESSIONID", sessionID)
            .timeout(10000)
            .get();// her to open any page with Session 

Name that you already have it

now you just need to go to the tag or element that you need to get your data from it

System.out.println(doc.body().text());// for print all text in page

or if you have element ID you can get data like this

String S = doc.select("span[id^=here put element id]");
System.out.println(S);

or like this

String S=doc.getElementById("ElementID").text();
System.out.println(S);
like image 158
user3214337 Avatar answered Oct 09 '22 10:10

user3214337