2. URLs

2.1.1. Definition

URL is an acronym that stands for Uniform Resource Locator and is a reference (an address) to a resource on the Internet.

2.1.2. Components

  • Protocol identifier

    • HTTP - http://www.unicamp.br:80/lecture.html#TITLE

    • FTP - ftp://ftp.fee.unicamp.br/linux/Read.me

    • JDBC - jdbc:msql://lcaee.fee.unicamp.br:4333/group1

  • Resource Name

    • www.unicamp.br:80/lecture.html#TITLE

    • ftp.fee.unicamp.br/linux/Read.me

    • msql://lcaee.fee.unicamp.br:4333/group1

2.1.3. Resource name format (protocol dependent):

2.1.3.1. host name

www.unicamp.br

the name of the machine the resource lives on.

2.1.3.2. filename

/lecture.html

the pathname to the file on the machine.

2.1.3.3. port number

:80

the port number to connect to (this is typically optional).

2.1.3.4. reference

#TITLE

a reference to a named anchor within a resource.

Audio in Portuguese

Simplest way:

http://www.gamelan.com/
URL gamelan = new URL("http://www.gamelan.com/");

Creating a URL Relative to Another:

URL gamelan = new URL("http://www.gamelan.com/");
URL gamelanNetwork = new URL(gamelan, "Gamelan.network.html");
URL gamelanNetworkBottom = new URL(gamelanNetwork, "#BOTTOM");

getProtocol() - Returns the protocol identifier component of the URL;

getHost()- Returns the host name component of the URL.;

getPort() - Returns the port number component of the URL;

getFile() - Returns the filename component of the URL;

getRef() - Returns the reference component of the URL.

[Note]Note

Remember that not all URL addresses contain these components.

class OpenStreamTest {
     public static void main(String[] args) {
        try {
           URL yahoo = new URL("http://www.yahoo.com/");
           DataInputStream dis = new DataInputStream(yahoo.openStream());
           String inputLine;

           while ((inputLine = dis.readLine()) != null) {
              System.out.println(inputLine);
           }
           dis.close();
        } catch (Exception me) {
           System.out.println("Exception: " + me);
        }
     }
}

Audio in Portuguese

[Note]Note

Note: URLConnection is an HTTP-centric class--many of its methods are useful only when working with HTTP URLs.

try {
     URL yahoo = new URL("http://www.yahoo.com/");
     yahoo.openConnection();
  } catch (MalformedURLException e) { // new URL() failed
     . . .
  } catch (IOException e) { // openConnection() failed
     . . .
}

2.6.1. Reading from a URLConnection

import java.net.*;
import java.io.*;

class ConnectionTest {
    public static void main(String[] args) {
        try {
            URL yahoo = new URL("http://www.yahoo.com/");
            URLConnection yahooConnection = yahoo.openConnection();
            DataInputStream dis = new DataInputStream(yahooConnection.getInputStream());
            String inputLine;

            while ((inputLine = dis.readLine()) != null) {
                System.out.println(inputLine);
            }
            dis.close();
        } catch (MalformedURLException me) {
            System.out.println("MalformedURLException: " + me);
        } catch (IOException ioe) {
            System.out.println("IOException: " + ioe);
        }
    }
}

Audio in Portuguese

2.7. Writing to a URLConnection (cgi-bin script)

  1. Create a URL.

  2. Open a connection to the URL.

  3. Get an output stream from the connection. This output stream is connected to the standard input stream of the cgi-bin script on the server.

  4. Write to the output stream.

  5. Close the output stream.

2.7.1. Program

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();

            PrintStream outStream = new PrintStream(connection.getOutputStream());
            outStream.println("string=" + stringToReverse);
            outStream.close();

            DataInputStream inStream = new DataInputStream(connection.getInputStream());
            String inputLine;

            while ((inputLine = inStream.readLine()) != null) {
                System.out.println(inputLine);
            }
            inStream.close();
        } catch (MalformedURLException me) {
            System.err.println("MalformedURLException: " + me);
        } catch (IOException ioe) {
            System.err.println("IOException: " + ioe);
        }
    }
}

2.7.2. Output

Reverse Me
 reversed is:
eM esreveR

Audio in Portuguese