Jsoup Demo Landing Example

Source: Internet
Author: User

Jsoup Simulation Landing Small example, different sites, need different simulation strategy, scattered fairy here just as a guide to learn.

Java code
    1. Package com.jsouplogin;
    2. Import Java.util.HashMap;
    3. Import java.util.List;
    4. Import Java.util.Map;
    5. Import org.jsoup.Connection;
    6. Import Org.jsoup.Jsoup;
    7. Import Org.jsoup.Connection.Method;
    8. Import Org.jsoup.Connection.Response;
    9. Import org.jsoup.nodes.Document;
    10. Import org.jsoup.nodes.Element;
    11. /**
    12. * Use Jsoup to simulate login Iteye
    13. *
    14. *
    15. * General ideas are as follows:
    16. *
    17. * First request landing page, get page information, contain form information, and cookie (this is very important), can not get, will simulate landing not on
    18. *
    19. *
    20. * Second login, set user name, password, the first cooking, put in, you can
    21. *
    22. * How to determine whether the landing is successful?
    23. *
    24. * After landing, print the page, you will see welcome xxx, you can prove
    25. *
    26. *
    27. * @date June 27, 2014
    28. * @author Qindongliang
    29. *
    30. *
    31. * **/
    32. Public class Jsouploginiteye {
    33. public static void Main (string[] args)throws Exception {
    34. Jsouploginiteye jli=New Jsouploginiteye ();
    35. Jli.login ("xxxxx", "xxxxx"); Enter the Iteye username, and password
    36. }
    37. /** 
    38. * Analog Login Iteye
    39. *
    40. * @param userName User Name
    41. * @param pwd Password
    42. *
    43. * **/
    44. public void Login (String username,string pwd)throws exception{
    45. //First time request
    46. Connection con=jsoup.connect ("Http://www.iteye.com/login");//Get Connection
    47. Con.header ("user-agent", "mozilla/5.0" (Windows NT 6.1; WOW64; rv:29.0) gecko/20100101 firefox/29.0 "); //Configure simulation browser
    48. Response rs= Con.execute (); //Get response
    49. Document D1=jsoup.parse (Rs.body ()); //Convert to DOM tree
    50. List<element> et= d1.select ("#login_form"); Get Form form, you can see the source code of the page to know
    51. //Get, cooking and form properties, the following map holds the data at Post
    52. Map<string, string> datas=new hashmap<> ();
    53. For (Element E:et.get (0). Getallelements ()) {
    54. if (e.attr ("name"). Equals ("name")) {
    55. E.attr ("value", userName); Set User name
    56. }
    57. if (e.attr ("name"). Equals ("password")) {
    58. E.attr ("value", pwd); //Set User password
    59. }
    60. if (e.attr ("name"). Length () >0) {//Exclude Null value form Properties
    61. Datas.put (e.attr ("name"), E.attr ("value"));
    62. }
    63. }
    64. /** 
    65. * Second request, post form data, and cookie information
    66. *
    67. * **/
    68. Connection con2=jsoup.connect ("Http://www.iteye.com/login");
    69. Con2.header ("user-agent", "mozilla/5.0" (Windows NT 6.1; WOW64;  rv:29.0) gecko/20100101 firefox/29.0 ");
    70. //Set cookies and the map data above the post
    71. Response Login=con2.ignorecontenttype (True). Method (Method.post). Data (datas). Cookies (Rs.cookies ()). Execute ()  ;
    72. //print, information after successful login
    73. System.out.println (Login.body ());
    74. //After the successful login cookie information, can be saved to the local, after landing, only one time to login can
    75. Map<string, string> map=login.cookies ();
    76. For (String s:map.keyset ()) {
    77. System.out.println (s+"" +map.get (s));
    78. }
    79. }
    80. }

Jsoup Demo Landing Example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.