Android模拟登陆教务系统爬取课程表
之前为项目做了个模拟登陆教务系统的爬虫,由于你懂的拖延症一直没把博客写出来,终于这天还是来写了。希望为大环境做一点点贡献,把中间的过程尽可能详细简单地写成博客分享出来。
我使用到的:
- IDE:AndroidStudio(使用框架:Jsoup,OkHttpUtils,RxAndroid)
- 抓包工具:HttpWatch Professional8.5 + IE (或是直接打开Chrome的开发者模式选择NetWork
这里安利一下HttpWatch~
链接: https://pan.baidu.com/s/147dLjKLcFWJob9IPasVojw
提取码: age9
项目定义是学生在何处通过任何网络方式都能访问到教务系统,那么就不能局限于校园网,所以我采用的是学校提供的vpn访问教务系统以此模拟登陆行为。但是没关系,模拟教务登陆行为和校园网登陆是一样的,在这里不讨论vpn登陆等相关问题。
废话不多说,下面开始整个模拟登陆的流程:
1. 获取Cookie
我们都知道,cookie是网站用来辨别用户身份的数据,我们学校教务系统使用的就是cookie来跟踪分辨用户【当然这是抓包后知道的】,那么我们就需要发起一次get请求以此获取Cookie,同时记录在客户端。
/**
* 请求新的Cookie
*/
private void GetCookies() {
cookie = "";
Observable.create((ObservableOnSubscribe<String>) emitter -> OkHttpUtils.get()
.url(NetConfig.BASE_EDU_PLUS)
.build()
.execute(new Callback() {
@Override
public Object parseNetworkResponse(Response response, int id) throws Exception {
Headers headers = response.headers();
for (int i = 0; i < headers.size(); i++) {
if (headers.name(i).equals("Set-Cookie"))
if (headers.value(i).endsWith(" Path=/"))
cookie += headers.value(i).substring(0, headers.value(i).length() - 7);
else
cookie += headers.value(i);
}
emitter.onNext(response.body().string());
return null;
}
@Override
public void onError(Call call, Exception e, int id) {
emitter.onError(new Throwable("GetCookies onError" + call.toString()));
}
@Override
public void onResponse(Object response, int id) {
Log.e("print", "GetCookies onResponse");
}
}))
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe