生活随笔
收集整理的這篇文章主要介紹了
安卓开发-模拟登陆上海理工大学-方正教务系统(HttpsURLConnection)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
通過HttpsURLConnection模擬登錄
今天,寫一下基于方正教務(wù)系統(tǒng)的上海理工大學教務(wù)系統(tǒng),項目放在了碼云,有需要的可以直接翻到最后查看
首先還是登陸一下,看看我們在登陸的時候提交了什么
火狐瀏覽器為例,f12開發(fā)者模式,選擇網(wǎng)絡(luò),然后登錄,找到發(fā)送信息的post選項
可以看到除了賬號密碼和幾項固定內(nèi)容外,還包含了2個隨機數(shù)據(jù)(不確定哪些是會變化的話可以多次登錄對比數(shù)據(jù))
那么首先我們就要先尋找這些數(shù)據(jù)的來源,回到登陸界面右鍵查看網(wǎng)頁源代碼。
然后搜索這兩個數(shù)據(jù)的id,這里比較好獲取,不同學??赡茉诓煌胤健?br /> 那么我們模擬登錄前首先就需要獲取到這兩個數(shù)據(jù),cookie管理依然是采用自帶CookieManager自動管理。
先準備個工具類,構(gòu)造函數(shù)中準備好CookieManager,至于為什么需要cookie可以看我的上一篇博客。
public class Login_download {Context context
;Login_download(Context context
) {this.context
= context
;CookieManager cookieManager
= new CookieManager();CookieHandler
.setDefault(cookieManager
);cookieManager
.setCookiePolicy(CookiePolicy
.ACCEPT_ALL
);}
}
首先我們是需要獲取內(nèi)容的,由于https的安全特性,我們是不能直接使用HttpsURLConnection進行登錄,一般有兩種解決方法,這里我們使用比較暴力簡單的信任所有證書。
直接放代碼(放入工具類)
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
;}}
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
);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
);httpsConn
.setDoInput(true);httpsConn
.setDoOutput(true);httpsConn
.setAllowUserInteraction(true);return httpsConn
;}
通過這兩個函數(shù),我們就可以得到一個信任所有證書的HttpsURLConnection,隨后的登錄操作都使用這樣獲取的HttpsURLConnection。
HttpsURLConnection的用法和HttpURLConnection一樣,這里節(jié)約版面,就不提,不太會的可以去上一篇,然后把HttpURLConnection直接替換一下就可以( ̄y▽, ̄)╭
下面按步驟放代碼,盡量少講
獲取兩個數(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ù)觀察可以看到點擊后經(jīng)過多次重定向后才進入本科教務(wù)系統(tǒng),這里只需要做一次第一步。
String temp1
=dct
.getinf("https://jwgl.webvpn.usst.edu.cn/sso/jziotlogin","UTF-8");
學生課表查詢
這里點擊后我們可以看到進行了很多信息,但是GET的操作里并沒有看到我們要的課表,這時候繼續(xù)向下翻,能看到3個post,這里面就有我們需要的課表信息
post包含的信息也很簡單,只包含了年份和學期數(shù)。這個數(shù)字我們可以直接從這個界面獲取,查看源代碼我們可以看到當前學期作為默認選項已經(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");
這里的返回值是一個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
;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("上課時間::", "星期"+classtime1
+" : "+classtime2
+" "+weeks
);Log
.d("課程名稱::", name
);Log
.d("上課地址::", classroom1
+"-"+classroom2
);Log
.d("老師::", teacher
+"\n");}} catch (JSONException e
) {Log
.e("jsonArray: ", e
.toString());}
至此,上海理工大學的模擬登錄和課表爬取已經(jīng)結(jié)束啦(撒花),時間倉促寫的比較粗。至于整個課表的項目,我已經(jīng)上傳了碼云,有需求的同學可以去那里看看,包含所有代碼
https://gitee.com/teddydesign/t_timetable?_from=gitee_search
回復(fù):
我這個其實就是用的webvpn,見圖一。具體代碼寫的不細,你可以看看我的碼云的項目
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)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。