How to automate Internet Explorer to post form data by using Visual C++ .NET or Visual C++ 2005 (815722)



The information in this article applies to:

  • Microsoft Visual C++ 2005 Express Edition
  • Microsoft Visual C++ .NET (2003)
  • Microsoft .NET Framework 1.1

For a Microsoft Visual Basic .NET version of this article, see 311293.

For a Microsoft Visual C# .NET version of this article, see 815724.


This article refers to the following Microsoft .NET Framework Class Library namespaces:
  • System::ComponentModel
  • Interop::SHDocVw
  • System::Text

IN THIS TASK

SUMMARY

An automation controller, such as Microsoft Visual C++ .NET or Microsoft Visual C++ 2005, can automate Microsoft Internet Explorer to use the POST method to send data to an HTTP server computer such as a computer that is running Microsoft Internet Information Services (IIS). This article describes how to automate Internet Explorer to post form data from a Microsoft Visual C++ .NET or Microsoft Visual C++ 2005 application. Although the example illustrates automation of the whole Internet Explorer application, you can use the same technique on the Web Browser ActiveX control when it is hosted in an application.

back to the top

Introduction

Several methods are available for sending data to an HTTP server. Currently, the GET method and the POST method are the most common. The POST method is frequently used to submit form data to an HTTP server when that data exceeds the maximum permitted transfer limit on the GET method. Typically, this limit is 2 kilobytes (KB).

Internet Explorer exposes the IWebBrowser2 interface to automation controllers. The IWebBrowser2 interface has a Navigate2 method.

In Interface Definition Language (IDL) syntax, the Navigate2 method is similar to the following.

Note IDL is a programming language that the syntax of both Microsoft Visual Basic and Microsoft Visual C++ is derived from.
HRESULT Navigate2(VARIANT *URL,
    VARIANT *Flags,
    VARIANT *TargetFrameName,
    VARIANT *PostData,
    VARIANT *Headers
);
To perform a post, only the URL parameter, the PostData parameter, and the Headers parameter are relevant.

To call the Navigate2 method and to 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 string that contains the following HTTP header:

Content-Type: application/x-www-form-urlencoded

This header indicates that the data that is being posted is encoded according to the rules in the HTML specification.

Notice that the Internet Explorer Script Object Model window object also has a Navigate method. This Navigate method only accepts a URL. You cannot use this Navigate method to post data to a Web server.

back to the top

Requirements

The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
  • Microsoft Visual Studio .NET 2003 or Microsoft Visual Studio 2005
  • Microsoft Internet Explorer (Programming) version 5.5 or later
back to the top

Create an Active Server Pages file

To create an Active Server Pages (.asp) file that is named Navpost.asp in a folder on a Microsoft Windows-based computer that is running Internet Information Services (IIS), follow these steps.

Note IIS must recognize the folder as a virtual root that has execute permissions.
  1. On a computer that has IIS installed, create a folder that is named Navpostfolder.
  2. In a text editor such as Notepad, paste the following code in a new file:
    <%@ Language=VBScript %>
     <HTML>
          <%
          cFlavor = Request("Flavor")
          cName = Request("FName")
          %>
          <BODY>
          Hello, <% =cName %>.
          One scoop of <% =cFlavor %> coming right up!
          </BODY>
     </HTML>
  3. Save the file as Navpost.asp in the Navpostfolder folder.
  4. Click Start, and then click Run.
  5. In the Open box, type inetmgr, and then click OK.
  6. In IIS Manager, expand the node of your computer name on the local computer.
  7. Expand Web Sites.
  8. Right-click Default Web Site, point to New, and then click Virtual Directory.
  9. On the Welcome to the Virtual Directory Creation Wizard page, click Next.
  10. In the Alias box, type navpost, and then click Next.
  11. Click Browse.
  12. Locate and then click the Navpostfolder folder that you created in step 1, and then click OK.
  13. Click Next.
  14. Click to select the Execute (such as ISAPI applications or CGI) check box, and then click Next.
  15. Click Finish.
back to the top

Create an application to post form data

To automate Internet Explorer to post form data from a Visual C++ .NET application, follow these steps:
  1. Start Microsoft Visual Studio .NET 2003 or Microsoft Visual Studio 2005.
  2. On the File menu, point to New, and then click Project.

    The New Project dialog box appears.
  3. Under Project Types, click Visual C++ Projects.

    Note In Visual Studio 2005, Visual C++ Projects is changed to Visual C++.
  4. Under Templates, click Windows Forms Application (.NET).

    Note In Visual Studio 2005, Windows Forms Application (.NET) is changed to Windows Forms Application.
  5. In the Name box, type POSTData, and then click OK.

    By default, the Form1 form is created.
  6. Add the following controls to the Form1 form, and then set the Name property and the Text property as indicated in the following table.
    Control nameName propertyText property
    LabellblNameFirst Name
    LabellblFlavorFlavor
    CommandButtoncmdSubmitSubmit
    ComboBoxcboFlavor
    TextBoxtxtName
  7. In Solution Explorer, right-click the POSTData node, and then click Add Reference.
  8. In the Add Reference dialog box, click the COM tab.
  9. Double-click to select Microsoft Internet Controls (Shdocvw.dll), and then click OK.
  10. On the View menu, click Code, and then add the following statements to the top of the code window after the other using directives:
    using namespace System::Text;
    using namespace Interop::SHDocVw;
  11. On the View menu, click Designer to switch to design view.
  12. Double-click the Submit button, and then add the following code to the Click event handler of the cmdSubmit button:
    InternetExplorer * ie  ;
    Object *vPost, *vHeaders, *vFlags, *vTargetFrame, *vUrl;
    String *cFlavor, *cParamFlavor, *cParamName, *cPostData, *cSeparator ;
    vFlags = NULL;
    vTargetFrame = NULL;
    vUrl= S"http://localhost/navpost/navpost.asp ";
    
    int selectedIndex = cboFlavor->SelectedIndex;
    Object * selectedItem = cboFlavor->SelectedItem; 
    cFlavor = selectedItem->ToString();
    cParamFlavor = S"Flavor=";
    cSeparator = S"&";
    cParamName = S"FName=";
    cPostData= String::Concat(cParamName,txtName->Text,cSeparator,cParamFlavor,cFlavor);   
    vHeaders =String::Concat(S"Content-Type: application/x-www-form-urlencoded" , Convert::ToString(10),Convert::ToString(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 InternetExplorerClass();
    ie->Visible = true;
    ie->Navigate2(&vUrl ,&vFlags, &vTargetFrame ,&vPost,&vHeaders);
    Note You must add a common language runtime support compiler option in Visual C++ 2005 to successful compile the previous code sample. To add the common language runtime support compiler option in Visual C++ 2005, use these following steps:
    1. Click Project, and then click <ProjectName> Properties.
    2. Expand Configuration Properties, and then click General.
    3. Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) at the right of Common Language Runtime support under Project Defaults in the right pane, click Apply, and then click OK.
    For more information about the common language runtime support compiler option, visit the following Microsoft Web site:
  13. Switch to design view, and then double-click the Form1 form.
  14. Add the following code to the Load event of Form1:
    cboFlavor->Items->Add(S"Vanilla");
    cboFlavor->Items->Add(S"Chocolate");
    cboFlavor->Items->Add(S"Strawberry");
    cboFlavor->SelectedIndex = 0;
  15. Press CTRL+SHIFT+S to save the project.
  16. Press CTRL+SHIFT+B to build the solution.
  17. Press CTRL+F5 to run the program.
  18. Type a name in the First Name box, select a flavor in the Flavor list, and then click Submit.

    The data from the Visual C++ .NET or Visual C++ 2005 form is posted to the HTTP server, and then the response appears in the visible browser window.
back to the top

REFERENCES

For additional information, click the following article numbers to view the articles in the Microsoft Knowledge Base:

165298 HOWTO: Simulate a form POST request using WinInet

165800 SAMPLE: PostMon.exe demonstrates how to use URL Moniker to POST data

313068 HOW TO: Host a WebBrowser control in Visual C# .NET to post form data

back to the top

Modification Type:MajorLast Reviewed:1/5/2006
Keywords:kbWindowsForms kbWebBrowser kbDirectory kbComboBox kbASPObj kbweb kbInetDev kbAutomation kbHOWTOmaster KB815722 kbAudDeveloper