The relevant use of Uniform Resource Locator URL in Java programming _java

Source: Internet
Author: User
Tags readline

The Uniform Resource Locator URL (uniform Resource Locator) is the name and address that the WWW client uses to identify the resource when it accesses the Internet. Hypertext Link Routing Uniform Resource Locator URL maintenance. The format of the URL is:
<METHOD>://<HOSTNAME:PORT>/<PATH>/<FILE>
Where: Method is the transport protocol: the hostname is the Internet host name (the point address in DNS in the domain Name System) where the document and the server are located; Port is the service port number (can be omitted); path is the pathname and file is the filename. 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 host name, view/6079.html is file path and filename)
URL class

The java.net package has a URL class, and a URL object can represent a network resource. The program utilizes URL objects to achieve Internet addressing, locating connections to network resources, and direct access between client and server. The method of constructing a URL class is

  URL (String s)


Wherein, s indicates a resource in the network.

The way to access resources on the Web using a URL object is to create the 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 ("Wrong URL:" +url+e);
}

A malformedurlexception exception may be generated by 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 the information and use the URLConnection class, the program obtains a URLConnection object, equivalent to completing an HTTP connection to the specified URL. Here's the code to get the URLConnection object.

  URL mu = new URL ("http://www.sun.com/")//First create a URL object
  urlconnection MuC = mu.openconnection ()//Get URLConnection Object


The above code explains that you first create a URL object and then use the OpenConnection () method of the URL object to obtain a URLConnection object from the system. After the program has a URLConnection object, you can use the following methods provided by the URLConnection class to obtain the stream object and implement the network connection:
Getoutputstream (): Obtains the OutputStream stream object which sends the information to the remote host;
getInputStream (): Gets the InputStream stream object that obtains information from the remote host. With the input and output stream of network connection, the program can realize remote communication;
Connect (): Set up a network connection.
Sending and receiving of information

Send and receive information to obtain a stream object and create an input or output data stream object from the stream object. Then, you can use the streaming method to access resources on the Web.

Refer to the Method Readbyurl () in the following example program, which shows the process of reading Web page content by a known URL. Methods using URL parameters to create a URL object URL, and then using the object URL Openconnect () method to obtain URLConnection object TC, with the object TC Connect () method to establish network connection, Then obtains the network connection the InputStreamReader class object in, converts the object in, transforms into the Bufferedread object dis, changes to the buffer type input. Finally, the ReadLine () method of object dis is used to read the network text data.

As with the local data stream, after the use of online resources, the data stream should be closed in time. For example, the code

  Dis.close ();


Closes the stream dis established by the previous code.

"Example" an application that reads the content of a Web page as a data flow method. When the program is running, 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 = new Infield (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 ());  The public void Readbyurl (String urlname) {try{URL url = new URL (urlname);//URL object created by urls urlconnection TC = Url.openconnectin ()//obtainedURLConnection Object Tc.connect ()//Set network connection Inptstreamreader in = new InputStreamReader (Tc.getinputstream ());
      BufferedReader dis = new BufferedReader (in);//a buffer type String inline is used;
      while ((inline = Dis.readline ())!=null) {showarea.append (inline + "\ n");
    Dis.close ();//online Resource usage ends, data flow closes in time}catch (Malformedurlexception e) {e.printstacktrace ();
    catch (IOException e) {e.printstacktrace ();} * * Access to the Internet resources may produce malformedurlexception and IOException exception *}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.