Application Protocols: HTTP CSNB534 Semester 2, 2007/2008 Asma Shakil.
-
Upload
oswin-stevenson -
Category
Documents
-
view
214 -
download
1
Transcript of Application Protocols: HTTP CSNB534 Semester 2, 2007/2008 Asma Shakil.
![Page 1: Application Protocols: HTTP CSNB534 Semester 2, 2007/2008 Asma Shakil.](https://reader036.fdocuments.us/reader036/viewer/2022062801/56649e4a5503460f94b3d97e/html5/thumbnails/1.jpg)
Application Protocols:HTTP
CSNB534
Semester 2, 2007/2008
Asma Shakil
![Page 2: Application Protocols: HTTP CSNB534 Semester 2, 2007/2008 Asma Shakil.](https://reader036.fdocuments.us/reader036/viewer/2022062801/56649e4a5503460f94b3d97e/html5/thumbnails/2.jpg)
What is HTTP?
HTTP (HyperText Transfer Protocol) is an application-level protocol that uses the TCP as a transport mechanism.
HTTP originated as a means of sharing documents across the Internet. By placing a hyperlink over a word, users would be able to
jump instantly from one document to another, even though the document could reside on servers located in another country.
Published as RFC 1954, pioneered by Tim Berners-Lee.
![Page 3: Application Protocols: HTTP CSNB534 Semester 2, 2007/2008 Asma Shakil.](https://reader036.fdocuments.us/reader036/viewer/2022062801/56649e4a5503460f94b3d97e/html5/thumbnails/3.jpg)
How does HTTP work?
When an HTTP client, such as a Web browser or a search engine, needs to access a file, it establishes a TCP connection to the Web server. Which by default uses TCP port 80 for
connection. The client sends a request for a particular file
and receives an HTTP response Which will often include the contents of the file.
![Page 4: Application Protocols: HTTP CSNB534 Semester 2, 2007/2008 Asma Shakil.](https://reader036.fdocuments.us/reader036/viewer/2022062801/56649e4a5503460f94b3d97e/html5/thumbnails/4.jpg)
How does HTTP work? The response includes
A status code (success/failure of a request) HTTP header information
Content-Length Size of item in octets Content-Type Type of item Content-Encoding Encoding used for item Content-Language Language(s) used in item
Example Of Header Content-Length: 34 Content-Type : text/html Content-Language: english Content-Encoding: ascii
The file contents (if appropriate)
![Page 5: Application Protocols: HTTP CSNB534 Semester 2, 2007/2008 Asma Shakil.](https://reader036.fdocuments.us/reader036/viewer/2022062801/56649e4a5503460f94b3d97e/html5/thumbnails/5.jpg)
Web Clients
Under HTTP/1.0, there are three types of requests that a client application can issue to a web server:
GET HEAD POST
![Page 6: Application Protocols: HTTP CSNB534 Semester 2, 2007/2008 Asma Shakil.](https://reader036.fdocuments.us/reader036/viewer/2022062801/56649e4a5503460f94b3d97e/html5/thumbnails/6.jpg)
GET Request Method
Used when a client needs to retrieve a resource from a web server.
Takes three parameters Pathname of the resource CGI Parameters (for CGI scripts) : to support interactive
pages rather than just static pages. Version of HTTP being used
For example GET /cgi-bin/birthday.pl?month=august&date=24 HTTP/1.0
![Page 7: Application Protocols: HTTP CSNB534 Semester 2, 2007/2008 Asma Shakil.](https://reader036.fdocuments.us/reader036/viewer/2022062801/56649e4a5503460f94b3d97e/html5/thumbnails/7.jpg)
HEAD Request Method
If the client is interested in information about a resource but not the resource itself.
Example HEAD /files/bigfile.zip /HTTP/1.0
Returns only the header fileds.
![Page 8: Application Protocols: HTTP CSNB534 Semester 2, 2007/2008 Asma Shakil.](https://reader036.fdocuments.us/reader036/viewer/2022062801/56649e4a5503460f94b3d97e/html5/thumbnails/8.jpg)
Example
Code for WebServer Demo.
![Page 9: Application Protocols: HTTP CSNB534 Semester 2, 2007/2008 Asma Shakil.](https://reader036.fdocuments.us/reader036/viewer/2022062801/56649e4a5503460f94b3d97e/html5/thumbnails/9.jpg)
HTTP and Java
While developers are free to write, their own HTTP implementations using TCP, the java.net package provides several in-built classes that offer HTTP functionality Java.net.URL Java.net.URLConnection Java.net.HttpURLConnection
![Page 10: Application Protocols: HTTP CSNB534 Semester 2, 2007/2008 Asma Shakil.](https://reader036.fdocuments.us/reader036/viewer/2022062801/56649e4a5503460f94b3d97e/html5/thumbnails/10.jpg)
URL Class URLs can point to
Files Web sites ftp sites Newsgroups E-mail addresses
Examples ftp://metalab.uniten.edu.my/~asma/csnb334/ telnet://localhost:8000/ mailto:[email protected]?subject=My%20Opinion
Components of a URL Protocol :// hostname (:port) – optional Path (can include the CGI parameters also after a ?) (#reference) – optional - fragment identifier
![Page 11: Application Protocols: HTTP CSNB534 Semester 2, 2007/2008 Asma Shakil.](https://reader036.fdocuments.us/reader036/viewer/2022062801/56649e4a5503460f94b3d97e/html5/thumbnails/11.jpg)
Creating a URL Constructors Six constructors
URL(String url_str) Creates a URL object based on the string parameter.
URL(String protocol, String host, String file) Creates a URL object based with the specified protocol, host and file path.
URL(String protocol, String host, int port, String file) Creates a URL object based with the specified protocol, host, port and file path.
URL(String protocol, String host, int port, String file, URLStreamHandler handler) Creates a URL object based with the specified protocol, host, port and file path
and stream handler. URL(URL context, String relative)
Creates a URL object using the context of an existing URL and relative URL E.g. URL = http://somewebsite.com; relative URL = /images/icon.gif Resulting URL http://somewebsite.com/images/icon.gif
URL(URL context, String relative, URLStreamHandler handler) All of them throw java.net.MalformedURLException if the URL cannot be
correctly parsed.
![Page 12: Application Protocols: HTTP CSNB534 Semester 2, 2007/2008 Asma Shakil.](https://reader036.fdocuments.us/reader036/viewer/2022062801/56649e4a5503460f94b3d97e/html5/thumbnails/12.jpg)
Using a URL The URL class provides the following methods to parse a URL and
extract individual components as well as to open an HTTP connection to the resource that it specifies.
Methods Object getContent()
Retrieves the contents of the resource located at the URL. String getProtocol()
Returns the protocol component of a URL String getHost()
Returns the host component of a URL. String getPort()
Returns the port component of a URL. String getFile()
Returns the pathname component of a URL. String getRef()
Returns the reference component of a URL. URLConnection openConnection()
Returns a URLConnection object which can be used to establish a connection to the remote resource.
InputStream openStream() Establishes a connection to the remote server where the resource is located
and provides an input stream to read the resource’s contents
![Page 13: Application Protocols: HTTP CSNB534 Semester 2, 2007/2008 Asma Shakil.](https://reader036.fdocuments.us/reader036/viewer/2022062801/56649e4a5503460f94b3d97e/html5/thumbnails/13.jpg)
A Simple Example
Parsing with the URL Class
![Page 14: Application Protocols: HTTP CSNB534 Semester 2, 2007/2008 Asma Shakil.](https://reader036.fdocuments.us/reader036/viewer/2022062801/56649e4a5503460f94b3d97e/html5/thumbnails/14.jpg)
Running URL parser
Valid URLS http://thisisnotarealmachine/norisithisarealpath http://www.uniten.edu.my:80/#top ftp://ftp.metelab.uniten.edu.my/~asma
Invalid URLS ttp://www.uniten.edu.my:80/#top abcdef://abcdef.com/absoup/
![Page 15: Application Protocols: HTTP CSNB534 Semester 2, 2007/2008 Asma Shakil.](https://reader036.fdocuments.us/reader036/viewer/2022062801/56649e4a5503460f94b3d97e/html5/thumbnails/15.jpg)
Retrieving a Resource with the URL Class
To retrieve a resource using the URL class InputStream URL.openStream() URLConnection URL.openConnection()
A simple example to fetch a URL.
![Page 16: Application Protocols: HTTP CSNB534 Semester 2, 2007/2008 Asma Shakil.](https://reader036.fdocuments.us/reader036/viewer/2022062801/56649e4a5503460f94b3d97e/html5/thumbnails/16.jpg)
URLConnection Class
The URLConnection Class is used to send HTTP requests and read HTTP responses.
URLConnection allows you to Connect to a web server. Set request header fields Read response header fields Read the contents of the resource.
![Page 17: Application Protocols: HTTP CSNB534 Semester 2, 2007/2008 Asma Shakil.](https://reader036.fdocuments.us/reader036/viewer/2022062801/56649e4a5503460f94b3d97e/html5/thumbnails/17.jpg)
Creating a URLConnection
No public constructors for the URLConnection class.
Instead, call the URL.openConnection() method which returns a URLConnection instance. URL myURL = new URL (_some URL_); URLConnection connection =
myURL.openConnection();
![Page 18: Application Protocols: HTTP CSNB534 Semester 2, 2007/2008 Asma Shakil.](https://reader036.fdocuments.us/reader036/viewer/2022062801/56649e4a5503460f94b3d97e/html5/thumbnails/18.jpg)
Using a URLConnection Methods Void connect();
The act of creating a URLConnection object does not establish a connection with the remote host until this method is called.
Object getContent(); Process the contents of a resource and return it as an Object.
String getContentEncoding(); Returns the value of the “Content-encoding” header field.
int getContentLength(); Returns the value of the “Content-length” header field.
String getContentType(); Returns the value of the “Content-type” header field.
![Page 19: Application Protocols: HTTP CSNB534 Semester 2, 2007/2008 Asma Shakil.](https://reader036.fdocuments.us/reader036/viewer/2022062801/56649e4a5503460f94b3d97e/html5/thumbnails/19.jpg)
Content-type Type application: Multipurpose files
application/javascript: Javascript application/octet-stream: Arbitrary byte stream. This is thought of as the "default"
media type used by several operating systems, often used to identify executable files. application/x-shockwave-flash: Adobe Flash files;
Type audio: Audio audio/mpeg: MP3 audio/x-ms-wma: Windows Media Audio; audio/x-wav: WAV audio
Type text: Human-readable text and source code text/html: HTML; text/plain: Textual data; text/xml: Extensible Markup Language;
Type video: Video video/mpeg: MPEG-1 video/mp4: MP4 video; video/quicktime: QuickTime video; video/x-ms-wmv: Windows Media Video;
![Page 20: Application Protocols: HTTP CSNB534 Semester 2, 2007/2008 Asma Shakil.](https://reader036.fdocuments.us/reader036/viewer/2022062801/56649e4a5503460f94b3d97e/html5/thumbnails/20.jpg)
Using a URLConnection
Methods InputStream getInputStream();
Returns an input stream object that reads the resource’s contents pointed to by the URLConnection.
OutputStream getOutputStream(); Returns an output stream object that writes to the
remote connection pointed to by the URLConnection. If the connection does not support writing
UnknownServiceException.
![Page 21: Application Protocols: HTTP CSNB534 Semester 2, 2007/2008 Asma Shakil.](https://reader036.fdocuments.us/reader036/viewer/2022062801/56649e4a5503460f94b3d97e/html5/thumbnails/21.jpg)
A Simple Example
Retrieving a resource with the URLConnection Class