Documente Academic
Documente Profesional
Documente Cultură
Objectives
TO INTRODUCE: IP addresses and Domain Name System (DNS) InetAddress class and associated methods Uniform Resource Identifier (URI), Uniform Resource Name (URN), Uniform Resource Locator (URL) Java URL class and associated methods
IP Addresses-IPv4
Computer locations on a IP network (including the internet) are given by an IP address
Continued
IP Addresses-IPv6
In IPv6, addresses are 128-bit numbers
We can now represent 2128 unique IP addresses Consists of 8 colon-separated 16-bit segments usually written as 8 blocks of 4 hexadecimal colon-separated digits Example the address for www.ipv6.com.cn is 2001:0250:02FF:0210:025:8BFF:FEDE:67C8 Leading zeros do not need to be written and a double colon (::) one of which may appear in any address could replace multiple zero blocks. Example FEDC:0000:0000:0000:00DC:0000:7076:0010 could be written as FEDC::DC:0:7076:10 In mixed networks of IPv6 and IPv4 the last four bytes of the IPv6 address can be written as an IPv4 dotted quad address. Example FEDC:BA98:7654:3210:FEDC:BA98:7654:3210 could be written as FEDC:BA98:7654:3210:FEDC:BA98:118.84.50.16
Alternatively we can just use the println method to print the entire address object via its toString() method
System.out.println(address); swin.edu.au/ 136.186.1.10 //displays something like
Schemes (protocols) identify the way the resource can be accessed & retrieved; eg. http, ftp, rmi
Authoriy
User info may include colon separated username and password credential of the client Host name identifies the host on which the resource resides Port is sometimes optional because most well known schemes have a
default port, eg. http 80 Path is a series of directory (folder) names, ending in a filename. The path doesnt need to represent a real file; it might be an object or operation name Query strings are heavily used in dynamic web systems to encode variables sent from the client, using the format parameter1=value1¶meter2=value2 Fragments are pointers to a part of a document or a file
Two paths may resolve to same file: http://www.swin.edu.au/staff/index.html http:// www.swin.edu.au/contact/../staff/index.html An RMI object registered/advertised under two names: rmi://localhost/Obj1 [Naming.rebind(Obj1, server)] rmi://localhost/Obj2 [Naming.rebind(Obj2, server)]
It extends java.lang.Object and implements the java.io.Serializable interface and is a final class that cannot be subclassed thus:
public final class URL extends Object implements Serializable (final): mean you cannot extend this class when define its final.
The URL class is the simplest way for a Java program to locate and retrieve data from network
No need to worry about the details of protocol being used, format of data being retrieved or how to communicate with the server
You simply tell Java the URL and it gets the data for you
Standard Java can only handle a limited number of protocols and content types, however, this capability can be extended to handle more protocols and types of data
Field values can be retrieved using their respective get methods (getProtocol(), getHost(), getPort() etc.)
There are six constructors that can be used differing in information required though they all throw MalformedURLException if you try to form a URL with an unsupported protocol and may throw the same exception if the URL is syntactically incorrect
The methods to use include: getProtocol(), getFile(), getHost, getPort(), getPath(), getRef(), getAuthority(), getUserInfo(), getQuery()
import java.net.*;
The URL class has several methods designed to retrieve data from a location pointed to by a URL
public InputStream openStream( ) throws IOException public URLConnection openConnection( ) throws IOException public URLConnection openConnection(Proxy proxy) throws IOException public Object getContent( ) throws IOException public Object getContent(Class[] classes) throws IOException
These methods return the data found at the URL as an instance of different classes
...
int c;
while ((c = in.read( )) != -1) System.out.write(c); } catch (IOException ex) { System.err.println(ex); }