1 / 9

Java URL Class

Java URL Class. Represents a Uniform Resource Locator scheme (protocol) hostname port path query string. Parsing. You can use a URL object as a parser : URL u = new URL(“http://www.cs.rpi.edu/”); System.out.println(“Proto:” + u.getProtocol()); System.out.println(“File:” + u.getFile());.

dotsonj
Télécharger la présentation

Java URL Class

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. Java URL Class • Represents a Uniform Resource Locator • scheme (protocol) • hostname • port • path • query string Netprog: Java URL Class

  2. Parsing • You can use a URL object as a parser: URL u = new URL(“http://www.cs.rpi.edu/”); System.out.println(“Proto:” + u.getProtocol()); System.out.println(“File:” + u.getFile()); Netprog: Java URL Class

  3. URL construction • You can also build a URL by setting each part individually: URL u = new URL(“http”, www.cs.rpi.edu,80,”/~hollingd/”); System.out.println(“URL:” + u.toExternalForm()); System.out.println(“URL: “ + u); Netprog: Java URL Class

  4. But wait, there’s more! • URL objects can retrieve the documents they refer to! • actually this depends on the protocol part of the URL. • HTTP is supported • File is supported (“file://c:\foo.html”) • You can get “Protocol Handlers” for other protocols. Netprog: Java URL Class

  5. Retrieving URL contents • There are a number of ways to do this: Object getContent(); InputStream openStream(); URLConnection openConnection(); Netprog: Java URL Class

  6. Getting Header Information • There are methods that return information extracted from response headers: String getContentType(); String getContentLength(); long getLastModified(); Netprog: Java URL Class

  7. URL Sample Code • GetPage.java: retrieves the page specified by a URL on the command line and prints it out. • GetHeaders.java: prints out some response headers given a URL. Netprog: Java URL Class

  8. URLConnection • Represents the connection (not the URL itself). • More control than URL • can write to the connection (send POST data). • can set request headers. • Closely tied to HTTP Netprog: Java URL Class

  9. URLConnection Sample • SubmitForm.java • Does an HTTP POST • URL and form field names/values specified on the command line. Netprog: Java URL Class

More Related