Scrivere dati su un server

  • Il seguente codice invia al server un oggetto (tramite il meccanismo della serializzazione) e legge il dato di ritorno:
    
        void doSave () {
            URL TextURL = null;
            URL TextSource = null;
            URLConnection Conn = null;
    
            try {
                TextURL = new URL("http://localhost/cgi-bin/ticm/save?primo.txt");
            } catch (java.net.MalformedURLException err) { 
                System.err.println ("MalformedUrl" + err);
            }
            try {
                Conn = TextURL.openConnection ();
                Conn.setDoOutput (true);
                // Scrive l'oggetto
                java.io.ObjectOutputStream oos = 
                    new java.io.ObjectOutputStream (Conn.getOutputStream ());
                oos.writeObject (sch);
                oos.close ();
    
                // Apre il canale di input per leggere il risultato
                DataInputStream in = new DataInputStream (Conn.getInputStream ());
                String inLine;
                while ((inLine = in.readLine ()) != null) {
                    System.out.println (inLine);
                }
    
            } catch (java.io.IOException err) { 
                System.err.println ("IoException" + err);
            }
        }
    
    

© Ing. Stefano Salvi - All rights reserved