Salve a tutti, ringrazio anticipatamente chi mi risponderà. Vi spiego cosa voglio fare: In parole povere vorrei creare un programmino java che accede ad un sito web in cui c'è un form di login con 1 campo di scelta e 2 campi area di testo(nome utente e password).
Ho provato con questo codice:
codice:import java.io.*; import java.net.*; import java.util.*; public class PostTest { public static void main(String[] args) { try { String fileName = "PostTest.properties"; Properties props = new Properties(); FileInputStream in = new FileInputStream(fileName); props.load(in); URL url = new URL(props.getProperty("URL")); props.remove("URL"); String r = doPost(url, props); System.out.println(r); } catch (IOException exception) {System.out.println("Error: " + exception); } } public static String doPost(URL url, Properties nameValuePairs) throws IOException{ URLConnection connection = url.openConnection(); connection.setDoOutput(true); PrintWriter out = new PrintWriter(connection.getOutputStream()); Enumeration enumd = nameValuePairs.keys(); while (enumd.hasMoreElements()) { String name = (String)enumd.nextElement(); String value = nameValuePairs.getProperty(name); char ch; if(enumd.hasMoreElements()) ch = '&'; else ch = '\n'; out.print(name + "="+URLEncoder.encode(value) + ch); } out.close(); BufferedReader in; try { in = new BufferedReader(new InputStreamReader(connection.getInputStream())); } catch (FileNotFoundException exception) { InputStream err = ((HttpURLConnection)connection).getErrorStream(); if(err == null) throw exception; in = new BufferedReader(new InputStreamReader(err)); } StringBuffer response= new StringBuffer(); String line; while ((line = in.readLine()) != null) response.append(line + "\n"); in.close(); return response.toString(); } }
Nel file PostTest.properties ci sono i dati da inserire nella form.
Ora il problema è che usando questo codice riesco ad inserire nome utente e password ma non riesco a selezionare il campo di scelta e quindi ad inviare correttamente la richiesta post al server.
C'è qualcuno che potrebbe aiutarmi? anche sconvolgendo il mio codice naturalmente.... sono iterativo e incrementale![]()
![]()
Grazie in anticipo per le risposte.

Rispondi quotando