JAVA使用HttpClient模拟登录正方教务系统,爬取学籍信息和课程表成绩等,超详细登录分析和代码注解

 

目录

前言

分析

代码实现

第一次GET

POST登录

第二次Get

第三次GET

第四次GET

第五次GET

测试

完整代码


 

前言

最近在做一个APP,需要获取我们学校——武汉纺织大学皇家停水断电断网高校的教务系统信息进行排课,无赖学校屏蔽了正方教务系统的默认登录页面,无法使用现有的接口,使用火狐抓包,发现学校自定义的登录页面用户名和密码都是明文传输,喜出望外使用HttpClient模拟登录,但是一直显示400错误,参考了CSDN和博客园的很多资料还是无解,最后只能推到,从头开始,终于发现了该死的5次GET才能获取真实的Cookies的奇葩设定(点名批评纺大教务系统,用户名密码明文存储居然登录搞那么复杂)

废话不多说,开始一步步超详细的分析爬取教务系统吧


分析

JAVA使用HttpClient模拟登录正方教务系统,爬取学籍信息和课程表成绩等,超详细登录分析和代码注解_第1张图片

打开学校教务系统登录页面,先请求生成一个Cookies,用于POST使用

JAVA使用HttpClient模拟登录正方教务系统,爬取学籍信息和课程表成绩等,超详细登录分析和代码注解_第2张图片

手动登录,可以看到,有5个302状态的请求,其中POST为发送登录数据,可以看到username和password分别是用户名,密码,execution和rmshown分别是第几次访问的标记,都不用管,但是有一个lt,经过查找发现lt是登录页面的内嵌字段,可以直接从登录页面获取,单独分析各个请求;

JAVA使用HttpClient模拟登录正方教务系统,爬取学籍信息和课程表成绩等,超详细登录分析和代码注解_第3张图片

第一次POST用登录页面的Cookies获取iPlanetDirectoryPro,以及重定向的

Location

http://jwglxt.wtu.edu.cn/sso/j…dPM5vmJu1586512656377-w6Ia-cas

JAVA使用HttpClient模拟登录正方教务系统,爬取学籍信息和课程表成绩等,超详细登录分析和代码注解_第4张图片

第二个302是GET,可以看到用iPlanetDirectoryPro可以来到JSESSIONID以及下一个GET的URI

Location

http://jwglxt.wtu.edu.cn/sso/j…1882D5B308262624B9BF5D5181B1D0

需要注意的是这个URI和Cookies都不是最终的,JSESSIONID只是中间Cookies用来验证其他GET

JAVA使用HttpClient模拟登录正方教务系统,爬取学籍信息和课程表成绩等,超详细登录分析和代码注解_第5张图片

可以看到在一个GET用刚刚拿到的Cookies,获取一个重定向页面

Location

http://jwglxt.wtu.edu.cn/ticke…2928F9906338285585624192011C17

很明显,这是下一个GET的地址

JAVA使用HttpClient模拟登录正方教务系统,爬取学籍信息和课程表成绩等,超详细登录分析和代码注解_第6张图片

GET上一步拿到的URI,终于拿到了最终的Cookies,但同时还有一个Location,你也许想这样可以跳过去直接到主页,但是不行,还是需要get它,这样才能获取动态的登录后页面,因为之后课表等信息都需要主页的来源地址,不允许直接跳转,否则会报400错误;

Location

/xtgl/login_slogin.html

注意,这是一个相对路径,需要加上前缀http://jwglxt.wtu.edu.cn

JAVA使用HttpClient模拟登录正方教务系统,爬取学籍信息和课程表成绩等,超详细登录分析和代码注解_第7张图片

使用最终iPlanetDirectoryPro和JSESSIONID继续GET上次获得的路径,终于拿到了最终页面地址

(同样也是相对地址)

JAVA使用HttpClient模拟登录正方教务系统,爬取学籍信息和课程表成绩等,超详细登录分析和代码注解_第8张图片

使用最终的URI和Cookies请求,终于拿到了登陆后的页面

JAVA使用HttpClient模拟登录正方教务系统,爬取学籍信息和课程表成绩等,超详细登录分析和代码注解_第9张图片

打开课表测试一下,拿到了课表,说明成功

JAVA使用HttpClient模拟登录正方教务系统,爬取学籍信息和课程表成绩等,超详细登录分析和代码注解_第10张图片

查看请求发现是POST,用到了最终的URI和Cookies

JAVA使用HttpClient模拟登录正方教务系统,爬取学籍信息和课程表成绩等,超详细登录分析和代码注解_第11张图片

以上登录的原理都分析完毕,开始代码实际操作

 

代码实现

第一次GET

使用GET解析登录页面,获取初始的Cookies和LT

GetMethod getMethod = new GetMethod(url);
        //发送get
        httpClient.executeMethod(getMethod);
        //获取get的内容
        InputStream inputStream = getMethod.getResponseBodyAsStream();
        StringBuilder output = new StringBuilder();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
        BufferedReader reader = new BufferedReader(inputStreamReader);
        String line = reader.readLine();
        while (line != null) {
            output.append(line);
            line = reader.readLine();
        }
        //解析为文档树
        Document document = Jsoup.parseBodyFragment(output.toString());
        Element body = document.body();
        //找到lt
        lt = body.select("[name=lt]").attr("value");
        System.out.println("lt: " + lt);
        //释放链接
        getMethod.releaseConnection();
        //状态码
        int statusCodeGet1 = getMethod.getStatusCode();
        System.out.println("Status code get1 :" + statusCodeGet1);
        //得到Cookies
        JSESSION = getMethod.getResponseHeader("Set-Cookie").getValue().trim().split(";")[0].split(",")[1].trim().split("=")[1];
        System.out.println("Cookies GET1 Jsession:" + JSESSION);
        ROUTE = getMethod.getResponseHeader("Set-Cookie").getValue().trim().split(";")[0].split(",")[0].trim().split("=")[1];
        System.out.println("Cookies GET1 Route:" + ROUTE);

POST登录

传输登录信息并且获取1/2的Cookies以及一个URI

String geturi2 = null;
        String loURI = "https://auth.wtu.edu.cn/authserver/login;jsessionid=";
        loURI += JSESSION;
        loURI += "?service=http%3A%2F%2Fjwglxt.wtu.edu.cn%2Fsso%2Fjziotlogin";
 PostMethod postMethod = new PostMethod(murl);
            //请求头
            postMethod.setRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
            postMethod.setRequestHeader("Accept-Encoding", "gzip, deflate, br");
            postMethod.setRequestHeader("Accept-Language", "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2");
            postMethod.setRequestHeader("Connection", "keep-alive");
            postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            postMethod.setRequestHeader("Cookie", "route=" + ROUTE + "; JSESSIONID_auth=" + JSESSION);
            postMethod.setRequestHeader("Host", "auth.wtu.edu.cn");
            postMethod.setRequestHeader("Origin", "https://auth.wtu.edu.cn");
            postMethod.setRequestHeader("Referer", url);
            postMethod.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0");
            //参数
            postMethod.addParameter(new NameValuePair("username", usernam));
            postMethod.addParameter(new NameValuePair("password", password));
            postMethod.addParameter(new NameValuePair("lt", lt));
            postMethod.addParameter(new NameValuePair("dllt", "userNamePasswordLogin"));
            postMethod.addParameter(new NameValuePair("execution", "e1s1"));
            postMethod.addParameter(new NameValuePair("_eventId", "submit"));
            postMethod.addParameter(new NameValuePair("rmShown", "1"));
            mClient.executeMethod(postMethod);
 //状态码
        int statusCodePost = postMethod.getStatusCode();
        System.out.println("Status code post :" + statusCodePost);
        //拿到第二步Get网址
        geturi2 = postMethod.getResponseHeader("Location").toString().split(" ")[1];
        System.out.println("Get Uri 2:" + geturi2);
        //拿到iPlanetDirectoryPro
        Header[] headers = postMethod.getResponseHeaders("Set-Cookie");
        iPlanetDirectoryPro = headers[2].getValue().split(";")[0];
        System.out.println("iPlanetDirectoryPro: " + iPlanetDirectoryPro);

第二次Get

获取中间Cookies和一小步的URI

String geturi3 = null;
        String JSESSIONID2 = null;
        //第二次GET请求进入登录界面,拿到第二个JSESSIONID_iPlanetDirectoryPro
        GetMethod getMethod2 = new GetMethod(geturi2);
        getMethod2.addRequestHeader(new Header("Cookie", iPlanetDirectoryPro));
        getMethod2.setFollowRedirects(false);//不自动处理重定向
        int statusCodeGet2 = httpClient.executeMethod(getMethod2);//状态码
        System.out.println("Status code get2 :" + statusCodeGet2);
        //得到重定向网址
        geturi3 = getMethod2.getResponseHeader("Location").toString();
        geturi3 = geturi3.replaceAll(" ", "");//去掉空格
        geturi3 = geturi3.split(":", 2)[1];//得到网址
        System.out.println("Get Uri 3 :" + geturi3);
        //得到set-Cookie 拿到JSESSIONID2
        Header[] headers2 = getMethod2.getResponseHeaders("Set-Cookie");
        JSESSIONID2 = headers2[0].getValue().split(";")[0];
        System.out.println("JSESSIONID2: " + JSESSIONID2);

第三次GET

获取最终cookies的URI地址

 String geturi4 = null;
        GetMethod getMethod3 = new GetMethod(geturi3);
        getMethod3.setFollowRedirects(false);
        getMethod3.addRequestHeader(new Header("Cookie", JSESSIONID2));
        int statusCodeGet3 = httpClient.executeMethod(getMethod3);
        System.out.println("Status code get3:" + statusCodeGet3);
        //获取下一步URI
        geturi4 = getMethod3.getResponseHeader("Location").toString();
        geturi4 = geturi4.replaceAll(" ", "");//去掉空格
        geturi4 = geturi4.split(":", 2)[1];//得到网址
        System.out.println("Get Uri 4:" + geturi4);

第四次GET

获取最终Cookies和下一步URI

String geturi5 = "http://jwglxt.wtu.edu.cn";
        GetMethod getMethod4 = new GetMethod(geturi4);
        getMethod4.setFollowRedirects(false);
        getMethod4.addRequestHeader(new Header("Cookie", iPlanetDirectoryPro));
        //状态和执行
        int statusCodeGet4 = httpClient.executeMethod(getMethod4);
        System.out.println("Status code get4:" + statusCodeGet4);
        //获取下一步URI
        String get4xd;//设置临时变量,相对URI
        get4xd = getMethod4.getResponseHeader("Location").toString();
        get4xd = get4xd.replaceAll(" ", "");//去掉空格
        get4xd = get4xd.split(":", 2)[1];//得到网址
        //拼接真实URI
        geturi5 += get4xd;
        System.out.println("Get Uri 5:" + geturi5);
        //得到set-Cookie 拿到真实
        Header[] headers3 = getMethod2.getResponseHeaders("Set-Cookie");
        JSESSION = headers3[0].getValue().split(";")[0];
        System.out.println("JSESSIONID: " + JSESSION);

第五次GET

获取登录后的页面

  GetMethod getMethod5 = new GetMethod(geturi5);
        getMethod5.setFollowRedirects(false);
        //设置Cookie
        getMethod5.addRequestHeader(new Header("Cookie", iPlanetDirectoryPro + ";" + JSESSION));
        //状态和执行
        int statusCodeGet5 = httpClient.executeMethod(getMethod5);
        System.out.println("Status code get5:" + statusCodeGet5);
        //获取下一步URI
        String get5xd;//设置临时变量,相对URI
        get5xd = getMethod5.getResponseHeader("Location").toString();
        get5xd = get5xd.replaceAll(" ", "");//去掉空格
        get5xd = get5xd.split(":", 2)[1];//得到网址
        //拼接真实URI
        loginuri += get5xd;
        System.out.println("Loginuri :" + loginuri);

 

测试

之前已经获取了所有的Cookies和最终的URI,现在就可以抓取教务系统,畅通无阻了,那么先测试一下登陆后的主页

GetMethod getMethodLog = new GetMethod(loginuri);
        getMethodLog.addRequestHeader(new Header("Cookie", iPlanetDirectoryPro + ";" + JSESSION));
        //状态和执行
        int statusCodelog = httpClient.executeMethod(getMethodLog);
        System.out.println("Status code log:" + statusCodelog);
        String result = getMethodLog.getResponseBodyAsString();
        System.out.println("最终结果:\n"+result);

测试爬取课表信息,由之前分析可以知道,这是一个POST

 String kcb = "http://jwglxt.wtu.edu.cn/kbcx/xskbcx_cxXsKb.html?gnmkdm=N253508";
PostMethod postMethod = new PostMethod(tourl);
            //请求头
            postMethod.setRequestHeader("Accept", "*/*");
            postMethod.setRequestHeader("Accept-Encoding", "gzip, deflate, br");
            postMethod.setRequestHeader("Accept-Language", "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2");
            postMethod.setRequestHeader("Connection", "keep-alive");
            postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            postMethod.setRequestHeader("Cookie", Cookies);
            postMethod.setRequestHeader("Host", "auth.wtu.edu.cn");
            postMethod.setRequestHeader("Origin", "https://auth.wtu.edu.cn");
            postMethod.setRequestHeader("Referer", "http://jwglxt.wtu.edu.cn/kbcx/xskbcx_cxXskbcxIndex.html?gnmkdm=N253508&layout=default&su=" + usernam);
            postMethod.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0");
            postMethod.setRequestHeader("X-Requested-With", "XMLHttpRequest");
            //表单数据
            postMethod.addParameter("xnm", "2019");
            postMethod.addParameter("xqm", "12");
            postMethod.setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");

            mClient.executeMethod(postMethod);
 int kcbcode = postMethodkcb.getStatusCode();
        System.out.println("Status code kcb :" + kcbcode);
        String ss=new String(postMethodkcb.getResponseBody(), Charset.forName("ASCII"));
        System.out.println(ss);

        //读取json
        InputStream in = postMethodkcb.getResponseBodyAsStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(in,"ISO-8859-1"));
        String tempbf;
        StringBuffer html = new StringBuffer(8*1024);
        while ((tempbf = br.readLine()) != null) {
            html.append(tempbf +"\n");
        }
        System.out.println(html);

 

我们来看看结果:——

JAVA使用HttpClient模拟登录正方教务系统,爬取学籍信息和课程表成绩等,超详细登录分析和代码注解_第12张图片

OK,可以看到数据已经全部出来了,那么获取有用的数据就和之前抓取LT一样直接在页面上查找就行,一切大功告成、

 

完整代码

最后因为POST太长了,看起来不美观,将两个POST封装到来一个内部类中,完整代码如下,可以试一试爬取你们学校的教务系统哦!

注意:需要httpclient、Jsoup两个工具包,可以添加Maven依赖或自下载

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

public class WTU2 {


    private static String usernam = "username";
    private static String password = "password";
    private static String lt = "";
    private static String JSESSION = "";
    private static String ROUTE = "";
    private static String iPlanetDirectoryPro = null;
    private static String loginuri = "http://jwglxt.wtu.edu.cn";
    private static String url = "https://auth.wtu.edu.cn/authserver/login?service=http%3A%2F%2Fjwglxt.wtu.edu.cn%2Fsso%2Fjziotlogin";

    public static void main(String[] ars) throws Exception {
        //定义一个Httpclient对象(相当于浏览器)
        HttpClient httpClient = new HttpClient();


        /*
         * 第一步:第一次Get,获取Cookie和lt
         * */
        //实例化一个get方法
        System.out.println("\n——————————————————————第一次GET——————————————————");
        GetMethod getMethod = new GetMethod(url);
        //发送get
        httpClient.executeMethod(getMethod);
        //获取get的内容
        InputStream inputStream = getMethod.getResponseBodyAsStream();
        StringBuilder output = new StringBuilder();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
        BufferedReader reader = new BufferedReader(inputStreamReader);
        String line = reader.readLine();
        while (line != null) {
            output.append(line);
            line = reader.readLine();
        }
        //解析为文档树
        Document document = Jsoup.parseBodyFragment(output.toString());
        Element body = document.body();
        //找到lt
        lt = body.select("[name=lt]").attr("value");
        System.out.println("lt: " + lt);
        //释放链接
        getMethod.releaseConnection();
        //状态码
        int statusCodeGet1 = getMethod.getStatusCode();
        System.out.println("Status code get1 :" + statusCodeGet1);
        //得到Cookies
        JSESSION = getMethod.getResponseHeader("Set-Cookie").getValue().trim().split(";")[0].split(",")[1].trim().split("=")[1];
        System.out.println("Cookies GET1 Jsession:" + JSESSION);
        ROUTE = getMethod.getResponseHeader("Set-Cookie").getValue().trim().split(";")[0].split(",")[0].trim().split("=")[1];
        System.out.println("Cookies GET1 Route:" + ROUTE);


        /*
         * 第二步:POST 获取Location和iPlanetDirectoryPro并且发送登录数据
         * */
        System.out.println("\n——————————————————————POST——————————————————");
        //拼接LoginURI
        String geturi2 = null;
        String loURI = "https://auth.wtu.edu.cn/authserver/login;jsessionid=";
        loURI += JSESSION;
        loURI += "?service=http%3A%2F%2Fjwglxt.wtu.edu.cn%2Fsso%2Fjziotlogin";
        //POST请求发送登录数据
        PostMethod postMethod = new Urlt().postAction(httpClient, loURI);
        //状态码
        int statusCodePost = postMethod.getStatusCode();
        System.out.println("Status code post :" + statusCodePost);
        //拿到第二步Get网址
        geturi2 = postMethod.getResponseHeader("Location").toString().split(" ")[1];
        System.out.println("Get Uri 2:" + geturi2);
        //拿到iPlanetDirectoryPro
        Header[] headers = postMethod.getResponseHeaders("Set-Cookie");
        iPlanetDirectoryPro = headers[2].getValue().split(";")[0];
        System.out.println("iPlanetDirectoryPro: " + iPlanetDirectoryPro);


        /*
         * 第三步:第二次GET,获取下一步的uri和中间cooki
         * */
        System.out.println("\n——————————————————————第二次GET——————————————————");
        String geturi3 = null;
        String JSESSIONID2 = null;
        //第二次GET请求进入登录界面,拿到第二个JSESSIONID_iPlanetDirectoryPro
        GetMethod getMethod2 = new GetMethod(geturi2);
        getMethod2.addRequestHeader(new Header("Cookie", iPlanetDirectoryPro));
        getMethod2.setFollowRedirects(false);//不自动处理重定向
        int statusCodeGet2 = httpClient.executeMethod(getMethod2);//状态码
        System.out.println("Status code get2 :" + statusCodeGet2);
        //得到重定向网址
        geturi3 = getMethod2.getResponseHeader("Location").toString();
        geturi3 = geturi3.replaceAll(" ", "");//去掉空格
        geturi3 = geturi3.split(":", 2)[1];//得到网址
        System.out.println("Get Uri 3 :" + geturi3);
        //得到set-Cookie 拿到JSESSIONID2
        Header[] headers2 = getMethod2.getResponseHeaders("Set-Cookie");
        JSESSIONID2 = headers2[0].getValue().split(";")[0];
        System.out.println("JSESSIONID2: " + JSESSIONID2);


        /*
         * 第四步 第三次GET 获取下一步uri
         * */
        System.out.println("\n——————————————————————第三次GET——————————————————");
        String geturi4 = null;
        GetMethod getMethod3 = new GetMethod(geturi3);
        getMethod3.setFollowRedirects(false);
        getMethod3.addRequestHeader(new Header("Cookie", JSESSIONID2));
        int statusCodeGet3 = httpClient.executeMethod(getMethod3);
        System.out.println("Status code get3:" + statusCodeGet3);
        //获取下一步URI
        geturi4 = getMethod3.getResponseHeader("Location").toString();
        geturi4 = geturi4.replaceAll(" ", "");//去掉空格
        geturi4 = geturi4.split(":", 2)[1];//得到网址
        System.out.println("Get Uri 4:" + geturi4);


        /*
         * 第五步 第四次GET 获取下一步uri和真实的Cookie
         * */
        System.out.println("\n——————————————————————第四次GET——————————————————");
        String geturi5 = "http://jwglxt.wtu.edu.cn";
        GetMethod getMethod4 = new GetMethod(geturi4);
        getMethod4.setFollowRedirects(false);
        getMethod4.addRequestHeader(new Header("Cookie", iPlanetDirectoryPro));
        //状态和执行
        int statusCodeGet4 = httpClient.executeMethod(getMethod4);
        System.out.println("Status code get4:" + statusCodeGet4);
        //获取下一步URI
        String get4xd;//设置临时变量,相对URI
        get4xd = getMethod4.getResponseHeader("Location").toString();
        get4xd = get4xd.replaceAll(" ", "");//去掉空格
        get4xd = get4xd.split(":", 2)[1];//得到网址
        //拼接真实URI
        geturi5 += get4xd;
        System.out.println("Get Uri 5:" + geturi5);
        //得到set-Cookie 拿到真实
        Header[] headers3 = getMethod2.getResponseHeaders("Set-Cookie");
        JSESSION = headers3[0].getValue().split(";")[0];
        System.out.println("JSESSIONID: " + JSESSION);


        /*
         * 第六部 第五次GET 获取最终的登录页面
         * */
        System.out.println("\n——————————————————————第五次GET——————————————————");
        GetMethod getMethod5 = new GetMethod(geturi5);
        getMethod5.setFollowRedirects(false);
        //设置Cookie
        getMethod5.addRequestHeader(new Header("Cookie", iPlanetDirectoryPro + ";" + JSESSION));
        //状态和执行
        int statusCodeGet5 = httpClient.executeMethod(getMethod5);
        System.out.println("Status code get5:" + statusCodeGet5);
        //获取下一步URI
        String get5xd;//设置临时变量,相对URI
        get5xd = getMethod5.getResponseHeader("Location").toString();
        get5xd = get5xd.replaceAll(" ", "");//去掉空格
        get5xd = get5xd.split(":", 2)[1];//得到网址
        //拼接真实URI
        loginuri += get5xd;
        System.out.println("Loginuri :" + loginuri);


        /*
         * 第七部 第六次GET 登录教务系统
         * */
        System.out.println("\n——————————————————————登录页面——————————————————");
        GetMethod getMethodLog = new GetMethod(loginuri);
        getMethodLog.addRequestHeader(new Header("Cookie", iPlanetDirectoryPro + ";" + JSESSION));
        //状态和执行
        int statusCodelog = httpClient.executeMethod(getMethodLog);
        System.out.println("Status code log:" + statusCodelog);
//        String result = getMethodLog.getResponseBodyAsString();
//        System.out.println("最终结果:\n"+result);



        /*
         * 学籍信息测试
         * */
        System.out.println("\n——————————————————————学籍页面——————————————————");
        String lf="http://jwglxt.wtu.edu.cn/xsxxxggl/xsgrxxwh_cxXsgrxx.html?gnmkdm=N100801&layout=default&su="+usernam;
        GetMethod getMethodtest = new GetMethod(lf);
        getMethodtest.addRequestHeader(new Header("Cookie", iPlanetDirectoryPro + ";" + JSESSION));
        //状态和执行
        int statusCodetest = httpClient.executeMethod(getMethodtest);
        System.out.println("Status code test1" + statusCodetest);
        String resulttest = getMethodtest.getResponseBodyAsString();
        System.out.println(resulttest);


        /*
         * Test 获取课表
         * */
        System.out.println("\n——————————————————————课表——————————————————");
        String kcb = "http://jwglxt.wtu.edu.cn/kbcx/xskbcx_cxXsKb.html?gnmkdm=N253508";
        PostMethod postMethodkcb = new Urlt().postKCB(httpClient, kcb, loginuri, iPlanetDirectoryPro + ";" + JSESSION);
        int kcbcode = postMethodkcb.getStatusCode();
        System.out.println("Status code kcb :" + kcbcode);
        String ss=new String(postMethodkcb.getResponseBody(), Charset.forName("ASCII"));
        System.out.println(ss);

        //读取json
        InputStream in = postMethodkcb.getResponseBodyAsStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(in,"ISO-8859-1"));
        String tempbf;
        StringBuffer html = new StringBuffer(8*1024);
        while ((tempbf = br.readLine()) != null) {
            html.append(tempbf +"\n");
        }
        System.out.println(html);
    }

    static class Urlt {

        //登录
        public PostMethod postAction(HttpClient client, String murl) throws IOException {
            HttpClient mClient = client;
            PostMethod postMethod = new PostMethod(murl);
            //请求头
            postMethod.setRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
            postMethod.setRequestHeader("Accept-Encoding", "gzip, deflate, br");
            postMethod.setRequestHeader("Accept-Language", "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2");
            postMethod.setRequestHeader("Connection", "keep-alive");
            postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            postMethod.setRequestHeader("Cookie", "route=" + ROUTE + "; JSESSIONID_auth=" + JSESSION);
            postMethod.setRequestHeader("Host", "auth.wtu.edu.cn");
            postMethod.setRequestHeader("Origin", "https://auth.wtu.edu.cn");
            postMethod.setRequestHeader("Referer", url);
            postMethod.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0");
            //参数
            postMethod.addParameter(new NameValuePair("username", usernam));
            postMethod.addParameter(new NameValuePair("password", password));
            postMethod.addParameter(new NameValuePair("lt", lt));
            postMethod.addParameter(new NameValuePair("dllt", "userNamePasswordLogin"));
            postMethod.addParameter(new NameValuePair("execution", "e1s1"));
            postMethod.addParameter(new NameValuePair("_eventId", "submit"));
            postMethod.addParameter(new NameValuePair("rmShown", "1"));
            mClient.executeMethod(postMethod);
            return postMethod;
        }

        //请求课表
        public PostMethod postKCB(HttpClient client, String tourl, String refurl, String Cookies) throws IOException {
            HttpClient mClient = client;
            PostMethod postMethod = new PostMethod(tourl);
            //请求头
            postMethod.setRequestHeader("Accept", "*/*");
            postMethod.setRequestHeader("Accept-Encoding", "gzip, deflate, br");
            postMethod.setRequestHeader("Accept-Language", "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2");
            postMethod.setRequestHeader("Connection", "keep-alive");
            postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            postMethod.setRequestHeader("Cookie", Cookies);
            postMethod.setRequestHeader("Host", "auth.wtu.edu.cn");
            postMethod.setRequestHeader("Origin", "https://auth.wtu.edu.cn");
            postMethod.setRequestHeader("Referer", "http://jwglxt.wtu.edu.cn/kbcx/xskbcx_cxXskbcxIndex.html?gnmkdm=N253508&layout=default&su=" + usernam);
            postMethod.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0");
            postMethod.setRequestHeader("X-Requested-With", "XMLHttpRequest");
            //表单数据
            postMethod.addParameter("xnm", "2019");
            postMethod.addParameter("xqm", "12");
            postMethod.setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");

            mClient.executeMethod(postMethod);
            return postMethod;
        }
    }
}

有用的话记得点个赞,可以相互讨论共同进步哦!

转载请注明出处!

你可能感兴趣的:(JAVA使用HttpClient模拟登录正方教务系统,爬取学籍信息和课程表成绩等,超详细登录分析和代码注解)