Chapter 5 URLs and URIs

¡Supera tus tareas y exámenes ahora con Quizwiz!

Constructing a URL from its component parts?

URL u = new URL("http", "www.eff.org", "/blueribbon.html#intro");

How to construct a URL from a string?

URL u = new URL("http://www.audubon.org/");

Constructing relative URLs

URL u1 = new URL("http://www.ibiblio.org/javafaq/index.html); URL u2 = new URL(u1, "mailinglists.html");

Relative URLs

URLs that aren't complete but inherit pieces from their parent

public URI parseServerAuthority()

Used because Java can't always initially detect syntax errors in the authority component which causes incorrect parsing

Authority

Names what is responsible for resolving the rest of the URI

public URLConnection openConnection() throws IOException

Opens a socket to the specified URL and returns a URLConnection object A URLConnection represents an open connection to a network resource Use the method to communicate directly to the server The URLConnection gives you access to everything sent by the server.

public Object getContent(Class[] classes) throws IOException

Overreid which lets you choose which class you'd like the content to be returned as. Attempts to return the first format but will use other applicable types URL u = new URL("http://www.nwu.org"); Class<?>[] types = new Class[3]; types[0] = String.class; types[1] = Reader.class; types[2] = InputStream.class; Object o = u.getContent(types);

public URLConnection openConnection(Proxy proxy) throws IOException

Override of the previous method that specifies the proxy server.

Scheme Specific Parts of All URIs

//authority/path?query

URLs

A URI that as well as identifying a resource, provides a specific network location for the resource that a client can use to retrieve a representation of that resources

What do you receive from a server?

A representation of a resource which comes in the form of bytes

Uniform Resource Identifier

A string of characters in a particular syntax that identifies a resource

equals()

Actually tries to resolve the host with the DNS if not all portions are exactly the same This means that it is potentially I/O blocking and should be avoided

toString()

Always an absolute URL

sameFile()

Checks whether two URLs point to the same resource public boolean sameFile(URL other) Does not consider the fragment identifier but the same as equals otherwise

resolve(URI uri)

Compares the uri argument to this URI and use it to construct a new URI object that wraps an absolute URI absolute = new URI("http://www.example.com/"); URI relative = new URI("images/logo.png"); URI resolved = absolute.resolve(relative); // http://www.example.com/images/logo.png

public InputStream openStream() throws IOException

Connects to the resource referenced by the URL, performs any necessary handshaking between the client and the server, and returns an InputStream from which data can be read

Constructing a URI

Constructors public URI(String uri) (String scheme, String schemeSpecificPart, String fragment) (String scheme, String host, String path, String fragment) (String scheme, String userInfo, String host, int port, String path, String query, String fragment)

toExternalForm()

Converts a URL object to a string that can be used in an HTML link or a web browser's Open URL dialog

toURI()

Converts a URL to URI

relativize(URI uri)

Creates a new URI object from the uri arg that is relative to the invoking URI URI absolute = new URI("http://www.example.com/images/logo.png"); URI top = new URI("http://www.example.com/"); URI relative = top.relativize(absolute); // images/logo.png

URI class

Differs from URL class in three ways URI is purely about identification of resources and parsing URI is more conformant to the relevant specs URI object can represent a relative URI while the URL class absolutes all URIs before storing them.

fragment

References a particular part of the remote resource

public Object getContent() throws IOException

Retrieves the data referenced by the URL and tries to make it into some type of object based on the data. Operates by looking at the Content-type field in the header of the data it gets from the server Hard to predict what kind of object you'll get

getRawFoo()

Return the encoded forms of the parts of the URI

getHost()

Returns a String containing the hostname of the URL

getPath()

Returns a String containing the path and file portion of a URL Does not include the query string

getProtocol()

Returns a String containing the scheme of the URL

ClassLoader.getSystemResource(String name)

Returns a URL from which a single resource can be read

toURL()

Returns a file URL matching the given file

getFile()

Returns a string that contains the path portion of a URL

getUserInfo()

Returns information after the scheme and before the host which is delimited by the @

getCodeBase()

Returns the URL of the applet .class file

getDocumentBase()

Returns the URL of the page that contains the applet

getDefaultPort()

Returns the default port used for this URL's protocol when none is specified in the URL

getRef()

Returns the fragment identifier part of the URL

getPort()

Returns the port number specified in the URL as an int IF not port was specified, returns -1

getAuthority()

Returns the portion between the scheme and the path of a URL

getQuery()

Returns the query string of the URL

isOpaque()

Returns true if it is not hierarchical

isAbsolute()

Returns true if the URI is absolute

What are the five Pieces of URLs

Scheme Authority Path Fragment Identifier Query String

getResource(String name)

Searches the path used by the referenced class loader for a URL to the named resource

What constitutes the authority?

userinfo, host and port

URL Class

java.net.URL extends java.lang.Object Uses the Strategy Design Pattern with protocol handlers as the Strategies and URL class forming the context URLs are immutable

getFoo()

methods first decode any percent-escaped characters and then return the decoded part

Syntax of a URL

protocol://userinfo@host:port/path?query#fragment

URI syntax

scheme:scheme-specifc-part:fragment

Syntax of a URI

scheme:scheme-specific-part

Should you prefer URI or URL?

You should only prefer URL when downloading content from a server


Conjuntos de estudio relacionados

Advertising Language, Discursive Practices, and Intertextuality: 1 Coleman (1990), 2 Cook (1992), 3 Frith, Shaw & Cheng (2005), 4 Gottschalk (2002), 5 Panigrahi & Chandra (2013), and 6 Simpson (2001)

View Set

Vocabulary Power Plus Book 2: Lesson 10

View Set

Bone II - Osteoclast Activity and Regulation

View Set

Intermediate accounting Test questions

View Set

Art History Final (Chapters 19-24)

View Set

Independence of India and Pakistan

View Set