Java Reference
In-Depth Information
}
}
catch (MalformedURLException murle)
{
System.err.println("invalid url");
}
catch (IOException ioe)
{
System.err.println("i/o
error:
"+ioe.getMessage());
}
}
}
Compile this source code (
javac GetResource.java
) and execute
java
GetResource
http://tutortutor.ca
.
Thefollowingoutputpresentsashort
prefix of the returned web page:
<!DOCTYPE html PUBLIC "-//w3c//dtd html 4.01//en"
"http://www.w3.org/tr/html4/strict.dtd">
<html>
<head>
<title>
TutorTutor -- /main
</title>
openStream()
is a convenience method for invoking
openConnec-
tion().getInputStream()
.Eachof
URL
's
URLConnection openConnec-
tion()
and
URLConnection openConnection(Proxy proxy)
methodsre-
turnsaninstanceofthe
java.net.URLConnection
class,whichrepresentsacom-
munications link between the application and a URL.
URLConnection
gives you additional control over client/server communication.
For example, you can use this class to output content to various resources that accept
content. In contrast,
URL
only lets you input content via
openStream()
.
URLConnection
declares various methods, including the following:
•
InputStream getInputStream()
returns an input stream that reads
from this open connection.