Android/java Simulation Login Square Educational system

Source: Internet
Author: User

recently idle to have no matter, intends to start to write a blog, also is a summary of their own knowledge. This article will explain how to use httpclient simulation to login to the square educational system.

Need to use ethical jar package: Httpclient,jsoup. (Download jar package)

This simulation login to the University of Chengdu's educational system, other schools of the educational system, can refer to the process and code given in this article to modify and test.

Basic Flow:

1). Use Google Chrome to open the homepage of the educational system, and open the browser developer tool to record the browsing process, and then log in and browse your schedule, results and other information.

2). Download the jar package and reference the jar to the project you need to create a new tool class.

3). Create the HttpClient instance, then create the Httprequestbase instance, and then call Httpclient.execute () to get the HttpResponse instance. Get verification code pictures and other data.

Test code:

public class loginutil {//  Educational System Homepage private string mainurl =  "HTTP// 202.115.80.153/";//  Verification Code Get page private string checkcodeurl = " http://202.115.80.153/ Checkcode.aspx ";//  Verification code Picture save path private string checkcodedes = " c:\\users\\linyang\\ Desktop ";//  landing page private string loginurl = " http://202.115.80.153/default2.aspx ";//   Access to the Academic system homepage to obtain the cookieprivate string cookie =  "";//  Student number private string  stuno =  "201210411122";//  educational system password, for the protection of privacy, now the password hidden private string password =  "* * * "The student name of the;//  educational system private string realname = " ";//  after successful login, redirect the page private  string contenturl =  "Http://202.115.80.153/xs_main.aspx?xh="  + stuNo;//  Get the course page private string courseurl =  "http://202.115.80.153/xskbcx.aspx?xh="  +  stuno;//  Course Number Private string courseno =  "gnmkdm=n121603";//  score number private string soureno =  "";//  HttpClient Object Private httpclient httpclient = null;public void scanmainurl ()   Throws exception {httpclient = httpclients.createdefault ();                          //according to the browser records, is the Get method to use HttpGet, is post is with Httpposthttpget getmainurl = new httpget (MAINURL ), or//by calling HttpClient's Execute (httprequestbase) method to obtain the HttpResponse instance httpresponse response =  Httpclient.execute (Getmainurl);//Get Cookiecookie = response.getfirstheader ("Set-Cookie"). GetValue (); /output The value of the cookie to the console System.out.println (cookie);//parse the HTML page into a string for easy access to the hidden parameters in the form and the information needed for the elements string temphtml  = parsetostring (response);//construction requires a collection of query elements list<queryentity> keywords = new  Arraylist<queryentity> ();//Add QueryElement information, here is a new definition of an instance class Keywords.add (New queryentity ("Input[name=__viewstate",  "Val",  null));         //Get query Information collection list<string> values =  Getvaluesbykeywords (temphtml, keywords);         // Get Captcha picture Getmainurl = new httpget (Checkcodeurl); Response = httpclient.execute ( Getmainurl)         //The Verification code request return stream resolved to the picture saved to the desktop, Developers can also request API direct programming to get the CAPTCHA string Parseistoimage (Response.getentity (). GetContent ()), or the login method to log on to login (values,  httpclient, response);} Public void login (list<string> values, httpclient httpclient, httpresponse  response)  throws exception {system.out.println ("Please enter the Verification Code:");//Scan the input received the verification code Scanner scanner  = new scanner (system.in); String checkcode = scanner.nextline ();//Create an HttpPost instance for the impersonation login operation httppost httppost =  new&nbsP HttpPost (loginurl);//Set HttpPost header information Httppost.addheader ("Cookie",  cookie); Httppost.addheader ("Accept",   "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"); Httppost.addheader (" Content-type ", " application/x-www-form-urlencoded ") Httppost.addheader (" Referer ",  mainurl); List<namevaluepair> requestentity = new arraylist<namevaluepair> (); Requestentity.add (New basicnamevaluepair ("__viewstate",  values.get (0)); Requestentity.add (new  Basicnamevaluepair ("txtUserName",  stuno)); Requestentity.add (New basicnamevaluepair ("TextBox2",  Password) requestentity.add (New basicnamevaluepair ("Txtsecretcode",  checkcode)); Requestentity.add ( New basicnamevaluepair ("RadioButtonList1",  "%d1%a7%c9%fa"); Requestentity.add (new  Basicnamevaluepair ("Button1",  ")); Requestentity.add (New basicnamevaluepair (" Lblanguage ", " ")); Requestentity.add (New basicnamevaluepair ("Hidpdrs", ")); Requestentity.add (New basicnamevaluepair ("HIDSC",  ")");// Set the HttpPost request Body httppost.setentity (new urlencodedformentity (requestentity,  "gb2312")); response =  httpclient.execute (HttpPost); judgeloginsuccess (response);} /*  determine if login successful  */private void judgeloginsuccess (httpresponse response)  throws  exception {// todo auto-generated method stub//to determine whether the page is redirected or not redirected, you need to check if the parameter is missing and the password is incorrect!if   (Response.getstatusline () Getstatuscode ()  == 302)  {system.out.println ("Login Successful!! "); Httpget getcontent = new httpget (contentURL); Getcontent.setheader ("Referer", &NBSP;MAINURL) ; Getcontent.setheader ("Cookie",  cookie); Response = httpclient.execute (getcontent); String temphtml = parsetostring (response); System.out.println (temphtml); List<queryentity> keywords = new arraylist<queryentity> (); KeyWords.add (new  queryentity ("Span#xhxm",  "text",  null));//Get Student name Realname = getvaluesbykeywords (temphtml, keywords). Get (0); GetCourse ();} &NBSP;ELSE&NBSP;{SYSTEM.OUT.PRINTLN ("Login failed!! ");}} /*  Get Course page  */public void getcourse ()  throws Exception {String  courseurl1 = courseurl +  "&xm="  + realName +  "&"  +  Courseno; Httpget getcourse = new httpget (COURSEURL1); Getcourse.setheader ("Referer",  "http:// 202.115.80.153/xs_main.aspx?xh=201210411122 "); Getcourse.setheader (" Cookie ",  cookie); Httpresponse response = httpclient.execute (GetCourse); String temp = parsetostring (response); System.out.println ("\ n Course page:"  + temp);} Public static void main (String[] args)  throws exception {new loginutil (). Scanmainurl ();} Parse InputStream into picture public void parseistoimage (Inputstream is)  throws exception { FileoutPutstream fos = new fileoutputstream (New file (checkcodedes,  "CheckCode.gif")); byte[] tempdata = new byte[1024];int len = 0;while  ((len =  Is.read (TempData))  != -1)  {fos.write (Tempdata, 0, len);} Fos.close (); Is.close ();} Parse HttpResponse into stringpublic string parsetostring (httpresponse response)  throws  Exception {inputstream is = response.getentity (). getcontent (); Bufferedreader reader = new bufferedreader (New inputstreamreader (IS)); string line = null; Stringbuilder builder = new stringbuilder ();while  (Line = reader.readline ( )  != null)  {builder.append (line +  "\ n");} Reader.close (); Is.close (); return builder.tostring ();} The incoming query collection, which takes the values of the query element, encapsulates it using Java reflection, simplifies the operation Public list<string> getvaluesbykeywords (string html,  list<qUeryentity> queryentities)  throws exception {list<string> values =  new ArrayList<String> (); Element body = jsoup.parse (HTML). Select ("Body"). Get (0);for  (queryentity entity :  queryentities)  {element element = body.select (entity.targetselector). Get (0); method method = null; string value = null; Class<?> clazz = element.getclass ();if  (entity.methodparms == null)  { Method = clazz.getmethod (Entity.methodname);value =  (String)  method.invoke (element,  new object[] {});}  else {method = clazz.getmethod (entity.methodname, new class[] {  string.class });value =  (String)  method.invoke (element, new object[] {  entity.methodparms });} The value of the output selector and the corresponding selector to the console System.out.println (entity.targetselector +  "\ t" + value"); Values.add (value);} Return values;}} A query body entity class that defines query HTML elements to simplify query operations class queryentity {string targetselector; string methodname; string methodparms;/** *  @param  targetSelector  selector  *  @param  methodname   Method name for Get value  *  @param  methodParms  method callback parameter  */public queryentity (string  Targetselector, string methodname, string methodparms) {this.targetselector =  Targetselector;this.methodname = methodname;this.methodparms = methodparms;}}


This article is from the "shallow Dream thin Cool" blog, please be sure to keep this source http://lovexiaoyang.blog.51cto.com/10006124/1885933

Android/java Simulation Login Square Educational system

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.