`
ritchrs
  • 浏览: 7879 次
文章分类
社区版块
存档分类
最新评论

用httpclient先登录后抓取

阅读更多
最近这个小私活有个地方比较变态,让我登陆一个系统自动下载一个excel备份和入库,尤其讨厌的是excel的是通过一大堆条件查询出来的,有些条件明面上还没有,需要自己去捉个提取,经过反复测试,终于能拿到符合条件的东西了,初步代码如下:
          DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = null;
            HttpEntity entity = null;
            httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
            HttpPost httpost = new HttpPost("http://....../cdc/login.do?formAction=index&moduleFlag=DI");
            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            nvps.add(new BasicNameValuePair("username", "U25655556"));
            nvps.add(new BasicNameValuePair("passwd", "ruanjian"));
            try {
                httpost.setEntity(new UrlEncodedFormEntity(nvps));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            try {
                httpclient.execute(httpost);
                httpost.abort();
            } catch (IOException e) {
                e.printStackTrace();
            }

            HttpPost excelExport = new HttpPost("http://....../cdc/report/reportExcel.do?formAction=listreport");
            List<NameValuePair> promaters = new ArrayList<NameValuePair>();
//            promaters.add(new BasicNameValuePair("ListZoneCode", "11010600"));
//            promaters.add(new BasicNameValuePair("QueryZoneCode", "11010600"));
//            promaters.add(new BasicNameValuePair("apanagecode", "11010600"));
//            promaters.add(new BasicNameValuePair("rptorgcode", "11010600"));
            promaters.add(new BasicNameValuePair("zoneselect", "apanagecode"));
            promaters.add(new BasicNameValuePair("date_name_s", "intime"));
            promaters.add(new BasicNameValuePair("filltime_start", "2010-05-31"));
            promaters.add(new BasicNameValuePair("filltime_stop", "2010-05-31"));
//            promaters.add(new BasicNameValuePair("disflag", "0"));
            promaters.add(new BasicNameValuePair("audit_flag", "1"));
            try {
                excelExport.setEntity(new UrlEncodedFormEntity(promaters));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            try {
//                ResponseHandler<String> responseHandler = new BasicResponseHandler();
                response = httpclient.execute(excelExport);
                entity = response.getEntity();
            } catch (IOException e) {
                e.printStackTrace();
            }


            try {
//                InputStream inputStream = entity.getContent();
//                InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
//                CSVReader reader = new CSVReader(inputStreamReader);
//                List<String[]> list = reader.readAll();
//                for (String[] strings : list) {
//                    for (String string : strings) {
//                        System.out.println(string);
//                    }
//                }

                FileOutputStream outputStream = new FileOutputStream("E:\\my.csv");
                entity.writeTo(outputStream);
                outputStream.close();
//                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }


            httpclient.getConnectionManager().shutdown();
        }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics