Java Reference
In-Depth Information
public OutputStream getOutputStream() throws IOException. Returns
the output stream of the URL connection for writing to the resource.
public URL getURL().
Returns the URL that this URLConnection object
is connected to.
The URLConnection class also contains methods for accessing the header
information of the connection, allowing you to determine the type and length
of the URL's content, the date it was last modified, the content encoding, and
so forth. Be sure to check the documentation for a listing of all the methods in
the URLConnection class.
The following URLConnectionDemo program connects to a URL entered from
the command line. If the URL represents an HTTP resource, the connection is cast
to HttpURLConnection, and the data in the resource is read one line at a time.
Figure 17.11 shows the output of the program for the URL www.javalicense.com.
import java.net.*;
import java.io.*;
public class URLConnectionDemo
{
public static void main(String [] args)
{
try
{
URL url = new URL(args[0]);
URLConnection urlConnection = url.openConnection();
HttpURLConnection connection = null;
if(urlConnection instanceof HttpURLConnection)
{
connection = (HttpURLConnection) urlConnection;
}
else
{
System.out.println(“Please enter an HTTP URL.”);
return;
}
BufferedReader in = new BufferedReader(
new InputStreamReader(
connection.getInputStream()));
String urlString = “”;
String current;
while((current = in.readLine()) != null)
{
urlString += current;
}
System.out.println(urlString);
}catch(IOException e)
{
e.printStackTrace();
}
}
}
Search WWH ::




Custom Search