Jsoup Simulation Landing Small example, different sites, need different simulation strategy, scattered fairy here just as a guide to learn.
Java code
- Package com.jsouplogin;
- Import Java.util.HashMap;
- Import java.util.List;
- Import Java.util.Map;
- Import org.jsoup.Connection;
- Import Org.jsoup.Jsoup;
- Import Org.jsoup.Connection.Method;
- Import Org.jsoup.Connection.Response;
- Import org.jsoup.nodes.Document;
- Import org.jsoup.nodes.Element;
- /**
- * Use Jsoup to simulate login Iteye
- *
- *
- * General ideas are as follows:
- *
- * First request landing page, get page information, contain form information, and cookie (this is very important), can not get, will simulate landing not on
- *
- *
- * Second login, set user name, password, the first cooking, put in, you can
- *
- * How to determine whether the landing is successful?
- *
- * After landing, print the page, you will see welcome xxx, you can prove
- *
- *
- * @date June 27, 2014
- * @author Qindongliang
- *
- *
- * **/
- Public class Jsouploginiteye {
- public static void Main (string[] args)throws Exception {
- Jsouploginiteye jli=New Jsouploginiteye ();
- Jli.login ("xxxxx", "xxxxx"); Enter the Iteye username, and password
- }
- /**
- * Analog Login Iteye
- *
- * @param userName User Name
- * @param pwd Password
- *
- * **/
- public void Login (String username,string pwd)throws exception{
- //First time request
- Connection con=jsoup.connect ("Http://www.iteye.com/login");//Get Connection
- Con.header ("user-agent", "mozilla/5.0" (Windows NT 6.1; WOW64; rv:29.0) gecko/20100101 firefox/29.0 "); //Configure simulation browser
- Response rs= Con.execute (); //Get response
- Document D1=jsoup.parse (Rs.body ()); //Convert to DOM tree
- List<element> et= d1.select ("#login_form"); Get Form form, you can see the source code of the page to know
- //Get, cooking and form properties, the following map holds the data at Post
- Map<string, string> datas=new hashmap<> ();
- For (Element E:et.get (0). Getallelements ()) {
- if (e.attr ("name"). Equals ("name")) {
- E.attr ("value", userName); Set User name
- }
- if (e.attr ("name"). Equals ("password")) {
- E.attr ("value", pwd); //Set User password
- }
- if (e.attr ("name"). Length () >0) {//Exclude Null value form Properties
- Datas.put (e.attr ("name"), E.attr ("value"));
- }
- }
- /**
- * Second request, post form data, and cookie information
- *
- * **/
- Connection con2=jsoup.connect ("Http://www.iteye.com/login");
- Con2.header ("user-agent", "mozilla/5.0" (Windows NT 6.1; WOW64; rv:29.0) gecko/20100101 firefox/29.0 ");
- //Set cookies and the map data above the post
- Response Login=con2.ignorecontenttype (True). Method (Method.post). Data (datas). Cookies (Rs.cookies ()). Execute () ;
- //print, information after successful login
- System.out.println (Login.body ());
- //After the successful login cookie information, can be saved to the local, after landing, only one time to login can
- Map<string, string> map=login.cookies ();
- For (String s:map.keyset ()) {
- System.out.println (s+"" +map.get (s));
- }
- }
- }
Jsoup Demo Landing Example