Data Centers

Demystify HTTP by understanding how to control data flow

All data transferred through HTTP is text data. Learn how to control this flow of data, which will open new doors to your solutions.

This article originally appeared as a Web Development Zone e-newsletter.

By Phillip Perkins

I'll probably be criticized for saying this, but all data transferred through HTTP is text data. Understanding how to control this flow of data is the key to opening new doors to your solutions.

HTTP allows for different types of data to be streamed across its pathway, and MIME types define this data. For most HTTP transactions, this MIME type is "text/html". The majority of requests from a Web server are Web pages. Web pages are created by a cacophony of technologies, but the end to these Web pages is HTML. HTML is what the browser uses to render the data into a splendor of color, fonts, and intelligent layouts.

Sometimes developers need to present information through an HTTP server that isn't "text/html". An example of this is an image file or any other binary file such as a Microsoft Word document. You can control this by changing the "Content-Type" header in the document. For example, a JPEG-type image file transferred over an HTTP server would have a "Content-Type" of "image/jpeg".

Another header needed in this case is the "Content-Length" header. The Content-Length header defines the size in bytes of the content body of the document. And if the document data is encoded for delivery, another header, "Content-Encoding", defines the algorithm for document encoding. "Content-Disposition" describes how the content of the document should be handled. Two important Content-Disposition values are "attachment" and "inline".

Once the client receives the complete content body, the client closes the connection with the HTTP server. Here's an example of a reply sent to a client machine connected to the HTTP server:

HTTP/1.1 200 OK
Server: Microsoft-IIS/4.0
Date: Mon, 01 Jan 2002 00:00:00 GMT
Connection: Keep-Alive
Content-Length: 2025
Content-Type: text/html
Expires: Mon, 01 Jan 2002 00:00:00 GMT
path=/
Cache-control: private


<HTML. . .

The HTTP server is responding to a "GET http://. . .test.asp" call. The server responds, telling the client that the HTTP version is 1.1. "200 OK" is the response code and the response description. All of the following information is headers to the document to be delivered and the actual document data. This document's Content-Type is "text/html", which lets us know that this is a Web page. After a couple of CRLFs, the document starts at "<HTML. . ."; this is the content body of the response.

In order to send almost any type of document data across the Internet, it's important to understand the delivery mechanism. Say you want to send a Word document across the Internet that would be handled by the client machine once it's received, all you need to do in ASP is to change the "Content-Type" header in the response to "application/vnd-ms.word" and use the Response.BinaryWrite() method to send the contents of the file. Here's an example of the code:

Dim strm
Response.ContentType = "application/vnd-ms.word"
Response.AddHeader "Content-Disposition",
"attachment;filename=test.doc"
Set strm = Server.CreateObject("ADODB.Stream")
strm.Type = 1 'adTypeBinary
strm.Open
strm.LoadFromFile("c:\test.doc")
Response.BinaryWrite strm.Read
strm.Close
Set strm = Nothing
Response.End

In this code, the file is loaded into a binary type ADODB.Stream object. This data is then written to the Response through the BinaryWrite() method. In most cases, when the client browser requests this page, the browser should open this file in Word.

Now you can begin to create your own HTTP solutions for practically any MIME type. The Web world isn't limited to text and HTML. Knowing how to control the output of information to the client machine gives you the ability to create powerful, customizable solutions. It also helps you in your debugging and solution design.

For more information on the HTTP standard, visit the W3C site.

Phillip Perkins is a contractor with Ajilon Consulting. His experience ranges from machine control and client/server to corporate intranet applications.

Editor's Picks

Free Newsletters, In your Inbox