Three URL Connection Methods in Java

Source: Internet
Author: User

Java Network Class allows you to implement applications through network or remote connection. Moreover, this platform can now access the Internet and URL resources. The Java URL class makes access to network resources as convenient and convenient as accessing your local folder. You can use the Java URL class to read and modify data through the URL.

Through a URL Connection, we can determine the resource location, such as network files, network pages, and network applications.Program. It contains many syntax elements.
The data obtained from the URL can be varied. All of these require a unified mechanism to read and modify the URL. Java provides such a mechanism in its java.net software package.

The URL class is extracted from the URL identifier. It allows Java programmers to open a specific URL Connection, read and write the data in it, and read and write the first information. It also allows programmers to perform other URL-related operations.

Structure

When creating a java.net URL instance, you can use many public constructors to make it more flexible. For example, this class provides a constructor that uses complete URL Information strings, and a constructor that breaks down URL Information strings into protocols, host names, and files and resources, another method is to break down the URL Information string into the Protocol, host name, port number, and file constructor. First, we use the complete URL to create a URL Class Example:

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

In this example, we created a URL class using the complete URL, specifically specifying 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 an error occurs in the URL syntax, the constructor will issue malformedurlexception.

Connection
Once you have successfully created a URL class instance, you can operate on it. However, before you can access the resources and content on this URL, you must open the connection to these resources and content. You can use openconnection to complete this operation.

Openconnection does not require a parameter. After the operation is successful, it returns an instance of urlconnection class. In listing a, we demonstrated the process of opening a URL Connection. Once your connection is successful, you can start reading and writing the input and output streams of this urlconnection.

SlaveURLReading data in connection

Using java. Io stream class to read data from a URL is a very simple process. Once you establish a successful connection, you can obtain the input stream for the connection and start the write operation. Fortunately, java. Io classes can operate the data returned from the urlconnection stream in the same way as the file stream or socket stream.

PairURLWrite operations

Using java. Io stream classes to write URLs is also very simple. Once you have established a successful connection, you can get the output stream from the connection and start the write operation. Of course, it makes sense only to write the data that the customer wants. Similarly, before obtaining and writing the urlconnection stream, you must use setdooutput (Boolean) to set the output attribute to true) to specify the connections that can be written. Java. Io classes allows you to write data to the urlconnection stream. This operation is the same as the write operation on the file stream and socket stream.

Other operations

You can obtain other types of information from URL and urlconnection object connections, such as host name, port, content length, content encoding, and content type. Using these methods together with stream I/O classes allows you to build complex and effective network customer applications and services.

Convenient access to networks

The URL class provided by the Java platform allows us to conveniently and effectively access resources on the network and make us feel as relaxed and happy as accessing local files. We don't have to worry about the details of network communication. We just need to focus on creating useful applications and services.

 

Three Connection Methods

  //   method 1   
URL url = New URL (" http://www.sina.com.cn ");
urlconnection urlcon = URL. openconnection ();
inputstream is = urlcon. getinputstream ();
// method 2
URL url = New URL (" http://www.yhfund.com.cn ");
httpurlconnection urlcon = (httpurlconnection) URL. openconnection ();
inputstream is = urlcon. getinputstream ();
// method 3
URL url = New URL (" http://www.yhfund.com.cn ");
inputstream is = URL. openstream ();

Example

            Long Begintime = system. currenttimemillis ();

URL url = New URL ("http://www.yhfund.com.cn ");
Httpurlconnection urlcon = (httpurlconnection) URL. openconnection ();
Urlcon. Connect (); // Get connection
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) + "millisecond ");
} Catch (Ioexception e ){
System. Out. println (E );
}
}
}
Related Article

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.