How to connect to the site and retrieve data using java jsoup?

匿名 (未验证) 提交于 2019-12-03 01:20:02

问题:

I know there's a lot of info out there but I couldn't find anything fitting to my problem. I want to gather data from a page I need to be logged in. Here is what I'm trying to do:

I gather Cookies doing:

Connection.Response res = Jsoup       .connect("http://website.com/login?event=doLogin")       .execute(); Map <String,String> cookies = res.cookies();

And then read the html for the hidden values:

 Document doc = Jsoup       .connect("http://website.com/login?event=doLogin")       .cookies(cookies)       .get();           html = doc.toString();         length = html.length();         counter = 0;          for (int i = 0; i < length; i++) {             if (html.startsWith("document.write", i)){                 name[counter] = html.substring(i + 41, i + 144);                 value[counter] = "Login";                 counter++;             }             if (html.startsWith("hidden", i)) {                 name[counter] = html.substring(i + 13, i + 81);                 value[counter] = html.substring(i + 90, i + 123);                 counter++;             }         }

Finally I want to use this information to login using Cookies and hidden values:

 Document doc2 = Jsoup       .connect("http://website.com/login?event=doLogin")       .cookies(cookies)       .data("email", "my@email")       .data("pass", "mypass")       .data(name[0], value[0])       .data(name[1], value[1])       .data(name[2], value[2])       .method(Connection.Method.POST)       .get();  System.out.println(doc2);

But all I got is the login page. I'm afraid those hidden values can be changed when I try:

Document doc2 = Jsoup.connect

Am I doing it right?

回答1:

It's kind of mixed context when you set method to POST and then call GET request. Try this:

Connection.Response res = Jsoup.connect("http://website.com/login?event=doLogin")                                .execute();  ...  Document doc = Jsoup.connect("http://website.com/login?event=doLogin")                     .cookies(res.cookies())                     .data("email", "my@email")                     .data("pass", "mypass")                     .data(name[0], value[0])                     .data(name[1], value[1])                     .data(name[2], value[2])                     .post();


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!