Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Url Connection


Published on

  • Be the first to comment

  • Be the first to like this

Url Connection

  1. 1. URL Connection
  2. 2. URLs and URIs <ul><li>The URL and URLConnection classes encapsulate much of the complexity of retrieving information from a remote site. Here is how you specify a URL. </li></ul><ul><li>URL url = new URL(urlString); </li></ul><ul><li>The Java 2 platform supports both HTTP and FTP resources. </li></ul><ul><li>If you simply want to fetch the contents of the resource, then you can use the openStream method of the URL class. This method yields an InputStream object. Using this stream object, you can easily read the contents of the resource . </li></ul>
  3. 3. URLs and URIs <ul><li>InputStream uin = url.openStream(); </li></ul><ul><li>BufferedReader in </li></ul><ul><li>= new BufferedReader(new InputStreamReader(uin)); </li></ul><ul><li>String line; </li></ul><ul><li>while ((line = in.readLine()) != null) { </li></ul><ul><li>process line; </li></ul><ul><li>} </li></ul><ul><li>The package makes a useful distinction between URL s </li></ul><ul><li>( uniform resource locators ) and URI s ( uniform resource identifiers ). </li></ul><ul><li>A URI is a purely syntactical construct that specifies the various parts of the string specifying a web resource. A URL is a special kind of URI , namely one with sufficient information to locate a resource. Other URIs, such as </li></ul><ul><li> </li></ul><ul><li>are not locators—there is no data to locate from this identifier. Such a URI is called a URN (uniform resource name ). </li></ul>
  4. 4. Using a URLConnection to Retrieve Information <ul><li>If you want additional information about a web resource, then you should use the URLConnection class, which gives you much more control than the basic URL class. </li></ul><ul><li>Call the openConnection method of the URL class to obtain the URLConnection object: </li></ul><ul><li>URLConnection connection = url.openConnection(); </li></ul><ul><li>Set any request properties, using the methods </li></ul><ul><ul><li>setDoInput </li></ul></ul><ul><ul><li>setDoOutput </li></ul></ul><ul><ul><li>setIfModifiedSince </li></ul></ul><ul><ul><li>setUseCaches </li></ul></ul><ul><ul><li>setAllowUserInteraction </li></ul></ul><ul><ul><li>setRequestProperty </li></ul></ul><ul><li>Connect to the remote resource by calling the connect method. </li></ul><ul><ul><li>connection.connect(); </li></ul></ul>
  5. 5. Using a URLConnection to Retrieve Information <ul><li>After connecting to the server, you can query the header information. There are two methods, getHeaderFieldKey and getHeaderField , to enumerate all fields of the header. As of SDK 1.4, there is also a method getHeaderFields that gets a standard Map object containing the header fields. For your convenience, the following methods query standard fields. </li></ul><ul><ul><li>getContentType </li></ul></ul><ul><ul><li>getContentLength </li></ul></ul><ul><ul><li>getContentEncoding </li></ul></ul><ul><ul><li>getDate </li></ul></ul><ul><ul><li>getExpiration </li></ul></ul><ul><ul><li>getLastModified </li></ul></ul>
  6. 6. Using a URLConnection to Retrieve Information <ul><li>Finally, you can access the resource data. Use the getInputStream method to obtain an input stream for reading the information. (This is the same input stream that the openStream method of the URL class returns.). </li></ul><ul><li>Some programmers form the wrong mental image when using the URLConnection class and think that the getInputStream and getOutputStream methods are similar to those of the Socket class. But that isn't quite true. The URLConnection class does quite a bit of magic behind the scenes, in particular the handling of request and response headers. </li></ul><ul><li>URL ( String url_str ) throws—creates a URL object based on the string parameter. If the URL cannot be correctly parsed,a MalformedURLException will be thrown. </li></ul>
  7. 7. URL Class <ul><li>URL ( String protocol, String host, String file ) throws — creates a URL object with the specified protocol , host , and file path . </li></ul><ul><li>URL ( String protocol, String host, int port, String file ) throws — creates a URL object with the specified protocol , host , port , and file path . </li></ul><ul><li>String getProtocol () — returns the protocol component of a URL . </li></ul><ul><li>String getHost () — returns the hostname component of a URL . </li></ul><ul><li>String getPort () — returns the port component of a URL . This is an optional component, and if not present a value of –1 will be returned. </li></ul><ul><li>String getFile () — returns the pathname component of a URL. </li></ul>
  8. 8. URL Class <ul><li>String getRef () — returns the reference component of a URL. A null value will be returned if no reference was specified. </li></ul><ul><li>URLConnection openConnection()— returns a URLConnection object, which can be used to establish a connection to the remote resource. The name of this method can be deceiving, though, as no connection will be established until further methods of the URLConnection object are invoked. </li></ul><ul><li>InputStream openStream () throws — establishes a connection to the remote server where the resource is located, and provides an InputStream that can be used to read the resource's contents. This method provides a quick and easy way to retrieve the contents of a URL, without the added complexity of dealing with a URLConnection object. </li></ul>
  9. 9. Retrieving a Resource with the URL Class <ul><li>There are two URL methods that can assist in retrieving the contents of a remote resource: </li></ul><ul><li>1. InputStream URL.openStream() </li></ul><ul><li>2. URLConnection URL.openConnection(); </li></ul><ul><li>For greater control over how the request is made, a URLConnection object created by invoking the URL.openConnection () method would be used. In many situations, however, a simpler way to retrieve the contents of a resource is called for. The openStream () method returns an InputStream , which makes reading a resource simple. </li></ul>
  10. 10. Retrieving a Resource with the URL Class <ul><li>public class FtpUrls { </li></ul><ul><li>public static void main(String[] args) throws IOException { </li></ul><ul><li>//URL url = new URL( &quot;ftp://user:password@;); </li></ul><ul><li>URL url = new URL( &quot;; ); </li></ul><ul><li>InputStream uin = url.openStream(); </li></ul><ul><li>BufferedReader in = new BufferedReader( new </li></ul><ul><li>InputStreamReader(uin)); </li></ul><ul><li>String line; </li></ul><ul><li>while ((line = in.readLine()) != null ) { </li></ul><ul><li>System. out .println(line); </li></ul><ul><li>} </li></ul><ul><li>in.close(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>