1 / 9

Making URL Connections

pp 157-165, Horstmann and Cornell COREJAVA Vol. 2. Making URL Connections. High-level services. We've seen how to use socket-level programming to connect to an SMTP server and send email This is (relatively) low-level networking

anaya
Download Presentation

Making URL Connections

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. pp 157-165, Horstmann and Cornell COREJAVA Vol. 2 Making URL Connections

  2. High-level services • We've seen how to use socket-level programming to connect to an SMTP server and send email • This is (relatively) low-level networking • Most real apps would use the JavaMail API, with calls like Transport.send(message) to send a message • Here we'll see high-level calls for URL connections

  3. URLs (Uniform Resource Locators) • URL and URLConnection classes encapsulate much of the complexity of retrieving information from a remote site • You can construct a URL object from a string: URL url = new URL(urlString);

  4. Opening a Stream • if you want to fetch the contents of the resource, just use openStream method of the URL class. • This yields an InputStream object, which can be used in the usual way (e.g. to get a Scanner) InputStream inStream = url.openStream(); Scanner in = new Scanner(inStream);

  5. URLs versus URIs • A URL is a Uniform Resource Locator, whereas a URI is a Uniform Resource Identifier that may not give a location. • A URL is a special kind of URI, with enough info to locate a resource • Here's a URI that's not a URL: mailto:vkantabu@computer.org (no data to locate) • such a URI is called a URN (N = name)

  6. The Java URI and URL classes • URI class exists for parsing • book gives examples why parsing is not trivial • can parse an identifier and break it up into various components with methods like getScheme, getSchemeSpecificPart, getHost, getPort, etc. • URL class can open a stream to the resource (as we've seen) • But URL class only works with schemes that the Java library know how to deal with, such as http:, https:, ftp:, file:, jar:, gopher:

  7. Using a URLConnection to retrieve information • call the openConnection method of the URL class to get the URLConnection object • URLConnection connection = url.openConnection(); • set any request properties, using the methods • setDoInput • setDoOutput • setIfModifiedSince • setConnectTimeout • etc

  8. retrieving info (cont'd) • connect to the remote resource by calling the connect method – connection.connect(); • besides making a socket connection to the server, this method also queries the server for header information • can then query this header information. • two methods, getHeaderFieldKey and getHeaderField, enumerate all fields of the header • as of jdk1.4, getHeaderFields gets a standard Map object (Chap 2 v2 corejava) containing the header fields • there are also convenience methods to query standard fields – getContentType, getContentLength, etc

  9. retrieving info (cont'd) • finally can access the resource data • use getInputStream method to obtain an input stream for reading the info • same input stream that the openStream method of the URL class returns • the other method, getContent, isn't very useful in practice

More Related