1 the URL class in Java2 the Java Network class allows you to implement your application over a network or remote connection. Moreover, the platform is now accessible to the Internet as well as to URL resources. The Java URL class allows access to network resources just as quickly and easily as accessing your local folder. We can use the Java URL class to complete the operation of reading and modifying data via a URL. 3 4 with a URL connection, we can determine the location of the resource, such as network files, Web pages, and Web applications. It contains a number of grammatical elements. 5 The data obtained from the URL can be varied, requiring a unified mechanism for reading and modifying the URL. The Java language provides such a mechanism in its java.net software package. 6 7 the URL class is extracted from the URL identifier. It allows the Java Program designer to open a specific URL connection and read and write the data inside and read and write the header information. It also allows programmers to do other things about the URL. 8 9 ConstructionTen One When you create an instance of the Java.net URL, you can take advantage of many public constructors to make it more flexible. For example, this class provides a constructor that uses a complete URL string, a constructor that breaks down URL information strings into protocols, hostnames, and files and resources, and a constructor that breaks down URL information strings into protocols, hostnames, port numbers, and files. We first use the full URL to create an example of the URL class: A -URL Aurl =NewURL ("http://www.mycompany.com:8080/index.html "); - theIn this example, we create a URL that uses the full URLclass, which explicitly states that the protocol used is HTTP, the host name is www.mycompany.com, the port number is 8080, the file/the resource is index.html. If there is an error in the syntax that makes up the URL, the constructor emits malformedurlexception. - - Connection - Once you have successfully created an instance of the URL class, you can manipulate it. But before you can access the resources and content on this URL, you have to open the connection to those resources and content. You can do this by using OpenConnection. + - using openconnection does not require parameters, and after the operation succeeds, it returns an instance of the URLConnection class. In listing A, we demonstrated the process of opening a connection to a URL. Once your connection is successful, you can begin to read and write the input and output streams of this urlconnection. + reading data from a URL connection A at using the Java.io stream class to read data from a URL is a very simple process. Once you have established a successful connection, you can get the input stream for the connection and begin the write operation. Fortunately, java.io classes can operate on the data returned from the URLConnection stream in the same way that it operates on a file stream or a socket stream. - to write to a URL - -Using java.io stream classes to write to a URL is also very simple. Once you have established a successful connection, you can get the output stream from this connection and begin the write operation. Of course, it makes sense only to write to the data that the customer wants. Again, before you get and write to the URLConnection stream, you need to use Setdooutput (Boolean) mode to set the Output property to True (true) To specify the connections that can be written. java.io classes allows you to write data to the URLConnection stream, which is the same as you write to the file stream and the socket stream. - Other Operations - inYou can get other types of information from URLs and URLConnection object connections, such as hostname, port, content length, content encoding, and the type of content. Put these methods together with stream i/O classes together allows you to build complex and effective Web client applications and services. - easy access to the network to + the URL class provided by the Java platform allows us to easily and efficiently access resources on the network, and it makes us feel relaxed and happy as if we were accessing local files. Instead of worrying about the details of network communication, we need to focus on making useful applications and services. - the three methods of connection: * $ Copy CodePanax Notoginseng //Method One -URL url =NewURL ("http://www.sina.com.cn"); theURLConnection Urlcon =url.openconnection (); +InputStream is =Urlcon.getinputstream (); A the //Method Two +URL url =NewURL ("http://www.yhfund.com.cn"); -HttpURLConnection Urlcon =(HttpURLConnection) url.openconnection (); $InputStream is =Urlcon.getinputstream (); $ - //Method Three -URL url =NewURL ("http://www.yhfund.com.cn"); theInputStream is =Url.openstream (); - Copy CodeWuyi Specific examples: the - Copy Code Wu LongBeginTime =System.currenttimemillis (); - AboutURL url =NewURL ("http://www.yhfund.com.cn"); $HttpURLConnection Urlcon =(HttpURLConnection) url.openconnection (); -Urlcon.connect ();//Get Connections -InputStream is =Urlcon.getinputstream (); -BufferedReader buffer =NewBufferedReader (NewInputStreamReader (IS)); AStringBuffer bs =NewStringBuffer (); +String L =NULL; the while((L=buffer.readline ())! =NULL){ -Bs.append (L). Append ("/n"); $ } the System.out.println (bs.tostring ()); the the //System.out.println ("Content-encode:" +urlcon.getcontentencoding ()); the //System.out.println ("Content-length:" +urlcon.getcontentlength ()); - //System.out.println ("Content-type:" +urlcon.getcontenttype ()); in //System.out.println ("Date:" +urlcon.getdate ()); the theSYSTEM.OUT.PRINTLN ("Total Execution Time:" + (System.currenttimemillis ()-begintime) + "MS"); About}Catch(IOException e) { the System.out.println (e); the } the } +}
The URL class in Java