Uniform Resource Locator URL (Uniform Resource Locator) is the name and address used by www clients to identify resources when accessing the Internet. Hypertext links are maintained by uniform resource locator URLs. The format of the URL is:
<METHOD>://<HOSTNAME:PORT>/<PATH>/<FILE>
Among them: Method is the transmission protocol: HOSTNAME is the Internet host name where the document and server are located (the point address in DNS in the domain name system); PORT is the service port number (can be omitted); PATH is the path name, and FILE is the file name. For example:
http://www.weixueyuan.net/(http is the protocol name, www.weixueyuan.net is the host name)
http://www.weixueyuan.net/view/6079.html (www.weixueyuan.net is the host name, view/6079.html is the file path and file name)
URL class
The Java.net package has a URL class, a URL object can represent a network resource. Programs can use URL objects to implement Internet addressing, location connections of network resources, direct access between clients and servers, etc. The constructor method of URL class is
URL(String s)
Among them, s points to a resource in the network.
The method to use URL objects to access online resources is to first create a URL object, as shown in the following code:
URL myURL; try { myURL = new URL("http://www.weixueyuan.net:80/");}catch(MalformedURLException e){ System.out.println("Wrong URL:"+url+e );}
MalformedURLException may occur due to creating a URL object. Therefore, the code that creates the URL object should appear in the try...catch statement block so that URL error exceptions can be caught.
URLConnection class
To receive and send information, the URLConnection class must be used. The program obtains a URLConnection object, which is equivalent to completing an HTTP connection to the specified URL. The following is the code to obtain the URLConnection object.
URL mu = new URL("http://www.sun.com/");//First create a URL object URLConnection muC = mu.openConnection();//Get the URLConnection object
The above code illustrates that you must first create a URL object, and then use the openConnection() method of the URL object to obtain a URLConnection object from the system. Once the program has a URLConnection object, it can use the following methods provided by the URLConnection class to obtain the stream object and implement a network connection:
getOutputStream(): Obtain the OutputStream stream object that sends information to the remote host;
getInputStream(): Obtains the InputStream stream object that obtains information from the remote host. With input and output streams connected to the network, programs can communicate remotely;
connect(): Set up the network connection.
Sending and receiving information
To send and receive information, a stream object is obtained, and an input or output data stream object is created from the stream object. Then, you can use the streaming method to access online resources.
See the method readByURL() in the example program below. This method illustrates the process of reading web content from a known URL. The method uses the URL parameters to create a URL object url, then uses the openConnect() method of the object url to obtain the URLConnection object tc, uses the connect() method of the object tc to establish a network connection, and then obtains the InputStreamReader class object in of the network connection, and puts the object in , converted into BufferedRead object dis, changed to buffered input. Finally, use the readLine() method of the object dis to complete reading the network text data.
Just like the local data flow, the data flow should be closed in time after the use of online resources is completed. For example, the code
dis.close();
Close the stream dis created by the previous code.
[Example] An application that reads web content using a data stream method. When the program runs, the URL is read from the text box.
import java.net.*;import java.awt.*;import java.awt.event.*;import java.io.*;import java.javax.swing.*;public class Example10_2{ public static void main(String args []){ new downNetFile(); }}class DownNetFile extends JFrame implements ActionListener{ JTextFileld infield = new JTextField(30); JTextarea showArea = new JTextArea(); JButton b = new JButton("download"); JPanel p = new JPanel(); DownNetFile(){ super("read network text file application"); Container con = this.getContentPane(); p.add(infield);p.add(b); JScrollPane jsp = new JScrollPane(showArea); b.addActionListener(this); con.add(p,"North");con.add(jsp,"Center"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(500,400);setVisible(true) ; } public void actionPerformed(ActionEvent e){ readByURL(infield.getText()); } public void readByURL(String urlName){ try{ URL url = new URL(urlName);//Create a URL object from the URL URLConnection tc = url.openConnectin();//Get the URLConnection object tc.connect();//Set the network connection InptStreamReader in = new InputStreamReader(tc.getInputStream()); BufferedReader dis = new BufferedReader(in);//Use buffered input String inline; while((inline = dis.readLine())!=null){ showArea.append(inline +”/n”); } dis.close ();//After the use of online resources is completed, the data stream is closed in time}catch(MalformedURLException e){ e.printStackTrace(); } catch(IOException e){e.printStacktrace();} /*Accessing online resources may generate MalformedURLException and IOException*/ }}