import java.net.*; import java.io.*; // By: L.Goulet webmaster@java.dyn.ml.org // // This shows how to avoid browser caching when you fetch documents // // compile it and use: // // java No_browser_cache www.yahoo.com index.html // // or // java No_browser_cache your_host your_file // // To see yahoo's main page or whatever appear at System.out // // Don't take the chance to have your files cached by some browser // and have your product fail if update is critical for you! // // Socket connections migth not be allowed to go through firewall // where URL are fetched through a proxy on some weird port, // so back up with URLConnection just in case... // // Browser don't even know about your Socket connexion, so // there is no way it can cache the file... public class No_browser_cache { public static void main(String argv[]) { Socket s = null; try { StringBuffer document = new StringBuffer(); s = new Socket(argv[0],80); DataInputStream server_in = new DataInputStream(s.getInputStream()); // DataInputStream.readLine() is deprecated, use: // BufferedReader server_in = // new BufferedReader(new InputStreamReader(s.getInputStream())); // if you want but it only works in jdk1.1 PrintStream server_out = new PrintStream(s.getOutputStream()); server_out.println("GET /"+argv[1]+" HTTP/1.0"+"\n"); int i=0; String line; for (i=0; i<100; i++) { if ( server_in.available() != 0 ) { i = 0; line = server_in.readLine(); document.append(line+"\n"); // if (line.toUpperCase().indexOf("")!= -1) // or what ever break; // your file } // terminator is else Thread.sleep(500); // that makes a 50 seconds time out // (100*500ms) check for data every half second } System.out.println(document.toString()); } catch(Exception e) { // put your regular URLConnexion code in here... System.out.println(); System.out.println ("Did not work, should put conventionnal URLConnection code here,"); System.out.println ("or find out how to talk to the proxy... ;-)"); System.out.println(); System.out.println ("Not using a proxy???"); System.out.println(); System.out.println ("*** Of course you have to type the parameters right at the command"); System.out.println ("line or you will see this message also, all exeptions are catched here."); System.out.println(); System.out.println ("Usage: java No_browser_cache "); System.out.println(); } finally { try // make sure socket is closed whatever happends { if (s != null) { s.close() ; System.out.println("Socket closed, everything went fine"); } } catch (Exception e) { } } } // end main } // end class