Jsoup Implementing Java Analog Login

Source: Internet
Author: User

Jsoup java Analog Login 1: How to access cookies. 1.1: Because of the need to log in, so the first simulation of the landing of the <form> here, use map to load the variable name, variable value.  map<string, string> Map = new hashmap<string, string> (); Map.put ("username", username);  map.put ("pwd", Md5.getmd5ofstr (password)), Map.put ("Imgcode", "" "), Map.put (" F "," json "), how to assign a value to a map, requires the player to analyze the page himself, Locate the login form in the Web page, and then look at the desired value.  1.2:httpclient package has many, here I use an open source package Jsoup-1.7.1.jar, this one open source package, (actually compare suggest to learn httpclient the use of this package first, because that package is the most basic, All other open source packages should be improved on the basis of this one, jsoup a class connection in the package, this is the key class, and the request is made through it. Code: Connection conn = Jsoup.connect (Login_url); Conn.header ("(Request-line)", "Post/cgi-bin/login?lang=zh_cn http/1.1 Conn.header ("Accept", "Application/json, Text/javascript, */*; q=0.01 "), Conn.header (" accept-encoding "," gzip, deflate "), Conn.header (" Accept-language "," ZH-CN "); Conn.header (" Cache-control "," No-cache "), Conn.header (" Connection "," keep-alive "), Conn.header (" Content-type "," application/ x-www-form-urlencoded; Charset=utf-8 "), Conn.header (" Host "," mp.weixin.qq.com "), Conn.header (" Referer "," https:mp.weixin.qq.com/"); Conn.header (" User-agent "," mozilla/5.0 "(compatible; MSIE 9.0; Windows NT 6.1; trident/5.0; Malc) "); Response Response = Conn.ignorecontenttype (True). Method (Method.post). Data (map). Execute ();   as you can see, First to find the landing when the Login_url, this one to analyze the Web page source, as if there is any crawler technology, but I have not learned, constructs a connection conn object, the object's construction needs to pass a URL, followed by the Conn to pretend to be a browser simulation, That is Conn.header (), the inside specific to what value to carry out, this yo according to the actual situation, sometimes may request not so strict, in short "Host", "Referer", "user-agent" is must, then, let has simulated for the browser's conn, Start the request,  response Response = Conn.ignorecontenttype (True). Method (Method.post). Data (map). Execute ();  can be seen, Conn to call the data () method, import the value of the login form Map,method (method.post) or get method? This is actually to try, the general login is the Post method, not send data requests, simple just dot dot web is generally get method, anyway post is not, try to get it, finally call execute () Start execution, return a value of response type, The value of the cookies inside this response, as well as some of the returned information. (Response returns a Web page, as well as a Web page that does not see cookies).   1.3:cookies can be a data type that inherits the map, which is the form of {variable name = value   Variable name = value},  map<string, string> cookies = Response.Cookies ();  here defines a value for the map type to load the cookies. Do you get the value of the cookie here?  //do not know why, the code also has the following part??????? Further to get COokies? The value of this cookie is discarded, can't it be used????? However, the following is also called the Response.Cookies () method to obtain the value of the cookie. Originally, the following code is used to get the value of token!!!!!  1.4: How do I get the token value? String Json=response.body ();//It is obvious that the value of token is inside the body, where the body of response is extracted and a JSON of string type is included, which contains token, In fact, we can extract the value of the variable which is token, do not understand the value of the JSON can be directly printed out, in fact, not long, the value of extracting the key is just basic enough, but here with a called Gson-2.2.2.jar package, to help us to extract.   1.5: Use Gson to extract the value code of the keyword: weixinresponse weixinresponse=gson.fromjson (JSON, Weixinresponse.class);  Gson.fromjson requires 2 values, one is JSON, one is Class A, and the return value is Class A, and Class A is just a class consisting of simple setter,gettet, what is the value in Class A? This is where you need to print the JSON to determine the variable name. Then we get the class A so we can call Gettet to get tokens, that's all.                   Above is the acquisition of the value of the cookie. After obtaining the value of the cookie, how can I use the value of the cookie to access the page? 2: Preparation conditions: Url,cookie is just so 2, the value of the cookie has been obtained by login and saved, and later to use the value of the cookie can be used directly.  url is the page you want to visit.   2.1: Similarly, the access page uses the Connecion object in the Jsoup package. 2.2:document Document = Jsoup.connect (fans_url). Cookies (cookies). Get ();  Here is also careful, sometimes the Get method will not get the content, then change to post method. laterAlso, get () or post () is not predictable, we can only try.  2.3: Gets the object of a document. I prefer System.out.println (document.outerhtml ());  promised to go through the entire page to see if we were successful.  2.4: How do we get the values we want in a page? I like to analyze the page, and then look at the value of what kind of label, usually in <script> this type of label inside: Elements  cai = Document.getelementsbytag ("script"); Get a lot of elements of the elements, through cai.size () can know that there are several such tags.  2.5: int position=0;for (int b=0;b<cai.size (); b++) {if (Cai.get (b). Data (). Contains ("groupslist") = = true) {position = b;}} Element Dr =document.getelementsbytag ("script"). Get (position);  then I pick a keyword I want to <script>, a unique one, Groupslist, so we can get the <script> that we want.  2.6: The next step is to get the value we want in a <script>. String json = Dr.data (); String [] s = Json.split ("\\s+"); converts <script> to character type, separated by a space delimiter.   2.7: Further parsing script to extract the part of the string containing the ID, get temp2string temp2= ""; for (int i=0;i<s.length;i++) {// System.out.println (S[i]); if (S[i].contains ("id") = = True) {//system.out.println ("in" +i); Temp2 =temp2+s[i];}}  2.8:string fakeidstr = ""; int check2 = 0;for (int jh=0;jh&Lt;temp2.length (); jh++) {if (Temp2.charat (JH) >=48&&temp2.charat (JH) <=57) {if (Check2 = = 0) {Fakeidstr = fakeidstr+ "*";} Fakeidstr = Fakeidstr+temp2.charat (JH); check2 = 1;} else {check2 = 0;}}   Here I want a number, and the number is longer than 4.              Summary: 1: Know how to get cookies2: Know how to use cookies to further access Page 3: Know how to get the value you want in the page

Jsoup Implementing Java Analog Login

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.