共计 10504 个字符,预计需要花费 27 分钟才能阅读完成。
download:Web 前端架构师
package com.ican.yueban.jwgl;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import com.ican.yueban.utils.DateUtils;
import com.ican.yueban.utils.GlobalConstant;
import com.ican.yueban.utils.IOUtils;
import com.ican.yueban.utils.ParseUtils;
public class Jwgl {
private static String stuNumber = "";
private static String stuName = "";
private static String Cookie = "";
private String indexUrl = GlobalConstant.INDEX_URL;
private String secretCodeUrl = GlobalConstant.SECRETCODE_URL;
private String loginUrl = GlobalConstant.LOGIN_URL;
private String mainUrl = GlobalConstant.MAIN_URL;
private String queryClassroomUrl = GlobalConstant.QUERY_CLASSROOM_URL;
private String queryClassroomGnmkdm = GlobalConstant.QUERY_CLASSROOM_GNMKDM;
private String queryStuGradeUrl = GlobalConstant.QUERY_STU_GRADE_URL;
private String queryStuGradeGnmkd = GlobalConstant.QUERY_STU_GRADE_GNMKDM;
private String queryStuCourseUrl = GlobalConstant.QUERY_STU_COURSE_URL;
private String queryStuCourseGnmkd = GlobalConstant.QUERY_STU_COURSE_GNMKDM;
private String xixiaoqu = GlobalConstant.XIXIAOQU;
private String identityStu = GlobalConstant.IDENTITY_STU;
/**
* 登录性能
*
* @param stuNumber
* @param password
* @return
* @throws Exception
* @throws UnsupportedOperationException
*/
public boolean login(String stuNumber, String password)
throws UnsupportedOperationException, Exception {
this.stuNumber = stuNumber;
// 获取验证码
HttpGet secretCodeGet = new HttpGet(secretCodeUrl);
CloseableHttpClient client = HttpClients.createDefault();
CloseableHttpResponse responseSecret = client.execute(secretCodeGet);
// 获取返回的 Cookie
Cookie = responseSecret.getFirstHeader("Set-Cookie").getValue();
String viewState = IOUtils.getViewState(indexUrl, "","");
// 将验证码下载到 C 盘
IOUtils.getSecret(responseSecret.getEntity().getContent(),
"secretCode.png", "c://");
Scanner sc = new Scanner(System.in);
System.out.println("请输出验证码:");
// 手动填充方才获取的验证码的值
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));// 第几节
queryClassroomPair.add(new BasicNameValuePair("Button2", "空教室查问"));
queryClassroomPair.add(new BasicNameValuePair("xn", "2015-2016"));
queryClassroomPair.add(new BasicNameValuePair("xq", "2"));
queryClassroomPair.add(new BasicNameValuePair("ddlSyXn", "2015-2016"));
queryClassroomPair.add(new BasicNameValuePair("ddlSyxq", "2"));
UrlEncodedFormEntity entityClassroom = new UrlEncodedFormEntity(queryClassroomPair);
HttpPost queryClassroomPost = new HttpPost(newQueryClassrommUrl);
// newQueryClassrommUrl 示例:http://jwgl2.ujn.edu.cn/xxjsjy.aspx?xh=20121214104&xm=XXX&gnmkdm=N121611
queryClassroomPost.setEntity(entityClassroom);
queryClassroomPost.setHeader("Referer", mainUrl + stuNumber);// 设置头信息
queryClassroomPost.setHeader("Cookie", Cookie);
HttpResponse responseClassroom = client.execute(queryClassroomPost);
InputStream is = responseClassroom.getEntity().getContent();
String html = IOUtils.getHtml(is, "GB2312");
Document doc = Jsoup.parse(html);
Elements eleClassroom = doc.select("td");
Elements eleInfo = doc.select("#lblbt");
System.out.println(eleInfo.get(0).text());
for (int i = 0; i < eleClassroom.size(); i++) {
// 只打印教室名称
if (i % 8 == 1) {System.out.println(eleClassroom.get(i).text());
}
}
client.close();}
/**
* 重载查问空教室办法,默认工夫,课程节次的无参数查询方法
*
* @throws IOException
* @throws ClientProtocolException
*/
public void queryClassroom() throws ClientProtocolException, IOException,
Exception {String weekDay = DateUtils.getWeekDay() + "";// 获取以后工夫是星期几
String sdj = DateUtils.getNowCourse() + "";// 获取以后工夫是第几节课
new Jwgl().queryClassroom(xixiaoqu, weekDay, sdj);
}
/**
* 查问集体问题办法
*
* @throws ClientProtocolException
* @throws IOException
*/
public void queryStuGrade(String xn, String xq)
throws ClientProtocolException, IOException {CloseableHttpClient client = HttpClients.createDefault();
String newQueryStuGradeUrl = queryStuGradeUrl + stuNumber + "&xm="
+ stuName + queryStuGradeGnmkd;
HttpPost queryGradePost = new HttpPost(newQueryStuGradeUrl);
String viewState = IOUtils.getViewState(newQueryStuGradeUrl, Cookie,
mainUrl + stuNumber);
// 封装申请参数
List<NameValuePair> queryGradePair = new ArrayList<NameValuePair>();
queryGradePair.add(new BasicNameValuePair("__EVENTTARGET", ""));
queryGradePair.add(new BasicNameValuePair("__EVENTARGUMENT", ""));
queryGradePair.add(new BasicNameValuePair("__VIEWSTATE", viewState));
queryGradePair.add(new BasicNameValuePair("hidLanguage", ""));
queryGradePair.add(new BasicNameValuePair("ddlXN", xn));// 学年
queryGradePair.add(new BasicNameValuePair("ddlXQ", xq));// 学期
queryGradePair.add(new BasicNameValuePair("ddl_kcxz", ""));
queryGradePair.add(new BasicNameValuePair("btn_xq", "学期成绩"));
queryGradePost.setHeader("Cookie", Cookie);
queryGradePost.setHeader("Referer", mainUrl + stuNumber);
UrlEncodedFormEntity entityGrade = new UrlEncodedFormEntity(queryGradePair);
queryGradePost.setEntity(entityGrade);
HttpResponse responQueryGradePost = client.execute(queryGradePost);
String gradeHtml = IOUtils.getHtml(responQueryGradePost.getEntity()
.getContent(), "GB2312");
// System.out.println(gradeHtml);
Document gradeDoc = Jsoup.parse(gradeHtml);
Elements eleGrade = gradeDoc.select("td");
// 按需要解析 html<td> 标签内容并输入
for (int i = 0; i < 7; i++) {System.out.println(eleGrade.get(i).text());
}
for (int i = 11; i < eleGrade.size(); i = i + 10) {if (i + 15 < eleGrade.size()) {System.out.print(eleGrade.get(i).text() + " ");
i = i + 5;
System.out.print(eleGrade.get(i).text());
System.out.println();}
client.close();}
}
/**
* 查问集体课表办法
*
* @param xnd
* @param xqd
* @throws ClientProtocolException
* @throws IOException
*/
public void queryStuCourse(String xnd, String xqd)
throws ClientProtocolException, IOException {CloseableHttpClient client = HttpClients.createDefault();
String newQueryStuCourseUrl = queryStuCourseUrl + stuNumber + "&xm="
+ stuName + queryStuCourseGnmkd;
String viewState = IOUtils.getViewState(newQueryStuCourseUrl, Cookie,
mainUrl + stuNumber);
HttpPost queryStuCoursePost = new HttpPost(newQueryStuCourseUrl);
List<NameValuePair> stuCoursePair = new ArrayList<NameValuePair>();
stuCoursePair.add(new BasicNameValuePair("__EVENTTARGET", "xqd"));
stuCoursePair.add(new BasicNameValuePair("__EVENTARGUMENT", ""));
stuCoursePair.add(new BasicNameValuePair("__VIEWSTATE", viewState));
stuCoursePair.add(new BasicNameValuePair("xnd", xnd));
stuCoursePair.add(new BasicNameValuePair("xqd", xqd));
UrlEncodedFormEntity entitySource = new UrlEncodedFormEntity(stuCoursePair);
queryStuCoursePost.setEntity(entitySource);
queryStuCoursePost.setHeader("Cookie", Cookie);
queryStuCoursePost.setHeader("Referer", mainUrl + stuNumber);
HttpResponse responseStuCourse = client.execute(queryStuCoursePost);
String html = IOUtils.getHtml(responseStuCourse.getEntity()
.getContent(), "GB2312");
Document docCourse = Jsoup.parse(html);
Elements eleCourse = docCourse.select("td");
for (int i = 2; i < eleCourse.size(); i++) {System.out.print(eleCourse.get(i).text() + " ");
if (i % 9 == 0) {System.out.println();
}
}
}
public static void main(String[] args) {Jwgl jw = new Jwgl();
try {jw.login("这里是学号", "这里是明码");
System.out.println("查问问题测试 -------");
jw.queryStuGrade("2015-2016", "1");
// 查问西校区,周一,第 12 节空教室测试。// jw.queryClassroom("1", "1", "2");
System.out.println("查问空教室测试 ------");
jw.queryClassroom();
System.out.println("查问集体课表测试 -------");
jw.queryStuCourse("2014-2015", "1");
} catch (ClientProtocolException e) {e.printStackTrace();
} catch (IOException e) {e.printStackTrace();
} catch (Exception e) {e.printStackTrace();
}
//QQ:451209214