|
|
TheReverseTestexample has three problems.First, the
ReverseTestexample uses theDataInputStream.readLinemethod which has been deprecated in the JDK 1.1 because it does not convert correctly between bytes and characters. Most programs that useDataInputStream.readLinecan make a simple change to use the same method from the newBufferedReaderclass instead. Simply replace code of the form:with:DataInputStream d = new DataInputStream(in);BufferedReader d = new BufferedReader(new InputStreamReader(in));ReverseTestis one of those programs which can make this simple change.Second,
ReverseTestexplicitly creates aPrintStreamfor writing to theURLConnection. Using aPrintStreamhas been deprecated in favor ofPrintWriter.And finally,
ReverseTestmust call the newsetDoOutputon its URLConnection to be able to write to the connection.So, here is the new version of
ReverseTestthat fixes all of these problems:import java.io.*; import java.net.*; public class ReverseTest { public static void main(String[] args) { try { if (args.length != 1) { System.err.println("Usage: java ReverseTest string_to_reverse"); System.exit(1); } String stringToReverse = URLEncoder.encode(args[0]); URL url = new URL("http://java.sun.com/cgi-bin/backwards"); URLConnection connection = url.openConnection(); connection.setDoOutput(true); PrintWriter writer = new PrintWriter(connection.getOutputStream()); writer.println("string=" + stringToReverse); writer.close(); BufferedReader reader = new BufferedReader( new InputStreamReader(connection.getInputStream())); String inputLine; while ((inputLine = reader.readLine()) != null) { System.out.println(inputLine); } reader.close(); } catch (MalformedURLException me) { System.err.println("MalformedURLException: " + me); } catch (IOException ioe) { System.err.println("IOException: " + ioe); } } }
|
|