// 手动填充刚才获取的验证码的值
String secret = sc.next().trim();
HttpPost loginPost = new HttpPost(loginUrl);// 创建登录的Post请求
loginPost.setHeader("Cookie", Cookie);// 带上第一次请求的Cookie
List<NameValuePair> nameValuePairLogin = new ArrayList<NameValuePair>();// 封装Post提交参数
nameValuePairLogin
.add(new BasicNameValuePair("__VIEWSTATE", viewState));// 隐藏表单值
nameValuePairLogin
.add(new BasicNameValuePair("txtUserName", stuNumber));// 学号
nameValuePairLogin.add(new BasicNameValuePair("TextBox2", password));// 密码
nameValuePairLogin.add(new BasicNameValuePair("txtSecretCode", secret));// 验证码
nameValuePairLogin.add(new BasicNameValuePair("RadioButtonList1",
identityStu));// 身份,默认学生
nameValuePairLogin.add(new BasicNameValuePair("Button1", ""));
nameValuePairLogin.add(new BasicNameValuePair("lbLanguage", ""));
nameValuePairLogin.add(new BasicNameValuePair("hidPdrs", ""));
nameValuePairLogin.add(new BasicNameValuePair("hidsc", ""));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(
nameValuePairLogin, "GB2312");
loginPost.setEntity(entity);
HttpResponse responseLogin = client.execute(loginPost);
// client1.close();
// 第三步:判断提交数据是否成功,成功返回302
if (responseLogin.getStatusLine().getStatusCode() == 302) {
// 如果提交成功,带着Cookie请求重定向的main页面,并获取学生姓名
HttpGet mainGet = new HttpGet(mainUrl + stuNumber);
mainGet.setHeader("Cookie", Cookie);
mainGet.setHeader("Referer", loginUrl);
HttpResponse responseMain = client.execute(mainGet);
InputStream is = responseMain.getEntity().getContent();
String html = "";
try {
html = IOUtils.getHtml(is, "GB2312");
} catch (Exception e) {
System.out.println("解析html失败!");
e.printStackTrace();
}
stuName = Jsoup.parse(html).getElementById("xhxm").text();
System.out.println("登录成功!欢迎您:" + stuName);
client.close();
return true;
} else {
System.out.println("登录失败!");
client.close();
return false;
}
}
/**
* 查询空教室
*
* @throws Exception
*
* @throws Exception
*/
public void queryClassroom(String xiaoqu, String xqj, String sjd)
throws Exception {
CloseableHttpClient client = HttpClients.createDefault();
String newQueryClassrommUrl = queryClassroomUrl + stuNumber + "&xm="
+ stuName + queryClassroomGnmkdm;// 拼接请求的Url
String parseSjd = ParseUtils.parseWeek(sjd);// 解析当前节次对应的字符串
String nowWeek = DateUtils.getWeek() + "";// 获取当前时间是第几周
String viewState = IOUtils.getViewState(newQueryClassrommUrl, Cookie,
mainUrl + stuNumber);
// 封装查询空教室请求参数
List<NameValuePair> queryClassroomPair = new ArrayList<NameValuePair>();
queryClassroomPair.add(new BasicNameValuePair("__EVENTTARGET", ""));
queryClassroomPair.add(new BasicNameValuePair("__EVENTARGUMENT", ""));
queryClassroomPair
.add(new BasicNameValuePair("__VIEWSTATE", viewState));
queryClassroomPair.add(new BasicNameValuePair("xiaoq", xiaoqu));// 校区类型,默认西校区
queryClassroomPair.add(new BasicNameValuePair("jslb", ""));// 教室类别,默认为空
queryClassroomPair.add(new BasicNameValuePair("min_zws", "0"));// 最小座位数,默认为0
queryClassroomPair.add(new BasicNameValuePair("max_zws", ""));// 最大座位数,默认为空
queryClassroomPair.add(new BasicNameValuePair("ddlKsz", nowWeek));// 起始周,默认当前周
queryClassroomPair.add(new BasicNameValuePair("ddlJsz", nowWeek));// 结束周,默认当前周
queryClassroomPair.add(new BasicNameValuePair("xqj", xqj));// 星期几,默认当天
queryClassroomPair.add(new BasicNameValuePair("ddlDsz", ""));// 单双周,默认
queryClassroomPair.add(new BasicNameValuePair("sjd", parseSjd));// 第几节