会议室预定模拟登陆网站

/*预定会议室 每天早晨一点定会议室   addOAMeeting   */
//@Scheduled(cron = "0 0 1 * * ? ")
public void addOAMeeting() {
String bdUrl = Global.getConfig("jdbc.url");
if(!StringUtils.isEmpty(bdUrl)&&bdUrl.contains("ip")){

}else{
return;
}
String username = Global.getConfig("jdbc.username");
if(!"DMS_TEST".equals(username)){
return ;
}

String url = "http://ip:port/IMC/a/login" ;
Connection con = Jsoup
.connect(url);// 获取连接
con.header("User-Agent",
" Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0");// 配置模拟浏览器
Connection.Response rs = null;// 获取响应
try {
rs = con.execute();
} catch (IOException e) {
e.printStackTrace();
}
Document d1 = Jsoup.parse(rs.body());// 转换为Dom树
List et = d1.select("#loginForm");// 获取form表单,可以通过查看页面源码代码得知
// 获取,cooking和表单属性,下面map存放post时的数据
Map datas = new HashMap<>();
for (Element e : et.get(0).getAllElements()) {
if (e.attr("name").equals("username")) {
e.attr("value", "0546974");// 设置用户名
}
if (e.attr("name").equals("password")) {
e.attr("value", "123456"); // 设置用户密码
}
if (e.attr("name").length() > 0) {// 排除空值表单属性
datas.put(e.attr("name"), e.attr("value"));
}
}
/**
* 第二次请求,post表单数据,以及cookie信息
*
* **/
Connection con2 = Jsoup
.connect(url);
con2.header("User-Agent",
" Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0");
// 设置cookie和post上面的map数据
Response login = null;
System.out.println(rs.cookies());
try {
login = con2.ignoreContentType(true).method(Method.POST)
.data(datas).cookies(rs.cookies()).execute();
} catch (IOException e) {
e.printStackTrace();
}
// 打印,登陆成功后的信息
System.out.println(login.body());

// 登陆成功后的cookie信息,可以保存到本地,以后登陆时,只需一次登陆即可
Map map = login.cookies();
for (String s : map.keySet()) {
System.out.println(s + " " + map.get(s));
}

/*第三次请求*/
Connection con3 = Jsoup
.connect("http://ip:port/IMC/a/meetbook/meetBook/save");
con3.header("User-Agent",
" Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0");
// 设置cookie和post上面的map数据
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + 7);
Date today = calendar.getTime();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
/*获取7天后的时间*/
String time = format.format(today);
Map datas1 = new HashMap<>();
datas1.put("jobNum","card");
datas1.put("name","name");
datas1.put("office.id","D010001498");
datas1.put("office.name","平台开发部");
datas1.put("phone","tel");
datas1.put("workSpace","科技园");
datas1.put("meetCode","D2");
datas1.put("dateTime",time);
datas1.put("startTime","08:00");
datas1.put("endTime","08:30");
datas1.put("theme","");
Response login1 = null;
try {
login1 = con3.ignoreContentType(true).method(Method.POST)
.data(datas1).cookies(rs.cookies()).execute();
} catch (IOException e) {
e.printStackTrace();
}
// 打印,登陆成功后的信息
System.out.println(login1.body());
logger.error("*******");
logger.error(login1.body());
}

转载于:https://www.cnblogs.com/a6948076/p/11473900.html

你可能感兴趣的:(会议室预定模拟登陆网站)