Details the use of uniform resource locator urls in Java programming

  • 2020-04-01 04:14:46
  • OfStack

Uniform Resource Locator (Uniform Resource Locator) is the name and address of a WWW client that identifies a Resource when it accesses the Internet. Hypertext links are maintained by the uniform resource locator URL. The format of the URL is:
      < METHOD> : / / < The HOSTNAME: PORT> / < PATH> / < FILE>
Where: Method is the transport protocol: HOSTNAME is the Internet HOSTNAME where the document and server are located (the point address in DNS in the domain name system); PORT is the service PORT number (omitted); PATH is the PATH name and FILE is the FILE name. Such as:
      http://www.weixueyuan.net/ (HTTP protocol, www.weixueyuan.net is the hostname)
      http://www.weixueyuan.net/view/6079.html, www.weixueyuan.net is a host name, the view / 6079 HTML file path and file name)
The URL class

The Java.net package has a URL class, where a URL object can represent a network resource. The program USES the URL object to achieve Internet addressing, network resources location connection, direct access between the client and the server. The URL class is constructed by


  URL(String s)


Where, s refers to a resource in the network.

The way to access online resources with URL objects is to create a URL object first, as shown in the following code:


URL myURL;
try {
  myURL = new URL( " http://www.weixueyuan.net:80/ " );
}catch(MalformedURLException e){
  System.out.println( "There are wrong URL: " +url+e);
}

A MalformedURLException may result from the creation of a URL object. So, the code that creates the URL object should appear in the try... In the catch block so that you can catch the url error exception.
URLConnection class

To receive and send the information, the URLConnection class is used, and the program acquires a URLConnection object, which completes an HTTP connection to the specified URL. The following is the code that indicates the acquisition of the URLConnection object.


  URL mu = new URL( " http://www.sun.com/ " );//First you create a URL object
  URLConnection muC = mu.openConnection();// To obtain URLConnection object 


The code above explains that you first create a URL object and then obtain a URLConnection object from the system using the URL object's openConnection() method. After the program has the URLConnection object, it can use the following methods provided by the URLConnection class to obtain the flow object and realize the network connection:
GetOutputStream () : gets the OutputStream object that sends information to the remote host;
GetInputStream () : gets the InputStream object that gets the information from the remote host. With the network connection of the input and output streams, the program can achieve remote communication;
Connect () : sets up the network connection.
The sending and receiving of information

The information is sent and received to obtain the stream object, and the input or output data stream object is created by the stream object. Then, you can access online resources in a streaming way.

See the method readByURL() in the example program below, which illustrates how a given url reads the contents of a web page. Method USES the URL parameter 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, converts the object in into a BufferedRead object dis, and changes to BufferedRead input. Finally, the readLine() method of the object dis completes the reading of the network text data.

Just like the local data stream, the data stream should be shut down when the online resource is used. For example, code


  dis.close();


Close the flow dis created by the previous code.

Application that reads the contents of a web page by means of a data stream. As 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);//The URL object is created from the URL
      URLConnection tc = url.openConnectin();//Gets the URLConnection object
      tc.connect();//Set up 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 end of the use of online resources, the data stream is shut down in time
    }catch(MalformedURLException e){
      e.printStackTrace();
    }
    catch(IOException e){e.printStacktrace();}
    
  }
}


Related articles: