Presentation is loading. Please wait.

Presentation is loading. Please wait.

Application Protocols: HTTP CSNB534 Semester 2, 2007/2008 Asma Shakil.

Similar presentations


Presentation on theme: "Application Protocols: HTTP CSNB534 Semester 2, 2007/2008 Asma Shakil."— Presentation transcript:

1 Application Protocols: HTTP CSNB534 Semester 2, 2007/2008 Asma Shakil

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

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

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

5 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

6 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

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

8 Example Code for WebServer Demo.

9 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

10 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:president@whitehouse.gov?subject=My%20Opinion Components of a URL Protocol :// hostname (:port) – optional Path (can include the CGI parameters also after a ?) (#reference) – optional - fragment identifier

11 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.gifhttp://somewebsite.com 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.

12 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

13 A Simple Example Parsing with the URL Class

14 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/

15 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.fetch a URL

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

17 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();

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

19 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;

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

21 A Simple Example Retrieving a resource with the URLConnection Class Retrieving a resource with the URLConnection Class


Download ppt "Application Protocols: HTTP CSNB534 Semester 2, 2007/2008 Asma Shakil."

Similar presentations


Ads by Google