import java.applet.*; import java.awt.*; import java.net.*; import java.io.*; public class PostNoThread extends Applet { Button postButton; TextField tf; public void init() { tf = new TextField(10); add(tf); postButton = new Button ("Type a String and then press this button"); add (postButton); URLPoster.refreshStatics(); } public boolean action(Event evt, Object obj) { if (evt.target == postButton) { try { URLPoster.numberStarted++; new URLPoster(URLPoster.numberStarted, "TestExported", "test.txt", tf.getText() + ": data from run " + String.valueOf(URLPoster.numberStarted)); System.out.println(URLPoster.numberStarted + " POST is done"); } catch (Exception e) { System.out.println("send failed"); return true; } } return true; } } // END OF Class PostNoThread class URLPoster { URL urlCGI; String query, file, ext; int numberOfThis; static int numberStarted; URLPoster(int numberOfThis, String folder, String fileWithExt, String data) { this.numberOfThis = numberOfThis; urlCGI = null; try { urlCGI = new URL("http://segal.org/cgi-bin/form2mail"); } catch (Exception e) { urlCGI = null; System.out.println("url could not be formed" + e); } int dotPosition = fileWithExt.indexOf('.'); file = fileWithExt.substring(0, dotPosition); ext = fileWithExt.substring(dotPosition + 1); query = URLEncoder.encode("config") + "=" + URLEncoder.encode("/templates/" + folder + "Append.txt"); addToQuery("file", file); addToQuery("ext", ext); addToQuery("data", data); System.out.println(numberOfThis + " POST starting"); post(); } static final void refreshStatics() { numberStarted = 0; } final void addToQuery (String name, String value) { query += ("&" + URLEncoder.encode(name) + "=" + URLEncoder.encode(value)); } final void post() { try { URLConnection uc = urlCGI.openConnection(); System.out.println(numberOfThis + " uc = " + uc); uc.setUseCaches(false); System.out.println(numberOfThis + " point A"); uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); System.out.println(numberOfThis + " point B"); // setRequestProperty needed since some Netscape versions don't give this out properly uc.setAllowUserInteraction(false); System.out.println(numberOfThis + " point C"); uc.setDoInput(true); System.out.println(numberOfThis + " point D"); uc.setDoOutput(true); // after setRequestProperty which can reset doOutput to false System.out.println(numberOfThis + " point E"); DataOutputStream udos = new DataOutputStream(uc.getOutputStream ()); System.out.println(numberOfThis + " point F"); udos.writeBytes(query); System.out.println(numberOfThis + " point G"); udos.flush(); // sends System.out.println(numberOfThis + " point H"); udos.close(); // cgi program won't return response until this is closed System.out.println(numberOfThis + " point I"); DataInputStream udis = new DataInputStream(uc.getInputStream ()); System.out.println(numberOfThis + " point J"); // NN won't post data unless inputStream is opened, but reading input is not necessary udis.close(); // always close after opening System.out.println(numberOfThis + " point K"); } catch (Exception e) { System.out.println("Error in posting"); } } } // END OF Class URLPoster