URLs, InetAddresses, and URLConnections
We will learn how Java handles
I assume you
Applet Network Security Restrictions
Some Background
Hosts
Internet addresses
Domain Name System (DNS)
The InetAddress Class
Creating InetAddresses
The getByName() factory method
Other ways to create InetAddress objects
Getter Methods
Utility Methods
Ports
Protocols
IETF RFCs
W3C Standards
URLs
Example URLs
The Pieces of a URL
The java.net.URL class
Content and Protocol Handlers
Finding Protocol Handlers
Supported Protocols
URL Constructors
Constructing URL Objects
Constructing URL Objects in Pieces
Including the Port
Relative URLs
Constructing Relative URLs
Parsing URLs
For example,
Missing Pieces
Reading Data from a URL
Webcat
The Bug in readLine()
CGI
Normal web surfing uses these two steps:
Forms
GET and POST
HTTP
A Typical HTTP Connection
What the client sends to the server
MIME
Browser Request MIME Header
Server Response MIME Header
Query Strings
URL Encoding
The URLEncoder class
String eqs = "Author=" + URLEncoder.encode("Sadie, Julie");
The URLDecoder class
GET URLs
URLConnections
URLConnections vs. URLs
URLConnection five steps:
I/O Across a URLConnection
Reading Header Data
getHeaderFieldKey()
For example
getHeaderFieldInt() and getHeaderFieldDate()
The long returned by getHeaderFieldDate() can be converted into a Date object using a Date() constructor like this:
Six Convenience Methods
try {
Writing data to a URLConnection
Nine Steps:
6. Call getOutputStream() to get an output stream object.
POST CGIs
A POST request includes
A URLConnection for an http URL will set up the request line and the MIME header for you as long as you set its doOutput field to true by invoking setDoOutput(true).
The request line and MIME header are sent as soon as the URLConnection connects. Then getOutputStream() returns an output stream on which you can write the x-www-form-urlencoded name-value pairs.
HttpURLConnection
Recall
Response Codes
HTTP Protocols
getRequestMethod()
disconnect()
usingProxy
Redirect Instructions
PPT Slide
HTML is returned for browsers that don't understand redirects, but most modern browsers do not display this and jump straight to the page specified in the Location header instead.
Following Redirects
To Learn More
Questions?
Email: elharo@metalab.unc.edu
Home Page: http://metalab.unc.edu/javafaq/
Download presentation source