- Forward


HTTP for Text Transport
An Introduction with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

A Review of HTTP
Back SMYC Forward
  • Some Properties:
    • Simple
    • Stateless
  • For Our Current Purposes:
    • An easy way to "GET" text content from a host computer
The Process
Back SMYC Forward
  1. The client opens a connection
  2. The client sends a request (GET, HEAD, or POST)
  3. The client waits for a response
  4. The server processes the request
  5. The server sends a response
  6. The connection is closed
Handling Special Characters
Back SMYC Forward
  • Encoding:
    • URLEncoder
  • Decoding:
    • URLDecoder
An Example
Back SMYC Forward

A Text File Retriever

javaexamples/internet/HTTPFileRetriever.java
 
What Is In The Stream?
Back SMYC Forward
  • What's Possible?
    • The entire HTTP response
  • What's Actually There?
    • Just the data block
Processing the Entire HTTP Response
Back SMYC Forward
  • Making the Connection "Manually":
    • The openConnection() method (which can be passed an optional Proxy) returns a URLConnection which has a connect() method
  • Before Calling connect():
    • Setup parameters can be modified if necessary
  • After Calling connect():
    • getHeaderField(String name) returns a String containing the value
    • getHeaderFields() returns a Map<String,List<String>>
    • getContent() gets the content (as an Object) based on the content type
    • getInputStream() gets the InputStream
An Example
Back SMYC Forward

A Header Retriever/Presenter

javaexamples/internet/DisplayHeaders.java
 
There's Always More to Learn
Back -