日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

安卓开发-模拟登陆上海理工大学-方正教务系统(HttpsURLConnection)

發(fā)布時(shí)間:2023/12/14 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 安卓开发-模拟登陆上海理工大学-方正教务系统(HttpsURLConnection) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

通過HttpsURLConnection模擬登錄

今天,寫一下基于方正教務(wù)系統(tǒng)的上海理工大學(xué)教務(wù)系統(tǒng),項(xiàng)目放在了碼云,有需要的可以直接翻到最后查看

首先還是登陸一下,看看我們?cè)诘顷懙臅r(shí)候提交了什么

火狐瀏覽器為例,f12開發(fā)者模式,選擇網(wǎng)絡(luò),然后登錄,找到發(fā)送信息的post選項(xiàng)

可以看到除了賬號(hào)密碼和幾項(xiàng)固定內(nèi)容外,還包含了2個(gè)隨機(jī)數(shù)據(jù)(不確定哪些是會(huì)變化的話可以多次登錄對(duì)比數(shù)據(jù))
那么首先我們就要先尋找這些數(shù)據(jù)的來源,回到登陸界面右鍵查看網(wǎng)頁源代碼。
然后搜索這兩個(gè)數(shù)據(jù)的id,這里比較好獲取,不同學(xué)校可能在不同地方。
那么我們模擬登錄前首先就需要獲取到這兩個(gè)數(shù)據(jù),cookie管理依然是采用自帶CookieManager自動(dòng)管理。
先準(zhǔn)備個(gè)工具類,構(gòu)造函數(shù)中準(zhǔn)備好CookieManager,至于為什么需要cookie可以看我的上一篇博客。

public class Login_download {Context context;Login_download(Context context) {this.context = context;CookieManager cookieManager = new CookieManager();CookieHandler.setDefault(cookieManager);//接受所有cookiecookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);} //其他函數(shù) }

首先我們是需要獲取內(nèi)容的,由于https的安全特性,我們是不能直接使用HttpsURLConnection進(jìn)行登錄,一般有兩種解決方法,這里我們使用比較暴力簡(jiǎn)單的信任所有證書。
直接放代碼(放入工具類)

private static final class DefaultTrustManager implements X509TrustManager {@Overridepublic void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {}@Overridepublic void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {}@Overridepublic X509Certificate[] getAcceptedIssuers() {return null;}} //MOOD為GET或POST URL就是網(wǎng)址private static HttpsURLConnection getHttpsURLConnection(String uri, String method) throws IOException {SSLContext ctx = null;try {ctx = SSLContext.getInstance("TLS");ctx.init(new KeyManager[0], new TrustManager[] { new DefaultTrustManager() }, new SecureRandom());} catch (KeyManagementException e) {e.printStackTrace();} catch (NoSuchAlgorithmException e) {e.printStackTrace();}SSLSocketFactory ssf = ctx.getSocketFactory();URL url = new URL(uri);HttpsURLConnection httpsConn = (HttpsURLConnection) url.openConnection();httpsConn.setSSLSocketFactory(ssf);//設(shè)置一下頭,這里根據(jù)學(xué)校不同需要更改httpsConn.setRequestProperty("Accept",":text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");httpsConn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");httpsConn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0");httpsConn.setHostnameVerifier(new HostnameVerifier() {@Overridepublic boolean verify(String arg0, SSLSession arg1) {return true;}});httpsConn.setRequestMethod(method);//這里其實(shí)是需要細(xì)分的,但是我偷懶了httpsConn.setDoInput(true);httpsConn.setDoOutput(true);httpsConn.setAllowUserInteraction(true);return httpsConn;}

通過這兩個(gè)函數(shù),我們就可以得到一個(gè)信任所有證書的HttpsURLConnection,隨后的登錄操作都使用這樣獲取的HttpsURLConnection。
HttpsURLConnection的用法和HttpURLConnection一樣,這里節(jié)約版面,就不提,不太會(huì)的可以去上一篇,然后把HttpURLConnection直接替換一下就可以( ̄y▽, ̄)╭

下面按步驟放代碼,盡量少講

獲取兩個(gè)數(shù)據(jù)

這里推薦使用正則方法,我這里偷懶,直接subString。

String a=dct.getinf("https://ids6-443.webvpn.usst.edu.cn/authserver/login?service=https%3A%2F%2Fwebvpn.usst.edu.cn%2Fusers%2Fauth%2Fcas%2Fcallback%3Furl%3Dhttp%253A%252F%252Fwww.usst.edu.cn%252Fxywww%252Flist.htm","UTF-8");int yy=a.indexOf("name=\"lt\" value=\"");int yyy=a.indexOf("name=\"execution\" value=\"");String ln=a.substring(yy+17,a.indexOf("\"/>",yy));String execution=a.substring(yyy+24,a.indexOf("\"/>",yyy));String body11="username="+user;String body12="password="+password;String body13="lt="+ln;String body14="dllt=userNamePasswordLogin&execution="+execution+"&_eventId=submit&rmShown=1";body = body11 + "&" + body12 + "&" + body13 + "&" + body14;

body就是我們根據(jù)網(wǎng)頁里獲取的信息,拼接出來的。

隨后發(fā)送該信息

String a=dct.postinf(URL,body,"UTF-8");


這里我們要去本科教務(wù)系統(tǒng)

繼續(xù)觀察可以看到點(diǎn)擊后經(jīng)過多次重定向后才進(jìn)入本科教務(wù)系統(tǒng),這里只需要做一次第一步。

String temp1=dct.getinf("https://jwgl.webvpn.usst.edu.cn/sso/jziotlogin","UTF-8");

學(xué)生課表查詢

這里點(diǎn)擊后我們可以看到進(jìn)行了很多信息,但是GET的操作里并沒有看到我們要的課表,這時(shí)候繼續(xù)向下翻,能看到3個(gè)post,這里面就有我們需要的課表信息
post包含的信息也很簡(jiǎn)單,只包含了年份和學(xué)期數(shù)。這個(gè)數(shù)字我們可以直接從這個(gè)界面獲取,查看源代碼我們可以看到當(dāng)前學(xué)期作為默認(rèn)選項(xiàng)已經(jīng)被選擇

直接獲取,然后jsoup解析

String temp2=dct.getinf(" https://jwgl.webvpn.usst.edu.cn/kbcx/xskbcx_cxXskbcxIndex.html?gnmkdm=N253508&layout=default&su="+username.getText().toString(),sc.get_class_code());Document doc = Jsoup.parse(temp2);Elements inf=doc.getElementsByClass("row");List<Element> HYSOptionList=inf.select("option");String poinf=null;for (Element Option:HYSOptionList) {if (0 != Option.getElementsByAttribute("selected").size()) {if(poinf==null){poinf="xnm="+Option.val()+"&xqm=";}elsepoinf+=Option.val();}}

poinf就是我們獲取課表所需要提交的信息了:xnm=2019&xqm=12
最后一步就很明確了
myclass = dct.postinf(URL, poinf,"UTF-8");
這里的返回值是一個(gè)json字符串,解析方法這里就先不寫了(餓死我了)
直接放代碼,大家可以參考一下

try {JSONObject jsonObject = new JSONObject(myclass);JSONObject info = jsonObject.getJSONObject("xsxx");String id = info.getString("XH_ID");id+= info.getString("XM");id+="的課表";headname=id;//課表標(biāo)題System.out.println(id);JSONArray Class_inf = new JSONArray(jsonObject.getString("kbList"));myclass=Class_inf.toString();for (int i = 0; i < Class_inf.length(); i++) {JSONObject jsonOb = Class_inf.getJSONObject(i);String classtime1 = jsonOb.getString("xqj");String classtime2 = jsonOb.getString("jcor");String name = jsonOb.getString("kcmc");String classroom1 = jsonOb.getString("xqmc");String classroom2 = jsonOb.getString("cdmc");String weeks = jsonOb.getString("zcd");String teacher = jsonOb.getString("xm");Log.d("上課時(shí)間::", "星期"+classtime1+" : "+classtime2+" "+weeks);Log.d("課程名稱::", name);Log.d("上課地址::", classroom1+"-"+classroom2);Log.d("老師::", teacher+"\n");}} catch (JSONException e) {Log.e("jsonArray: ", e.toString());}

至此,上海理工大學(xué)的模擬登錄和課表爬取已經(jīng)結(jié)束啦(撒花),時(shí)間倉促寫的比較粗。至于整個(gè)課表的項(xiàng)目,我已經(jīng)上傳了碼云,有需求的同學(xué)可以去那里看看,包含所有代碼
https://gitee.com/teddydesign/t_timetable?_from=gitee_search

回復(fù):
我這個(gè)其實(shí)就是用的webvpn,見圖一。具體代碼寫的不細(xì),你可以看看我的碼云的項(xiàng)目

String a=dct.getinf("https://ids6-443.webvpn.usst.edu.cn/authserver/login?service=https%3A%2F%2Fwebvpn.usst.edu.cn%2Fusers%2Fauth%2Fcas%2Fcallback%3Furl%3Dhttp%253A%252F%252Fwww.usst.edu.cn%252Fxywww%252Flist.htm","UTF-8");int yy=a.indexOf("name=\"lt\" value=\"");int yyy=a.indexOf("name=\"execution\" value=\"");String ln=a.substring(yy+17,a.indexOf("\"/>",yy));String execution=a.substring(yyy+24,a.indexOf("\"/>",yyy));String body11="username="+user;String body12= "password="+password;String body13= "lt="+ln;String body14= "dllt=userNamePasswordLogin&execution="+execution+"&_eventId=submit&rmShown=1";body = body11 + "&" + body12 + "&" + body13 + "&" + body14;System.out.println(body);

總結(jié)

以上是生活随笔為你收集整理的安卓开发-模拟登陆上海理工大学-方正教务系统(HttpsURLConnection)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。