//Compile it by // javac Validator.java // //Modify INPUT_HTML_FILE and run it by // java Validator INPUT_HTML_FILE // //Then the program will send the INPUT_HTML_FILE to the W3C validator //(http://validator.w3.org/check) and print out the output. import java.io.*; import java.net.*; class Validator { static final String link = "http://validator.w3.org/check"; public static void main(String [] args) throws Exception { //create http connection with multipart/form-data Content-Type //i.e. simulating "Validate by File Upload" String boundary = "AaB03x", eol = "\r\n", s; HttpURLConnection httpC = (HttpURLConnection)new URL(link).openConnection(); httpC.setDoOutput(true); httpC.setDoInput(true); httpC.setUseCaches(false); httpC.setRequestMethod("POST"); httpC.setRequestProperty("Connection", "Keep-Alive"); httpC.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); httpC.setRequestProperty("Content-Length", "xxx"); //send the html file to w3c DataOutputStream out = new DataOutputStream(httpC.getOutputStream()); s = "--" + boundary + eol + "Content-Disposition: form-data; name=\"ss\"" + eol + "Content-Type: text/html; charset=ISO-8859-1" + eol + eol + 1 + eol + "--" + boundary + eol + "Content-Disposition: form-data; name=\"uploaded_file\"; filename=\"" + args[0] + "\"" + eol + "Content-Type: text/html; charset=ISO-8859-1" + eol + eol; out.writeBytes(s); BufferedReader in = new BufferedReader(new FileReader(args[0])); for(; (s = in.readLine()) != null; ) out.writeBytes(s + "\n"); in.close(); out.writeBytes( eol + "--" + boundary + "--" + eol); out.flush(); out.close(); //read and print the results from w3c in = new BufferedReader(new InputStreamReader(httpC.getInputStream())); for(; (s = in.readLine()) != null; ) System.out.println(s); in.close(); } }