httpclient3+jsoup擷取正方教務系統課表

來源:互聯網
上載者:User

標籤:

運用httpclient3+jsoup擷取正方教務系統課表

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"; private 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, 80, "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(); //得到cookies    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:學號 pwd:密碼    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 = "學生";                  try {   post.setRequestBody(new NameValuePair[]{     new NameValuePair("__VIEWSTATE",viewstate),     new NameValuePair("txtUserName",xh),     new NameValuePair("textBox2",pwd),     new NameValuePair("txtSecretCode",checkcode),     new NameValuePair("RadioButtonList1",URLEncoder.encode(type, "GBK")),     new NameValuePair("Button1",""),     new NameValuePair("lbLanguage",""),     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, 80, "http");  int status = 0;         String content = "";           //GET xs_main頁面  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 查詢頁面  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);          } private void loadPic(byte[] data) { //get.getResponseBody()擷取驗證碼圖片的位元組資訊,並寫入output輸出資料流中  File file = new File("D:/code.png");//驗證碼圖片存放位置  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) {    //擷取VIEWSTATE欄位  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.catchKbData("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擷取正方教務系統課表

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.