Java URL class

Source: Internet
Author: User

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.

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.
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.

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.

  

Structure

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:

URL aurl = new URL ("http://www.mycompany.com:8080/index.html");

In this example, we created a URL class that uses the full URL, which explicitly states that the protocol used is HTTP, the host name is www.mycompany.com, the port number is 8080, and the file/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.

from URL reading data in a connection

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.

  the URL to write the operation

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. Similarly, you need to set the output property to True (True) by using Setdooutput (Boolean) to specify those connections that can be written, before obtaining and writing to the URLConnection stream. 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

You 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. Using these methods together with stream I/O classes allows you to build complex and effective network client applications and services.

easy access to the network

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.

Three methods of connection

 // 
URL url = new url ("http://www.sina.com.cn");
URLConnection Urlcon = Url.openconnection ();
InputStream is = Urlcon.getinputstream ();

// method two
URL url = new url ("http://www.yhfund.com.cn");
HttpURLConnection Urlcon = (httpurlconnection) url.openconnection ();
InputStream is = Urlcon.getinputstream ();

// method three
URL url = new url ("http://www.yhfund.com.cn");
InputStream is = Url.openstream ();

Specific examples

           Long begintime = System.currenttimemillis ();

URL url =New URL ("http://www.yhfund.com.cn");
HttpURLConnection Urlcon = (httpurlconnection) url.openconnection ();
Urlcon.connect ();//Get connections
InputStream is = Urlcon.getinputstream ();
BufferedReader buffer =New BufferedReader (New InputStreamReader (IS));
StringBuffer bs =New StringBuffer ();
String L =Null
while ((L=buffer.readline ())!=null) {
Bs.append (l ). Append ("/n");
}
System.out.println (bs.tostring ());

//system.out.println ("Content-encode:" + Urlcon.getcontentencoding ());
//system.out.println ("Content-length:" + Urlcon.getcontentlength ());
//system.out.println ("Content-type:" + Urlcon.getcontenttype ());
//system.out.println ("Date:" +urlcon.getdate ()) ;

System.out.println ("Total Execution Time:" + (System.currenttimemillis ()-begintime) + "Ms ");
}catch (IOException e) {
System.out.println (e);
}
}
}

Java URL class

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.