HOW TO: Automate Internet Explorer to POST Form Data

For a Microsoft Visual Basic 6.0 version of this article, see
167658 .

IN THIS TASK

Summary

An automation controller, such as Microsoft Visual Basic, can automate Microsoft Internet Explorer to send data by using the POST method to an HTTP server (such as Microsoft Internet Information Services [IIS]). This article explains how to automate Internet Explorer to perform a POST of form data from a Visual Basic .NET application. Although the examples illustrate automation of the whole Internet Explorer application, the same techniques can be applied to the Web Browser ActiveX control when it is hosted in an application.

More Information

Several methods are available for sending data to an HTTP server. GET and POST are currently the most common two. POST is typically used to submit form data to an HTTP server when that data exceeds the maximum allowable transfer limit on the GET method. This limit is typically 2 kilobytes (KB).

Internet Explorer exposes the IWebBrowser(App) interface to automation controllers. The IWebBrowser(App) interface has a Navigate method. In Interface Definition Language (IDL) syntax (a universal representation from which both the Visual Basic and Visual C++ syntax is derived), the Navigate method looks like the following:
HRESULT Navigate([in] BSTR URL, [in] VARIANT* Flags,
[in] VARIANT* TargetFrameName, [in] VARIANT* PostData,
[in] VARIANT* Headers);
For the purposes of performing a POST, only the URL, PostData, and Headers parameters are relevant.

To call Navigate and POST form data to an HTTP server, the URL parameter must specify a valid address, the PostData parameter must contain a SAFEARRAY of bytes, and the Headers parameter must contain a BSTR that contains the following HTTP header:
Content-Type: application/x-www-form-urlencoded
This header indicates that the data being posted is encoded according to the rules specified in the HTML specification.

Note that the Internet Explorer Script Object Model window object has a navigate method as well. This navigate method will only accept a URL and cannot be used to POST data to a Web server.

Steps to Implement Technique

  1. Save the following Active Server Pages (ASP) code into a file, Navpost.asp, in a directory on a Microsoft Windows NT 4.0 or Microsoft Windows 2000 server running IIS version 4.0 or 5.0. The directory should be recognized by IIS as a virtual root with execute permissions:
    <%@ Language=VBScript %>
    <HTML>
    <%
    cFlavor = Request("Flavor")
    cName = Request("FName")
    %>
    <BODY>
    Hello, <% =cName %>.
    One scoop of <% =cFlavor %> coming right up!
    </BODY>
    </HTML>
  2. Start a new Standard EXE project in Visual Basic .NET. Form1 is created by default. Add the following controls to Form1:

    Object Name Caption
    -------------------------------------------------------------------
    Label lblName First Name
    Label lblFlavor Flavor
    CommandButton cmdSubmit Submit
    ComboBox cboFlavor
    TextBox txtName
  3. On the Project menu, click Add Reference, click the COM tab, scroll down to select Microsoft Internet Controls (Shdocvw.dll), and then click Select.
  4. Import the System.Text namespace. The ASCIIEncoding class provides a method to convert strings into an array of bytes.
    Imports System.Text
  5. Insert the following code inside Class Form1 of Form1.vb:
    Private Sub cmdSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSubmit.Click

    Dim ie As SHDocVw.InternetExplorer
    Dim vPost As Object
    Dim vHeaders As Object
    Dim cFlavor As String
    Dim cParamFlavor As String
    Dim cParamName As String
    Dim cPostData As String
    Dim cSeparator As String

    cFlavor = cboFlavor.Items(cboFlavor.SelectedIndex)
    cParamFlavor = "Flavor="
    cSeparator = "&"
    cParamName = "FName="

    cPostData = cParamName & txtName.Text & cSeparator & cParamFlavor & cFlavor

    vHeaders = "Content-Type: application/x-www-form-urlencoded" + Chr(10) + Chr(13)

    'Convert the string to post to an array of bytes.
    vPost = ASCIIEncoding.ASCII.GetBytes(cPostData)

    'Create an instance of Internet Explorer and make it visible.
    ie = New SHDocVw.InternetExplorer()
    ie.Visible = True
    ie.Navigate2("http://<web server>/navpost.asp", , , vPost, vHeaders)

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    cboFlavor.Items.Add("Vanilla")
    cboFlavor.Items.Add("Chocolate")
    cboFlavor.Items.Add("Strawberry")
    cboFlavor.SelectedIndex = 0

    End Sub
  6. Modify the URL in the call to Navigate, as appropriate. Quit all running instances of Internet Explorer, and then on the Run menu, click Start.
  7. Type your name in the first name box, select a flavor, and then click Submit.

    The data from the Visual Basic form will be posted to the HTTP server, and the response will appear in the visible browser window.

References

For additional information about how to use WinInet to simulate a form POST request, click the article number below to view the article in the Microsoft Knowledge Base:
165298 HOW TO: Simulate a Form POST Request Using WinInet
For additional information about how to use a URL Moniker to simulate a form POST request, click the article number below to view the article in the Microsoft Knowledge Base:
165800 PostMon.exe Demonstrates How to Use URL Moniker to POST Data
311294 HOW TO: Host a WebBrowser Control in Visual Basic .NET to Post Form Data
Properties

Article ID: 311293 - Last Review: Jul 30, 2003 - Revision: 1

Feedback