Java Network Programming notes (3)

Source: Internet
Author: User

Java Network Programming notes
N5
3. Obtain data using URL
Chapter 7 Retrieving Data with URLs
1. Create a URL object
MalformedURLException is thrown when the JVM does not support the url protocol.
(1) public URL (String url) throws MalformedURLException
(2) public URL (String protocol, String hostname, String file) throws MalformedURLException
This constructor sets port to-1, so the default port of the protocol will be used.
The file parameter should start with "/" and contain a path, file name, and an optional anchor.
Example: URL u = new URL ("http", "www.eff.org", "/blueribbon.html # intro ");
(3) public URL (String protocol, String host, int port, String file) throws MalformedURLException
You can specify a port. The other port is the same as the second constructor.
(4) public URL (URL base, String relative) throws MalformedURLException
Creates an absolute URL object from a relative URL address and a base URL object.
For example:
Try {
URL u1 = new URL ("http://www.myweb.com/java/index.html ");
URL u2 = new URL (u1, "test.html ");
}
Catch (MalformedURLException e ){}
Remove the u1's file name, then add the new file name test.html to form u2
2. Obtain the URL composition.
GetFile () returns the total path (not the file name), that is, the content starting from the first "/" to "#" in the URL. If there is no file, java1.3 returns an empty string, java1.1, 1.2 Returns "/"
GetHost () returns the host name, excluding user information such as user: user.
GetPort () if no port is specified in the URL,-1 is returned.
GetProtocol ()
GetRef () returns the specified anchor. If no anchor exists, null is returned.
// Since java1.3
GetQuery ()
GetPath () and getFile () are identical.
GetUserInfo ()
GetAuthority ()
3. Get data from URL
(1) public final InputStream openStream () throws IOException
Open the URL and get an InputStream to get the data.
(2) public URLConnection openConnection () throws IOException
Open the URL to obtain a URLConnection object. The URLConnection object indicates an open connection to network resources. This method can be used when you want to directly communicate with the server. URLConnection can get anything sent by the server, not just the document itself. You can also write data.
(3) public final Object getContent () throws IOException
The data downloaded from a URL is treated as an object, such as an image, text, sound, or an InputStream object that cannot be understood. You can use instance of to determine the object and cast it as the object.

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.