Java Reference
In-Depth Information
try
{
URL url = new URL(args[0]);
System.out.println(“URL is “ + url.toString());
System.out.println(“protocol is “
+ url.getProtocol());
System.out.println(“authority is “
+ url.getAuthority());
System.out.println(“file name is “ + url.getFile());
System.out.println(“host is “ + url.getHost());
System.out.println(“path is “ + url.getPath());
System.out.println(“port is “ + url.getPort());
System.out.println(“default port is “
+ url.getDefaultPort());
System.out.println(“query is “ + url.getQuery());
System.out.println(“ref is “ + url.getRef());
}catch(IOException e)
{
e.printStackTrace();
}
}
}
URL Connections
Using the openConnection() method of the URL class, you can connect to a
URL and communicate with the resource. The openConnection() method
returns a java.net.URLConnection, an abstract class whose subclasses repre-
sent the various types of URL connections. For example, if you connect to a
URL whose protocol is HTTP, the openConnection() method returns an
HttpURLConnection object. If you connect to a URL that represents a JAR file,
the openConnection() method returns a JarURLConnection object.
The URLConnection class has many methods for setting or determining
information about the connection, including the following:
public void setDoInput(boolean input). Passes in true to denote that the
connection will be used for input. The default value is true because
clients typically read from a URLConnection.
public void setDoOutput(boolean output). Passes in true to denote that
the connection will be used for output. The default value is false because
many types of URLs do not support being written to.
public InputStream getInputStream() throws IOException. Returns the
input stream of the URL connection for reading from the resource.
Search WWH ::




Custom Search