An Internet URL consists of the following fields:
protocol://host:port/filename#referenceThe class URL has a getXxx() method to retrieve any of these fields without the need for string parsing.
It is possible to read from a URL object directly. But a lot more flexibility and leverage are available by calling the URL object's openConnection() method, and then reading from or writing to the URLConnection object that is returned.
The top example shows how to open a connection to a URL address and then capture the contents of the file represented by the URL. The bottom example opens a connection to a CGI script hosted on a server at Sun that is waiting for input from the Internet. The program supplies that input by writing to the output stream of the URLConnection object. It then retrieves the response of the CGI script by reading from the URLConnection object's input stream.
The bottom example references the URLEncoder class. This class contains a single static method for converting a String into a MIME format called "x-www-form-urlencoded" format. The following rules are applied: