community.borland.com

Article #20550: Redirecting HTTP requests to other locations and custom response codes

Advanced Web Applications

Redirecting a request to another URL or HTML page
By Corbin Dunn

Download the entire Delphi 5 project for this document.

At some point, you may want to redirect a user request to another URL or HTML page on your server. This is quite easy to do with the following code inside your OnAction event:

  Response.SendRedirect('http://www.borland.com');
One question that you may ask is exactly what this does. The easiest way to find out is to look at the full headers that the server returns:
HTTP/1.1 302 Object Moved Location: http://www.borland.com Server: Microsoft-IIS/5.0 Content-Type: text/html Content-Length: 145 <head><title>Document Moved</title></head> <body><h1>Object Moved</h1> This document may be found <a HREF="http://www.borland.com">here</a></body>

But how did I produce this output? It is easy to "spy" on the headers returned by a webserver by simulating a GET request just like a web browser would make:

Simulating an HTTP GET request

Click on the Start button, and go to Run. Type in:
telnet support.microsoft.com 80
This will open up a connection to support.microsoft.com on the standard HTTP port (80), and you will see a blank telnet window.

Carefully type the following in the telnet window, noting the case:
GET / HTTP/1.0
Note that what you are typing will not be echoed back to you, so be sure to type carefully.

After you type in the request, press return twice to send the request.

Effectively, a break down what you are doing is this:

  • GET - This is the request type. GET is the most common, and is used for getting standard web pages and images. POST is another common request type used for posting data to a web server.
  • / - The next thing you type is a slash. This is the relative document that you want on the server. "/" means the default document. You could have easily typed something else, such as "/a_directory/a_file.html".
  • HTTP/1.0 - This is the HTTP version that your "browser" supports. 1.0 is the lowest, and will always work. 1.1 is also becoming a standard, but some servers may not support HTTP/1.1. You can look at the first line of the response to see what version they do support.
  • Two returns - Two returns are used to send the request. Two are needed because some requests may span multiple lines, and the server waits for a blank line followed by another return before executing the request.
After you press the two returns, you should get some content back, such as the response headers and requested file.

The thing that we are interested in looking at is the response code:
HTTP/1.1 302 Object Moved
The first part tells us what version of HTTP that the server supports (1.1 in this case). The second part is the response code, and the third part is a server defined response message for that code.

To find out exactly what each response code means, and when you should give it, you can look at the HTTP RFC (Request For Comments): http://www.w3.org/Protocols/rfc2068/rfc2068

From that document, we can see that 302 means:

302 Found

   The requested resource resides temporarily under a different URI.
   Since the redirection might be altered on occasion, the client SHOULD
   continue to use the Request-URI for future requests.  This response
   is only cacheable if indicated by a Cache-Control or Expires header
   field.
   ...
In other words, you should use a response of 302 when you want to redirect the user to another URL, but don't want them to have to change the link that they came from. All the proper headers are set up for you when you use:
  Response.SendRedirect('http://www.borland.com');
If you want to inform the web browser that the URL has been permanently moved, you need to send a response code 301:
301 Moved Permanently

   The requested resource has been assigned a new permanent URI and any
   future references to this resource SHOULD use one of the returned
   URIs.  Clients with link editing capabilities ought to automatically
   re-link references to the Request-URI to one or more of the new
   references returned by the server, where possible. This response is
   cacheable unless indicated otherwise.

   The new permanent URI SHOULD be given by the Location field in the
   response. ...
The reason for doing this is because some search engines will see a 301 response, and will update their database with the new link (since it was moved permanently, not temporarily like a 302 indicates).

Now, Delphi doesn't have an automatic way of sending a 301 Response, so we have to manually do it:

Sending a custom response and code

  // This action is going to send the web browser a 301 code indicating
  // a permanent move to a new URL.
  Response.ReasonString := 'Moved Permanently';
  Response.StatusCode := 301;
  // Location is the permanent new location; add it as a custom header
  Response.CustomHeaders.Values['Location'] :=
    'http://www.borland.com';
  Handled := True;

That's it! You should be able to easily extend this to send any custom response code and message.

More related Advanced Web Application documents:

Last Modified: 19-JAN-00