Java模拟登录系统抓取内容【转载】

     没有看考勤的习惯,导致我的一天班白上了,都是钱啊,系统也不发个邮件通知下。。。。
     为了避免以后还有类似状况特别写了个java模拟登录抓取考勤内容的方法(部分代码来自网络),希望有人修改后也可以用上,哈哈!
     公司内部系统,登录没有验证码,如果是有验证码的系统还得找破解方法
     定时器是用的spring的,没用spring的可以自己写个定时器
     用到的组件:httpclient-4.2.5.jar,httpcore-4.2.4.jar,jsoup-1.7.2.jar
     jsoup官方下载地址:http://jsoup.org/download
     httpclient官方下载地址:http://hc.apache.org/downloads.cgi
     废话不多说,贴上我的代码

  1 @Component 
  2 public class Login  extends BaseJobs {
  3 
  4     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
  5     /** 
  6      * 获取当月第一天 
  7      *  
  8      * @return 
  9      */  
 10     public String getFirstDayOfMonth() {  
 11         Calendar lastDate = Calendar.getInstance();  
 12         lastDate.set(Calendar.DATE, 1);// 设为当前月的1号  
 13         return sdf.format(lastDate.getTime());  
 14 
 15     }  
 16 
 17     /** 
 18      * 计算当月最后一天
 19      * @return 
 20      */  
 21     public String getLastDayOfMonth() {  
 22 
 23         Calendar lastDate = Calendar.getInstance();  
 24         lastDate.set(Calendar.DATE, 1);// 设为当前月的1号  
 25         lastDate.add(Calendar.MONTH, 1);// 加一个月,变为下月的1号  
 26         lastDate.add(Calendar.DATE, -1);// 减去一天,变为当月最后一天  
 27         return sdf.format(lastDate.getTime());  
 28 
 29     }  
 30 
 31     //测试
 32     public static void main(String args[]) throws Exception {
 33         Login l = new Login();
 34          l.login(); 
 35     }
 36 
 37 
 38     private static CookieStore cs = new BasicCookieStore();
 39 
 40     //利用spring定时器 每天早上10点一刻抓取并发邮件
 41     @Scheduled(cron = "0 15 10 ? * *")
 42     public void login() throws Exception {
 43         DefaultHttpClient httpclient = new DefaultHttpClient();
 44 
 45         // 创建一个本地上下文信息
 46         HttpContext localContext = new BasicHttpContext();
 47         // 在本地上下问中绑定一个本地存储
 48         localContext.setAttribute(ClientContext.COOKIE_STORE, cs); 
 49         // 目标地址
 50         HttpPost httppost = new HttpPost(
 51                 "http://***/userLogin.do"); 
 52         // 传参
 53         StringEntity reqEntity = new StringEntity(
 54                 "userName=jianancun&password=123456&randNum=565656");
 55         // 设置类型
 56         reqEntity.setContentType("application/x-www-form-urlencoded");
 57         // 设置请求的数据
 58         httppost.setEntity(reqEntity);
 59         // 执行
 60         HttpResponse response = httpclient.execute(httppost);
 61         //取得所有头内容
 62         Header[] headers = response.getAllHeaders();
 63         for (Header h : headers) {
 64             String name = h.getName();
 65             String value = h.getValue();
 66             System.out.println("header : " + h.getName() + ":" + h.getValue()); 
 67             if ("Set-Cookie".equalsIgnoreCase(name)) {
 68                 String[] strs = value.split(";");
 69                 for (String str : strs) {
 70                     String[] cookies = str.split("=");
 71                     //输出cookie名称及标题
 72                    // System.out.println("=============== : " + cookies[0] + ":" + cookies[1]);
 73                     cs.addCookie(new BasicClientCookie(cookies[0], cookies[1]));
 74                 }
 75                 cs.addCookie(new BasicClientCookie("userId", "8888"));
 76                 cs.addCookie(new BasicClientCookie("userName", "jiannancun"));
 77                 cs.addCookie(new BasicClientCookie("state", "0")); 
 78                 cs.addCookie(new BasicClientCookie("iAdmin", "0")); 
 79                 cs.addCookie(new BasicClientCookie("depCode", "0"));
 80             } 
 81         } 
 82         HttpEntity entity = response.getEntity(); 
 83         // 显示结果
 84         BufferedReader reader = new BufferedReader(new InputStreamReader(
 85                 entity.getContent(), "UTF-8"));
 86         String line = null;
 87         //返回是否登录成功的内容 忽略
 88         while ((line = reader.readLine()) != null) {
 89           //  System.out.println(line);
 90         }
 91 
 92 
 93 
 94        //可以添加多个用户
 95        String jlc[] ={URLEncoder.encode("贱男春"),"jiannancun@*.cn","888888"}; 
 96        List<String[]> list = new ArrayList<String[]>();
 97        list.add(jlc);
 98 
 99        for(String []u:list){
100            //查询本月考勤内容
101            String logPath = "http://**.cn/timeCard.jsp?nickName="+u[0]+"&eplTimeCard="
102            +u[2]+"&begDate="+getFirstDayOfMonth()+"&endDate="+getLastDayOfMonth();
103            String content= getContent(logPath); 
104             Document doc = Jsoup.parse(content);
105             Elements tds = doc.select("table td");
106             int i =0;
107             //返回的内容
108             String html =""; ;
109              for (Element td : tds) {
110                 //取前25行内容   前25行内容显示的是昨天和今天的考勤记录 
111                  if(i<25){
112                   html+=td.text().replace(u[2],URLDecoder.decode(u[0]))+"<br>" ;
113                  } else break;
114 
115                  i++;
116              } 
117             Map<String,Object> map = new HashMap<String,Object>();
118                 map.put("mailTo",u[1]);
119                 map.put("mailTitle","考勤提醒");
120                 map.put("messageBody",html); 
121                 //发送邮件
122                 //sendMail(map);
123        }
124     }
125     /**
126      *  Function: 请求地址并返回页面内容
127      *  @author JNC
128      *  @param url
129      *  @return
130      *  @throws Exception
131      */
132     private String getContent(String url) throws Exception {
133         DefaultHttpClient httpclient = new DefaultHttpClient();
134         String cookieStr = "";
135         List<Cookie> list = cs.getCookies();
136         for (Cookie cookie : list) {
137             cookieStr += cookie.getName() + "=" + cookie.getValue() + ";";
138         } 
139         // 请求目标地址并带上cookie
140         HttpGet httpget = new HttpGet(url);
141         httpget.setHeader("Cookie", cookieStr); 
142         // 执行
143         HttpResponse response = httpclient.execute(httpget);
144         HttpEntity entity = response.getEntity();
145         String resultStr ="";
146         BufferedReader reader = new BufferedReader(new InputStreamReader(
147                 entity.getContent(), "UTF-8"));
148         String line = null;
149         while ((line = reader.readLine()) != null) {
150             resultStr+=line;
151         }
152        return resultStr;
153     }
154 }

原文地址:http://www.liutime.com/javainfo/2150/

 

posted @ 2014-03-04 23:19  dekevin  阅读(1299)  评论(0编辑  收藏  举报