JAVA 模拟登陆华理教务处

来源:互联网 发布:solr默认端口号 编辑:程序博客网 时间:2024/05/16 18:52

概述

以前写过一个Python模拟登陆教务处的
现在来拿JAVA重写下
具体思路看那篇文章就好
不过这里还用了jsoup,那篇文章是正则表达式写
不过可以看出python比JAVA短太多了

Code

import org.apache.http.HttpEntity;import org.apache.http.NameValuePair;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.message.BasicNameValuePair;import org.jsoup.Jsoup;import org.jsoup.nodes.Document;import org.jsoup.nodes.Element;import org.jsoup.select.Elements;import java.util.ArrayList;import java.util.List;import java.util.Scanner;class Jwc {  final String LOGIN_URL = "http://202.120.108.14/ecustedu/K_StudentQuery/K_StudentQueryLogin.aspx";  final String LEFT_URL = "http://202.120.108.14/ecustedu/K_StudentQuery/K_StudentQueryLeft.aspx";  final String CLASS_TABLE_URL = "http://202.120.108.14/ecustedu/E_SelectCourse/ScInFormation/syllabus.aspx";  String username;  String password;  CloseableHttpClient client;  Jwc(String a, String b) {    username = a;    password = b;  }  String getContent(String url) throws Exception{    HttpGet httpGet = new HttpGet(url);    CloseableHttpResponse response = client.execute(httpGet);    HttpEntity entity = response.getEntity();    Scanner scanner = new Scanner(entity.getContent());    String s = new String();    while (scanner.hasNextLine()) {      s += (scanner.nextLine() + '\n');    }    return s;  }  boolean init() throws Exception{    client = HttpClients.createDefault();    String s = getContent(LOGIN_URL);    Document doc = Jsoup.parse(s);    Elements es = doc.getElementsByTag("input");    List<NameValuePair> nvps = new ArrayList<NameValuePair>();    for (Element e : es) {      String name = e.attr("name");      String value = e.attr("value");      if (name.equals("TxtStudentId")) value = username;      if (name.equals("TxtPassword")) value = password;      nvps.add(new BasicNameValuePair(name, value));    }    HttpPost post = new HttpPost(LOGIN_URL);    post.setEntity(new UrlEncodedFormEntity(nvps));    CloseableHttpResponse response = client.execute(post);    String ss = getContent(LEFT_URL);    if (ss.contains("您好")) return true; else return false;  }  String getClassTable() throws Exception{    String s = getContent(CLASS_TABLE_URL);    Document doc = Jsoup.parse(s);    Elements es = doc.getElementsByTag("input");    List<NameValuePair> nvps = new ArrayList<NameValuePair>();    for (Element e : es) {      String name = e.attr("name");      String value = e.attr("value");      nvps.add(new BasicNameValuePair(name, value));    }    HttpPost post = new HttpPost(CLASS_TABLE_URL);    post.setEntity(new UrlEncodedFormEntity(nvps));    CloseableHttpResponse response = client.execute(post);    Scanner scanner = new Scanner(response.getEntity().getContent());    String ss = new String();    while (scanner.hasNextLine()) {      ss += scanner.nextLine();    }    String res = new String();    doc = Jsoup.parse(ss);    es = doc.select("tr");    for (Element e : es) {      res += (e.text() + '\n');    }    return res;  }}public class Main {  public static void main(String[] args) throws Exception {    Jwc jwc = new Jwc("", "");    if (jwc.init()) {      System.out.println(jwc.getClassTable());    }  }}
0 0
原创粉丝点击