我正在制作一个URL连接Java应用程序,但是在表单提交时无法获取由javascript生成的表单的值。
< input type="hidden" id="hpts" name="hpts" />
< input type="hidden" id="hptsh" name="hptsh" />
< script type="text/javascript">
var handler = function() {
document.getElementById("hpts").value = "1549723003534";
document.getElementById("hptsh").value = "fCJbFrAEBPgYxVhbyNhvIn4DfvY=";
};
var form = document.getElementById("login_form");
if (form.addEventListener) {
form.addEventListener("submit", handler, false);
} else if (form.attachEvent) {
form.attachEvent("onsubmit", handler);
}
< /script>
我尝试了下面的代码
String _sourcePage = Parser.getSpecificFormValueFromFormWithID(page, "login_form", "_sourcePage");
String __fp = Parser.getSpecificFormValueFromFormWithID(page, "login_form", "__fp");
String hptsh = Parser.getSpecificFormValueFromFormWithClass(page, "minimal-form", "hptsh");
String hpts = Parser.getSpecificFormValueFromFormWithID(page, "login_form", "hpts");
public static String getSpecificFormValueFromFormWithClass(String page, String formClass, String inputFieldName) {
Document doc = Jsoup.parse(page);
Element form = doc.select("form." + formClass).last();
Elements inputElements = form.getElementsByTag("input");
for (Element inputElement : inputElements) {
String formInputName = inputElement.attr("name");
if (formInputName.equals(inputFieldName)) {
return inputElement.attr("value");
}
}
return null;
}
public static String getSpecificFormValueFromFormWithID(String page, String formId, String inputFieldName) {
Document doc = Jsoup.parse(page);
Element form = doc.getElementById(formId);
Elements inputElements = form.getElementsByTag("input");
for (Element inputElement : inputElements) {
String formInputName = inputElement.attr("name");
if (formInputName.equals(inputFieldName)) {
return inputElement.attr("value");
}
}
return null;
}
我想获取表单值,以便可以使用后请求将其发送到服务器。我正在获取除这些输入值以外的所有其他输入值的形式名称/值对。