URLs, InetAddresses, and URLConnections

8/22/98


Click here to start


Table of Contents

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

PPT Slide

CGI

Normal web surfing uses these two steps:

Forms

CGI

GET and POST

HTTP

A Typical HTTP Connection

MIME

Browser Request MIME Header

Server Response MIME Header

Query Strings

URL Encoding

For example,

The URLEncoder class

For example,

String eqs = "Author=" + URLEncoder.encode("Sadie, Julie");

GET URLs

URLConnections

URLConnections vs. URLs

URLConnection five steps:

I/O Across a URLConnection

For example,

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:

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).

For example,

The request line and MIME header are sent as soon as the URLConnection connects. Then use getOutputStream() to get an output stream on which you'll write the x-www-form-urlencoded name-value pairs.

HttpURLConnection

Recall

Response Codes

Java 1.0 only supported GET and POST requests to HTTP servers, but Java 1.1 allows the much broader range of requests specified in the HTTP/1.1 specification including GET, POST, HEAD, OPTIONS, PUT, DELETE, and TRACE.

getRequestMethod()

disconnect()

For example,

usingProxy

The HttpURLConnection class also has two static methods that affect how all URLConnection objects interact with web servers. With a true argument, the HttpURLConnection.setFollowRedirects(boolean followRedirects) method says that connections will follow redirect instructions from the web server. Untrusted applets are not allowed to set this. The boolean method HttpURLConnection.getFollowRedirects() returns true if redirect requests are honored, false if they're not.

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.

To Learn More

Author: Elliotte Rusty Harold

Email: elharo@sunsite.unc.edu

Home Page: http://sunsite.unc.edu/javafaq/

Download presentation source