Java Reference
In-Depth Information
System.out.print((char) ch);
}
Note For an HTTP connection, an internal socket is created that connects to
HTTP port 80 on the server identified via the URL's domain name/IP address, unless
you append a different port number to the domain name/IP address (e.g., ht-
tp://tutortutor.ca:8080 ) .
I've created an application that demonstrates locating and accessing an arbitrary re-
source. Listing 9-8 presents its source code.
Listing 9-8. Outputting the contents of the resource identified via a URL command-line argument
import java.io.InputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
class GetResource
{
public static void main(String[] args)
{
if (args.length != 1)
{
System.err.println("usage:
java
getresource
url");
return;
}
try
{
URL url = new URL(args[0]);
try (InputStream is = url.openStream())
{
int ch;
while ((ch = is.read()) != -1)
System.out.print((char) ch);
Search WWH ::




Custom Search