Java know how much (104) the Uniform Resource Locator URL for network programming

Source: Internet
Author: User

The Uniform Resource Locator URL (Uniform Resource Locator) is the name and address used to identify a resource when the WWW client accesses the Internet. Hypertext Link Routing Uniform Resource Locator URL maintained. The format of the URL is:

    <method>://<HOSTNAME:PORT>/<PATH>/<FILE>

Where: Method is the transport protocol: hostname is the Internet host name of the document and server (the point address in DNS in the domain Name System); Port is the service port number (can be omitted); path is the pathname and file is the file name. For example:

1     http://www.weixueyuan.net/(HTTP is protocol name, Www.weixueyuan.net is host name)2     http:// www.weixueyuan.net/view/6079.html (Www.weixueyuan.net is the host name, view/6079.html is the 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 can use URL object to realize Internet addressing, locating and connecting network resources, and direct access between client and server. The URL class is constructed by
URL (String s)
where s indicates a resource in the network.

The URL object is used to access network resources by first creating the URL object, as shown in the following code:

1 URL Myurl; 2 Try {3     New URL ("http://www.weixueyuan.net:80/"); 4 }catch(malformedurlexception e) {5     System.out.println ("Error URL: "+url+e); 6 }

Creating a URL object may produce a malformedurlexception exception. Therefore, the code that creates the URL object should appear in the Try...catch statement block so that the URL error exception can be caught.

URLConnection class

To receive and send off information also use the URLConnection class, the program obtains a URLConnection object, equivalent to complete an HTTP connection to the specified URL. The following is the code that motioned to get the URLConnection object.

    New URL ("http://www.sun.com/"); // to create a URL object first    URLConnection MuC = Mu.openconnection (); // get URLConnection Object

The code above shows that you first create a URL object and then use the OpenConnection () method of the URL object to get a URLConnection object from the system. Once the program has a URLConnection object, you can use the following methods provided by the URLConnection class to get the stream object and implement the network connection:

    1. Getoutputstream (): Obtains the OutputStream stream object which sends the information to the remote host;
    2. getInputStream (): Gets the InputStream stream object that obtains information from the remote host. With the input and output stream of the network connection, the program can realize the remote communication;
    3. Connect (): Set up a network connection.
Sending and receiving of information

Send and receive information to get the stream object and create the input or output data stream object from the stream object. Then, you can access the online resources in a streaming way.

See Method Readbyurl () in the "Example 13-2" program, which describes the process of reading Web page content by a known URL. The 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, and uses the Connect () method of the object TC to establish the network connection. Then get the network connection of the InputStreamReader class object in, the object in, into the Bufferedread object dis, changed to buffered input. Finally, the ReadLine () method of the object dis is used to complete the reading of the network text data.

As with the local data flow, the data flow should be closed in time after the online resource is used. For example, the code
Dis.close ();
Closes the stream dis established by the previous code.

Example 13-2 an application that reads Web page content as a data flow method. When the program runs, the URL is read from the text box.

1 Importjava.net.*;2 Importjava.awt.*;3 Importjava.awt.event.*;4 ImportJava.io.*;5 Importjava.javax.swing.*;6  Public classexample10_2{7      Public Static voidMain (String args[]) {8         Newdownnetfile ();9     }Ten } One classDownnetfileextendsJFrameImplementsactionlistener{ AJtextfileld infield =NewJTextField (30); -JTextArea Showarea =NewJTextArea (); -JButton B =NewJButton ("Download"); JPanel p =NewJPanel (); the Downnetfile () { -         Super("Read network text file application"); -Container con = This. Getcontentpane (); - P.add (Infield);p. Add (b); +JScrollPane jsp =NewJScrollPane (Showarea); -B.addactionlistener ( This); + Con.add (P, "North"); Con.add (JSP, "Center"); A setdefaultcloseoperation (jframe.exit_on_close); atSetSize (500,400); setvisible (true); -     } -      Public voidactionperformed (ActionEvent e) { - Readbyurl (Infield.gettext ()); -     } -      Public voidReadbyurl (String urlname) { in         Try{ -URL url =NewURL (urlname);//URL object created by URL toURLConnection TC = Url.openconnectin ();//get URLConnection Object +Tc.connect ();//set up a network connection -Inptstreamreader in =NewInputStreamReader (Tc.getinputstream ()); theBufferedReader dis =NewBufferedReader (in);//with buffered input * String Inline; $              while(inline = Dis.readline ())! =NULL){Panax NotoginsengShowarea.append (inline +"\ n"); -             } theDis.close ();//data flow shuts down immediately after online resource usage is completed +}Catch(malformedurlexception e) { A e.printstacktrace (); the        } +         Catch(IOException e) {e.printstacktrace ();} -         /*access to online resources may produce malformedurlexception and IOException exceptions*/ $     } $}

Series Articles:

Java know how much (top)Java know how much (medium)Java knows how many () Java vectors (vector) and their applicationsJava know how much (79) hash table and its applicationJava know how much (80) graphical Interface design basicsJava know how much (81) frame window BasicsJava know how much (82) Introduction to tags, buttons, and button eventsJava know how much (83) Panel Basics: JPanel and JScrollPaneJava know how much (84) layout design of graphical interfaceJava know how much (85) text box and text areaJava know how much (86) input and output of text box and text areaJava know how much (87) Select boxes and radio buttonsJava know how many (88) lists and combo boxesJava know how many (89) lists and combo boxesJava know how much (90) menuJava know how much (91) dialog boxJava know how much (92) scroll barJava know how much (93) mouse EventsJava know how much (94) keyboard EventsJava know how much (95) Drawing BasicsJava know how much (96) set the font and color of the drawingJava know how much (97) Drawing Mode OverviewHow much Java knows (98) the drawing method of the Graphics classJava know how much (graphics2d) the drawing method of the classJava know how much (100) Image processing BasicsJava know how much (101) image buffering TechnologyJava know how much (102) Multimedia FoundationJava know how much (103) network programming IP address and InetAddress class

Java know how much (104) the Uniform Resource Locator URL for network programming

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.