Java Network Programming notes (3)

Source: Internet
Author: User
Tags object constructor final relative string
Notes

Java Network Programming Notes

N5

Three uses the URL class to obtain the data
Chapter 7 Retrieving Data with URLs

1 Creating URL Objects
Throw malformedurlexception when the JVM does not support protocol for URLs

(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 for the protocol will be used.
The file parameter should start with "/", including a path, file name, and an optional anchor point
For 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, and the other 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 U1 file name, then add a new filename test.html, it constitutes U2

2 Get the composition of the URL
GetFile () returns the total path (not the filename), which is the content of the URL from the first "/" to "#". If there is no file part, java1.3 returns an empty string, java1.1,1.2 returns "/"
GetHost () returns the host name and does not contain user information such as User:user
Getport () returns 1 if no port is specified in the URL
Getprotocol ()
GETREF () returns the specified anchor point if no anchor point returns null

Since java1.3
Getquery ()
GetPath () and GetFile () exactly the same
GetUserInfo ()
Getauthority ()

3 Get data from URL
(1) Public final InputStream OpenStream () throws IOException
Open URL, get a InputStream get data
(2) Public URLConnection OpenConnection () throws IOException
Opens the URL, gets a URLConnection object, and the URLConnection object represents an open connection to the network resource. You can use this method when you want to communicate directly with the server. URLConnection can get anything that the server sends, not just the document itself. You can also write data.
(3) Public final Object getcontent () throws IOException
The data downloaded from the URL is treated as an object, such as a picture, text, sound, or a inputstream to an object that cannot be understood. You can use instance's to determine what kind of object it is and cast as the object.
(4) Public final Object getcontent (class[] classes) throws IOException//java1.3
You can specify what kind of class is returned. The method attempts to return the content in the order of the classes array.

4 Tool methods
public boolean samefile (URL other)
Tests whether two URLs point to the same file.
Public String Toexternalform ()
Returns a human-readable string representing the URL, which is equivalent to the ToString () method. Therefore, this method is not commonly used.

5 public boolean equals (Object O)
Meaning of equality: object o is also a URL object, two URLs point to the same file (Samefile ()), and two URLs have the same reference or are null.




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.