1. Home
  2. Computing & Technology
  3. Delphi Programming
RTL reference|Glossary|Tips/Tricks|FREE App/VCL|Best'O'Net|Books|Link To
 
ISAPI tutorial for Delphi developers
Page 6: A close look at the TWebRequest object
 More of this Feature
• Pg 1: Intro to web-broker
• Pg 2: Using this tutorial
• Pg 3: Getting started
• Pg 4: Web Actions
• Pg 5: The first ISAPI app
• Pg 7: Request-responding
• Pg 8: TPageProducers
• Pg 9: Cookies made easy
• Pg 10: Stateless HTTP
• Pg 11: DB enabled apps
• Pg 12: FAQ
• Pg 13: DB apps - Part 1
• Pg 14: DB apps - Part 2
• Pg 15: DB apps - Part 3
 Join the Discussion
"Post your questions, concerns, views and comments to this article..."
Discuss!
 Related Resources
• Internet programming
• CGI and ISAPI with Delphi

So, up to this point we know how to create a simple ISAPI application, insert the code needed and run it. In this chapter I am going to examine the TWebRequest object, covering it's most important methods and properties.

The TWebRequest contains the information that is made by the client request and passed on to the web server application. It has three descendants, depending on the application you are working with:

1. TISAPIRequest (if you are creating an ISAPI/NSAPI application).

2. TCGIRequest (if you are creating a CGI console application).

3. TWinCGIRequest (if you are creating a WinCGI application).

We will be focusing on the TISAPIRequest object since the examples in this tutorial are mainly based on ISAPI's. However, if you list the properties, methods of each one you can see that they are basically the same.

I am not going to cover each and every method and property in this chapter, only the ones I see as relevant for this tutorial. If I did, it would basically be a copy of the Delphi help files, and since we all have those, I'll leave it at that.

   Properties
URL, Host, ScriptName, PathInfo and Query
First off, let's examine the URL property. This contains the target value of the requested object. Additionally, the TWebRequest object parses this string into several fields which are Host, ScriptName, PathInfo and Query. To see this better, I am going to show it with an example.

http://localhost/scripts/chapter6.dll /action?target=10

Referer
As I mentioned in the previous chapter, the Referer contains the URI where the request came from. For example, if you have page with URL: http://localhost/test1.htm, and there is a link to call the DLL http://localhost/scripts/test1.dll/action, the Referer property will contain the value: "http://localhost/test1.htm".

RemoteAddr
Similarly, RemoteAddr contains information about where the request came from. This time it contains the clients IP address.

UserAgent
The UserAgent contains information about the clients browser and operating system. For example, if you are viewing a web page with Internet Explorer 4 and your operating system is Windows NT, the UserAgent could contains something similar to: Mozilla/Microsoft IE 4.1 (Windows NT).

Let's pause for a minute here. Taking into account the information we have up to now, see how easy it would be to make a Statistics application for your web-site. With the Referer you could see what page is being visited. With RemoteAddr you could see the user's IP address and therefor find out what domain it corresponds to, thus extracting the country of origin. Finally with the UserAgent property you could see what browser and operating system they are using. The first ISAPI application I ever made (and had to, so it wan't by choice) was an auditing system that did what I have just described. It additionaly inserted this information into a database and made graphics for you the customer to see a report on the hits to his/her website. If you think about it, it's actually very easy to do.

The next two properties we are going to examine are the most basic and important ones. They let us interact and exchange information with the client. I am referring to the ContentFields and QueryFields properties.

ContentFields
Have you ever filled out an HTML form? Well if you haven't by now...WHY THE HELL NOT? You can find them everywhere, whether it's checking your mail from a web-based mail system, downloading trial versions of software, contacting a company, etc. They are everywhere on the Internet. Forms provide us with a very powerful mean of gathering information. They also allow us to make on-line database application where people could update a database or make queries with just a browser.

Well, when submitting information to a DLL (or CGI) application, you are using the HTTP Post method. Below is an example of what a form looks like:

Name:

SurName:


and here is the HTML code:

<FORM METHOD="post" ACTION="http://localhost/scripts/chapter6.dll/form"> Name:<br><input type="text" name="NAME_FIELD"><br> SurName:<br><input type="text" name="SURNAME_FIELD"><br> <input type="submit" name="Submit" value="Submit"><br> <input type="reset" name="reset" value="Reset"> </FORM>

(Note that I have taken tags like Font Face, Paragraph, etc out do make it simple).

First, let's take a look at the form tag. It indicates that the information is going to be submitted via POST method. It then tells what action it's going to call (this is basically the same actions that your application consists of). In this case, it's going to call the action "form" of our "chapter6.dll". Next we have the fields that the form contains. The first one is of type TEXT and is called NAME_FIELD. The next one is called SURNAME_FIELD. Lastly, there are two buttons. The first one is the one that actually submits the information contained in the form to the action specified. The second one just resets the form.

IMPORTANT: Only one Submit is performed per form. Note that the actual action isn't associated with the SUBMIT button, but with the FORM. You can never make a form submit to two different places with just plain HTML. There are ways to do this with JavaScript and it's should be quite simply (I'm not going to get into that right now though).

So, what has all this got to do with the ContentFields property we were talking about? Well, the ContentFields property actually contains all the information that is in the form. Let's take a look at our next example program. This example makes use of the ContentFields property to extract the name and surname and create a response that contains the phrase: "Hello Name Surname, how are you doing today?".

Let's look at the code:

Response.Content := 'Hello '+ Request.ContentFields.Values['NAME_FIELD'] + ' ' + Request.ContentFields.Values['SURNAME_FIELD'] + ' how are you today?';

Well, that looks easy. As you can see, to retrieve the values of an HTML form field, all you need to do is access the stringlist of values of the ContentFields, indicating the name you have the field in the HTML form.

Of course, this works with TEXT fields. What about things like a CHECKBOX. Again, this is quite simple. When you post a form that contains a checkbox to your application, if the box is ticked (checked), the value will be in the ContentFields. If it is not checked, then the field won't even be contained in the ContentFields. For example, if you have a CHECKBOX named Tick1:

to actually see if the form was posted with the checkbox ticked, you could do something like: if Request.ContentFields.IndexOfName('Tick1') <> -1 ..... We will examine the ContentFields with more detail in later chapters when we need to make use of the properties. This is just a small sample to show you how to access the fields.

QueryFields
But what about a URL like: 'http://localhost/scripts/chapter6b.dll/action?proto=http&target=www.borland.com? This is what a GET looks like. To access the parameters of the call you would use the QueryFields and NOT the ContentFields.

Let's take a look at the code for this example.

...
if (Request.QueryFields.Values['proto'] = 'http') or
   (Request.QueryFIelds.Values['proto'] = 'ftp') then

if Request.QueryFields.Values <> '' then

   Response.SendRedirect
        (Request.QueryFields.Values['proto'] + '://' +
   Request.QueryFields.Values['target'])
...

We'll examine the SendRedirect method in the next chapter. The idea here is to see how to access the QueryFields values. Again as with the ContentFields you can use the Values property indicating the name of the field. All this action does is re-direct the call to the URI specified.To run the example, open your browser and type different URI's. Make sure you have the DLL copied to the correct location.

We will examine more properties of the TWebRequest object in later chapter, as we need them.

   Methods
I am not going to go into too much detail about the Methods of the TWebRequest. I have hardly ever needed to use any of them. However, I have occassionaly used two, so I'll quickly tell you about these. For more information, refer to the on-line Delphi help.

GetFieldByName
As you saw with the Properties, there are a number of HTTP headers the the TWebRequest handles (such as PathInfo, Host, etc). You might want to one day have access to a certain HTTP header that is not available to you through this properties. To do so, you can use the GetFieldByName method.

ReadClient
As great and easy to use as the ContentFields are, they have their restrictions. The main one is that the Content is limited in size. You can only retrieve contents of up to 48K. Past this value, you need to use the ReadClient function to get access to additional data. This could be used for example when you want to create an application to upload files via the web to your server.

The basic methods and properties needed to start doing some serious applications are covered now. In the next chapter I'll examine the TWebResponse object.

Next page > Responding to a request > Page 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15

All graphics (if any) in this feature created by Zarko Gajic.

 More Delphi
· Learn another routine every day - RTL Quick Reference.
· Download free source code applications and components.
· Talk about Delphi Programming, real time.
· Link to the Delphi Programming site from your Web pages.
· Tutorials, articles, tech. tips by date: 2001|2000|1999|1998 or by TOPIC.
· NEXT ARTICLE: ADO Cursors - DB/10.
Chapter ten of the free Delphi Database Course for beginners. How ADO uses cursors as a storage and access mechanism, and what you should do to choose the best cursor for your Delphi ADO application.
 Stay informed with all new and interesting things about Delphi (for free).
Subscribe to the Newsletter
Name
Email

 Got some code to share? Got a question? Need some help?

Explore Delphi Programming

More from About.com

  1. Home
  2. Computing & Technology
  3. Delphi Programming

©2008 About.com, a part of The New York Times Company.

All rights reserved.