such as create an inputStream from URL.openStream();
and read from it. what is the use of this information read?
When we create an URL object using an URL address, actually we are not setting up a connection between our java program and the URL on the internet, right? Since no such connection is built (our 2 are isolated in other words), how do we even read the information from it? in other wor开发者_开发知识库ds, how do we know what it is like?
Sorry, a frosh about network. :p
Thanks for any input!
URL.openStream() does open a connection to the URL. From the API documentation:
Opens a connection to this URL and returns an InputStream for reading from that connection. This method is a shorthand for:
openConnection().getInputStream()
It should be obvious by now that a connection of some sort is established. The connection is setup to fetch the contents of the resource that is located at the URL. That of course, depends on the protocol scheme specified in the URL object - HTTP or JAR. The response can be read as bytes from the input stream, just like any other stream.
An InetAddress is "example.com" (or 99.99.99.99
) but a URL is "http://example.com/wombats.html".
An InetAddress is just the network address a machine, without providing any information about what services that machine supports, or how to connect to them. (The InetAddress class will take care of mapping a DNS name such as "example.com" to an IP address.)
A URL is a location of a resource (e.g. file) and typically implies a way to fetch it. (URL is short for Uniform Resource Locator.) The URL.openStream()
method is a simple way to (attempt to) fetch the resource denoted by a URL.
When you create a URL
object you do not establish any network connections. The connection is established when you (for example) call URL.openStream()
.
精彩评论