Java用JSoup组件提取asp.net webform开发网页的viewstate相关相关参数
/** * 从页面中提取特定input的的Value * @param formparams * @param document * @param elementId */ private void addFormHiddenElementValue(List<BasicNameValuePair> formparams, Document document, String elementId) { Element element = document.getElementById(elementId); if(element != null){ String elementValue = element.attr("value"); formparams.add(new BasicNameValuePair(elementId,elementValue)); } }
/** * 取得对应URL的Document实例 * @param url * @return */ private static Document getDocument(String url) { Document document = null; try { document = Jsoup.connect(url).get(); } catch (IOException e) { e.printStackTrace(); } return document; }
Document document = getDocument(url); if(document != null){ List<String> elementIds = new ArrayList<String>(); elementIds.add("__VIEWSTATE"); elementIds.add("__VIEWSTATEGENERATOR"); elementIds.add("__EVENTVALIDATION"); for(String elementId : elementIds){ addFormHiddenElementValue(formparams, document, elementId); } }