Using Httpclient3+jsoup to obtain the curriculum of square educational system
Import Java.io.file;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.io.unsupportedencodingexception;import Java.net.urlencoder;import Java.util.scanner;import Org.apache.commons.httpclient.cookie;import org.apache.commons.httpclient.HttpClient; Import Org.apache.commons.httpclient.httpexception;import Org.apache.commons.httpclient.namevaluepair;import Org.apache.commons.httpclient.methods.getmethod;import Org.apache.commons.httpclient.methods.postmethod;import Org.jsoup.jsoup;import Org.jsoup.nodes.document;public class Login {private static String Host_url = "jwc.sju.js.cn"; pr Ivate static String Login_url = "/default2.aspx"; private static String Code_url = "/checkcode.aspx"; private static String Kb_url = "/xskb.aspx"; Private HttpClient client; Private String viewstate = ""; Private String cookie = ""; Public Login () {init ();} private void Init () {client = new HttpClient (); Client.getparams (). Sethttpelementcharset ("GBK"); Client.gethostconfiguration (). Sethost (Host_url, "http"); GetMethod get = new GetMethod (Login_url); Get.addrequestheader ("Accept", "*/*"); Get.addrequestheader ("User-agent", "mozilla/4.0" (compatible; MSIE 8.0; Windows NT 6.1; WOW64; trident/4.0; SLCC2;. NET CLR 2.0.50727;. NET CLR 3.5.30729;. NET CLR 3.0.30729; Shuame) "); Get.addrequestheader ("accept-encoding", "gzip, deflate"); Get.addrequestheader ("Host", "jwc.sju.js.cn"); Get.addrequestheader ("Connection", "keep-alive"); int status = 0; try {status = Client.executemethod (get); ViewState = Getviewstate (get.getresponsebodyasstring ()); } catch (HttpException e) {e.printstacktrace (); } catch (IOException e) {e.printstacktrace (); } Cookie cookies[] =client.getstate (). GetCookies (); Get the cookie for (int i = 0; i < cookies.length; i++) {if (i = = 0) {cookie = cookies[i].tostring (); } else {cookie + = Cookies[i] + ";"; }} System.out.println (cookie); Get.releaseconnection (); Get = NEW GetMethod (Code_url); try {status = Client.executemethod (get); Loadpic (Get.getresponsebody ()); } catch (HttpException e) {e.printstacktrace (); } catch (IOException e) {e.printstacktrace (); } get.releaseconnection (); } public void Autologin (String xh,string pwd) {//xh: study number pwd: password Postmethod post = new Postmethod (Login_url); Post.setrequestheader ("Accept", "Application/x-ms-application, Image/jpeg, Application/xaml+xml, Image/gif, image/ Pjpeg, APPLICATION/X-MS-XBAP, Application/vnd.ms-excel, Application/vnd.ms-powerpoint, Application/msword, */* "); Post.setrequestheader ("Referer", "http://jwc.sju.js.cn/"); Post.setrequestheader ("Accept-language", "ZH-CN"); Post.setrequestheader ("User-agent", "mozilla/4.0" (compatible; MSIE 8.0; Windows NT 6.1; WOW64; trident/4.0; SLCC2;. NET CLR 2.0.50727;. NET CLR 3.5.30729;. NET CLR 3.0.30729; Shuame) "); Post.setrequestheader ("Content-type", "application/x-www-form-urlencoded"); Post.setrequEstheader ("accept-encoding", "gzip, deflate"); Post.setrequestheader ("Connection", "keep-alive"); Post.setrequestheader ("Cache-control", "No-cache"); String Checkcode = ""; Scanner sc = new Scanner (system.in); Checkcode = Sc.nextline (). Trim (); System.out.println (Checkcode); String type = "Student"; try {post.setrequestbody (new namevaluepair[]{new Namevaluepair ("__viewstate", VIEWSTATE), New Namevaluepair ("TX Tusername ", XH), New Namevaluepair (" TextBox2 ", pwd), New Namevaluepair (" Txtsecretcode ", Checkcode), new Namevalue Pair ("RadioButtonList1", Urlencoder.encode (Type, "GBK")), New Namevaluepair ("Button1", ""), New Namevaluepair ("Lblan Guage "," "), New Namevaluepair (" Hidpdrs "," "), New Namevaluepair (" HIDSC "," ")}); } catch (IllegalArgumentException e) {e.printstacktrace (); } catch (Unsupportedencodingexception e) {e.printstacktrace (); } int Status = 0; String content = ""; try {status = Client.executemethod (POST); Content = Post.getresponsebodyasstring (); Post.releaseconnection (); } catch (HttpException e) {e.printstacktrace (); } catch (IOException e) {e.printstacktrace (); } System.out.println (POST); } public void Catchkbdata (String xh,string xq) {client = new HttpClient (); Client.getparams (). Sethttpelementcharset ("GBK"); Client.gethostconfiguration (). Sethost (Host_url, "http"); int status = 0; String content = ""; GET Xs_main page String xs_mainurl = "/xs_main.aspx?xh=" + xh; GetMethod get = new GetMethod (Xs_mainurl); Get.setrequestheader ("Accept", "Application/x-ms-application, Image/jpeg, Application/xaml+xml, Image/gif, image/ Pjpeg, APPLICATION/X-MS-XBAP, Application/vnd.ms-excel, Application/vnd.ms-powerpoint, Application/msword, */* "); Get.setrequestheader ("Referer", "http://jwc.scnu.edu.cn/"); Get.setrequestheader ("Accept-language", "ZH-CN"); Get.setrequestheader ("User-agent", "mozilla/4.0" (compatible; MSIE 8.0; Windows NT 6.1; WOW64; trident/4.0; SLCC2;. NET CLR 2.0.50727;. NET CLR 3.5.30729;. NET CLR 3.0.30729; Shuame) "); Get.setrequestheader ("accept-encoding", "gzip, deflate"); Get.setrequestheader ("Connection", "keep-alive"); Get.setrequestheader ("Cache-control", "No-cache"); Get.setrequestheader ("Cookie", cookie); try {status = Client.executemethod (get); Content = Get.getresponsebodyasstring (); Get.releaseconnection (); } catch (HttpException E1) {e1.printstacktrace (); } catch (IOException E1) {e1.printstacktrace (); } System.out.println (content); Get query page get = new GetMethod (Kb_url); Get.setrequestheader ("Accept", "Application/x-ms-application, Image/jpeg, Application/xaml+xml, Image/gif, image/ Pjpeg, APPLICATION/X-MS-XBAP, Application/vnd.ms-excel, Application/vnd.ms-powerpoint, Application/msword, */* "); Get.setrequestheader ("Connection", "keep-alive"); Get.setrequEstheader ("Accept-language", "ZH-CN"); Get.setrequestheader ("Host", "jwc.sju.js.cn"); Get.setrequestheader ("accept-encoding", "gzip, deflate"); Get.setrequestheader ("User-agent", "mozilla/4.0" (compatible; MSIE 8.0; Windows NT 6.1; WOW64; trident/4.0; SLCC2;. NET CLR 2.0.50727;. NET CLR 3.5.30729;. NET CLR 3.0.30729; Shuame) "); Get.setrequestheader ("Cookie", cookie); Get.setquerystring (new namevaluepair[] {new Namevaluepair ("XH", XH), New Namevaluepair ("Xhxx", Xh+xq)}); try {client.executemethod (get); Content = Get.getresponsebodyasstring (); Get.releaseconnection (); } catch (HttpException e) {e.printstacktrace (); } catch (IOException e) {e.printstacktrace (); } System.out.println (content); The private void Loadpic (byte[] data) {//get.getresponsebody () gets the byte information of the Authenticode picture and writes the file File = new file in the output stream ("D:/code.png ");//Verification Code picture storage location FileOutputStream output; try {output = new FileOutputStream (fILE); Output.write (data); Output.close (); } catch (FileNotFoundException e) {e.printstacktrace (); } catch (IOException e) {e.printstacktrace (); }} private string Getviewstate (string html) {//Get viewstate field Document doc = jsoup.parse (HTML); String viewstate = ""; ViewState = Doc.select ("input[name=__viewstate]"). Val (); System.out.println ("_viewstate=" + VIEWSTATE); return viewstate; public static void Main (string[] args) {Login login = new login ();//login.autologin ("xxxxxxxx", "xxxxxx");//login.catch Kbdata ("xxxxxxxx", "2014-20151");}}
Poxm.xml
<dependency><groupid>commons-httpclient</groupid><artifactid>commons-httpclient</ Artifactid><version>3.1</version></dependency><dependency><groupid>org.jsoup </groupid><artifactid>jsoup</artifactid><version>1.8.1</version></dependency >
Httpclient3+jsoup get the curriculum of the square educational system