public static List<NameValuePairString> formFieldsFromString(String fromFiddler) { List<NameValuePairString> fields = new ArrayList<NameValuePairString>(); String[] pairs = fromFiddler.split("&"); for (String couple : pairs) { String name; String value; name = couple.split("=")[0]; value = couple.split("=")[1]; fields.add(new NameValuePairString(name, value)); } return fields; }
public static boolean getFormFields( ResponseWrapper rw, List<NameValuePairString> hiddenFormFields, String formSelector) { // --- analisi della pagina contente la form, specifica al sito Document doc = rw.getJSoupDocument(); Elements els = doc.select(formSelector); // per debug, dovrebbe essere uo if (els == null || els.size() <= 0) { log.error("unable to find form at selector: " + formSelector); System.exit(1); return false; } Element loginForm = els.get(0); if (loginForm == null) { log.error("failed to get form to analyze at: " + rw.dump()); System.exit(1); } // log.info("login form OUTER HTML\n" + loginForm.outerHtml()); Elements inputFields = loginForm.select("input"); // display all for (Element e : inputFields) { String type = e.attr("type"); if (type.equals("submit")) { continue; } String attrName = e.attr("name"); hiddenFormFields.add(new NameValuePairString(attrName, e.val())); log.debug("captured form input: " + attrName + " = " + e.val()); } return false; }
/** * Used to set form fields * * @param formFields * @param replaceVals * @param removeList * @param addList * @return */ public static boolean setFormParams( List<NameValuePairString> formFields, List<NameValuePairString> replaceVals, List<String> removeList, List<NameValuePairString> addList) { // campi da rimuovere if (removeList != null) for (NameValuePairString nvp : formFields) { for (String removeKey : removeList) { if (removeKey.equals(nvp.getKey())) { formFields.remove(nvp); continue; } } } // replace values if (replaceVals != null) for (NameValuePairString nvp : formFields) { for (NameValuePairString repl : replaceVals) { if (nvp.getKey().equals(repl.getKey())) { nvp.setValue(repl.getValue()); } } } // addMap name value pairs if (addList != null) for (NameValuePairString nvp : addList) { formFields.add(nvp); } // rimuoviamo campi con chiave nulla, non so perchè ma ne pesco ArrayList<NameValuePairString> temp = new ArrayList<NameValuePairString>(); for (NameValuePairString nvp : formFields) { if (nvp.getKey().length() > 0) { temp.add(nvp); } else { log.info("beccata chiave nulla, valore: " + nvp.getValue()); } } formFields.clear(); formFields.addAll(temp); return true; }