90 likes | 192 Views
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
E N D
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 • 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
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);
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);
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)
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:
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
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
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