URL is an acronym that stands for Uniform Resource Locator and is a reference (an address) to a resource on the Internet.
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
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 |
|---|---|
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);
}
}
}![]() | 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
. . .
}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);
}
}
}Create a URL.
Open a connection to the URL.
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.
Write to the output stream.
Close the output stream.
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);
}
}
}Copyright © 1998-2009 Dilvan Moreira