Automating Internet Explorer to POST Form Data

By: Corbin Dunn

Abstract: How to control Internet Explorer and make it POST form data from an application

Automating Internet Explorer to POST Form Data
By Corbin Dunn
Delphi Developer Support

This document is based on an example from the MSDN: HOWTO: Automate Internet Explorer to POST Form Data. It shows the Delphi way of POSTing form data.

It is easy to automate Internet Explorer (IE) to send data using the POST method to any web server. This example illustrates automation of Internet Explorer (TWebBrowser) while it is embedded inside of the application, but it could easily be not shown and hidden from the user.

Internet Explorer versions 3.0, 3.01, and 3.02 do not support posting data to an HTTP server via the Navigate method. WinInet and URL Monikers are two possible alternatives. For more information on WinInet and Monikers, visit these articles on the MSDN: Q165298 - HOWTO: Simulate a Form POST Request Using WinInet and Q165800 - SAMPLE: Using a URL Moniker to POST Data.

To test the example below, the following Active Server Page (ASP) should be saved to the file navpost.asp in a directory on Windows NT 4 or 2000 machine running at least Internet Information Server (IIS) version 3.0. The directory should be recognized by IIS as a virtual root with execute permission:

  cFlavor = Request("Flavor")
  cName = Request("FName")
  Hello, <% =cName %>. <br>
  One scoop of <% =cFlavor %> coming right up!  

1. Create a new application with Delphi
2. Add two TLabel's, a TEdit, TComboBox, TWebBrowser and a TButton to the form with names and a layout similar to what is shown below:

Form Screen Shot
In the TComboBox, add several flavors to the Items.
3. Under the implementation section of the form, add "uses HTTPApp;". HTTPApp contains the HTTPEncode function used to url-encode strings.
4. Double click on the TButton and add the following code to the OnClick event handler (I named my button btnSubmit):

procedure TForm1.btnSubmitClick(Sender: TObject);
  EncodedDataString: string;
  PostData: OleVariant;
  Headers: OleVariant;
  I: Integer;
  // First, create a URL encoded string of the data
  EncodedDataString := 'FName=' + HTTPEncode(edtFirstName.Text) + '&' +
    'Flavor=' + HttpEncode(cmbxFlavor.Text);
  // The PostData OleVariant needs to be an array of bytes as large
  // as the string (minus the NULL terminator)
  PostData := VarArrayCreate([0, Length(EncodedDataString) - 1], varByte);
  // Now, move the Ordinal value of the character into the PostData array
  for I := 1 to Length(EncodedDataString) do
    PostData[I-1] := Ord(EncodedDataString[I]);
  Headers := 'Content-Type: application/x-www-form-urlencoded' + #10#13;
  // Finally, we just Navigate to the URL. Note that you may have to modify
  // the path to your ASP page's location.
  WebBrowser1.Navigate('https://localhost/scripts/navpost.asp', EmptyParam,
    EmptyParam, PostData, Headers);

You should now be able to run the application and see the results.

Download the complete source from CodeCentral

Server Response from: ETNASC04