JAVA 模拟登陆华理教务处

#概述
以前写过一个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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值