Working with URLs |
If you've successfully usedopenConnection()
to initiate communications with a URL, then you have a reference to a URLConnection object. The URLConnection class contains many methods that let you communicate with the URL over the network. URLConnection is an HTTP-centric class--many of its methods are useful only when working with HTTP URLs. However, most URL protocols let you read from and write to the connection so this page shows you how to both read from and write to a URL through a URLConnection object.Reading from a URLConnection
The following program performs the same function as the program shown inThese lines ensure that the user provides one and only one command line argument to the program and "encodes" it. The command line argument is the string to be reversed by the cgi-bin scriptbackwards
. The command line argument may have spaces or other "weird" characters in it. Those characters must be "encoded" because various processing may happen on the string on its way to the server. This is achieved by the URLEncoder class. There's more about the URLEncoder class and what theencode()
method does in The URLEncoder Helper Class.Next the program creates the URL object--the URL for the
backwards
script on www.javasoft.com.Next, the program creates a URLConnectiong and then opens an output stream on that connection. The output stream is filtered throught a PrintStream.URL url = new URL("http://www.javasoft.com/cgi-bin/backwards");The second line calls theURLConnection connection = url.openConnection(); PrintStream outStream = new PrintStream(connection.getOutputStream());getOutputStream()
method on the connection. If the URL does not support output, this method will throw a UnknownServiceException. If the URL supports output, then this method returns an output stream which is connected to the standard input stream of the URL on the server side--the client's output is the server's input.[PENDING: picture]
Next, the program writes the required information to the output stream and closes the stream:
This line writes to the output stream using theoutStream.println("string=" + stringToReverse); outStream.close();println()
method. So you can see, writing data to a URL is as easy as writing data to a stream. The data written to the output stream on the client-side is the input for thebackwards
script on the server-side. The ReverseTest program constructs the input in the form required by the script by concatenatingstring=
to the encoded string to be reversed.Often, as with this example, when you are writing to a URL you are passing information to a cgi-bin script which reads the information you write, performs some action and then sends information back to you via the same URL. So it's likely that you will want to read from the URL after you've written to it. The ReverseTest program does that next:
When you run the ReverseTest program usinginStream = new DataInputStream(connection.getInputStream()); while (null != (inputLine = inStream.readLine())) { System.out.println(inputLine); } inStream.close();Reverse Me
as an argument, you should see this output:Like previous examples in this section, you may set the proxy host when you run the program so that it can find the www.javasoft.com server. You can set the proxy host through the command line when you run the program. In addition to setting the proxy host you must also provide a string on the command line for the program to reverse:Reverse Me reversed is: eM esreveRjava -DproxySet=true -DproxyHost=proxyhost ReverseTest "Reverse Me"
Working with URLs |