javaweb 要再后台自动提交buffalo文件到网页上,采用爬虫的方式。
提交成功后:
使用的方法是httpClient,用post模拟请求。
要注意一点是文件要通过相对目录找到绝对目录,才能找到这个文件,再去上传。
String url = "http://bdp.jd.com/buffalo/task/add-grace.html?businessType=001";
HttpPost post = new HttpPost(url);
CloseableHttpClient httpClient = HttpClients.createDefault();
String cookie = "XXXX";
String user_agent = "\"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36";
post.addHeader("Cookie",cookie);
post.addHeader("Accept","application/json, text/javascript, */*; q=0.01");
post.addHeader("User-Agent",user_agent);
String filename = "mytest5.py";
String filepath;
filepath = request.getSession().getServletContext().getRealPath("/WEB-INF/uploads/"+filename);
FileBody bin = new FileBody(new File(filepath));
HttpEntity reqEntity = MultipartEntityBuilder.create()
.addPart("jsdAppgroupId",new StringBody("10656", ContentType.TEXT_PLAIN))
.addPart("managers",new StringBody("XXXXX", ContentType.TEXT_PLAIN))
.addPart("description",new StringBody("测试", ContentType.TEXT_PLAIN))
.addPart("calEngine",new StringBody("hive", ContentType.TEXT_PLAIN))
.addPart("verDescription",new StringBody("测试", ContentType.TEXT_PLAIN))
.addPart("model",new StringBody("001", ContentType.TEXT_PLAIN))
.addPart("param",new StringBody("[{\"name\":\"hive\",\"desc\":\"hive\"}]", ContentType.TEXT_PLAIN))
.build();
post.setEntity(reqEntity);
HttpResponse response = httpClient.execute(post);
//结果显示
InputStream in = response.getEntity().getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(in, "utf-8"));
StringBuilder strber= new StringBuilder();
String line = null;
while((line = br.readLine())!=null){
strber.append(line+'\n');
}
br.close();
in.close();
String result = strber.toString();
logger.info(result);
一种webClient的方法,可以简单点击和填充:
WebClient webClient = new WebClient();
webClient.getOptions().setCssEnabled(false);
webClient.getOptions().setJavaScriptEnabled(false);
//登录
HtmlPage page = webClient.getPage("http://bdp.jd.com/login.html");
HtmlForm htmlForm = page.getForms().get(0);
HtmlInput erp = htmlForm.getInputByName("erp");
HtmlInput password = htmlForm.getInputByName("password");
erp.setValueAttribute("xxx");
password.setValueAttribute("xxx");
HtmlPage page2 = htmlForm.getElementsByTagName("button").get(0).click();
logger.info(page2.asXml());