用httpclient先登录后抓取

本文介绍了一种使用Java程序自动登录特定系统并根据指定条件下载Excel报告的方法。通过构造HTTP请求和设置必要的参数,实现了远程获取定制报表的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近这个小私活有个地方比较变态,让我登陆一个系统自动下载一个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();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值