Microsoft KB Archive/815722

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

Article ID: 815722

Article Last Modified on 1/5/2006

-

APPLIES TO


 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * 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
 * Introduction
 * Requirements
 * Create an Active Server Pages file
 * Create an application to post form data
 * REFERENCES



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.  On a computer that has IIS installed, create a folder that is named Navpostfolder.  In a text editor such as Notepad, paste the following code in a new file: <%@ Language=VBScript %>  <%     cFlavor = Request(&quot;Flavor&quot;) cName = Request(&quot;FName&quot;) %>      Hello, <% =cName %>. One scoop of <% =cFlavor %> coming right up!    Save the file as Navpost.asp in the Navpostfolder folder. Click Start, and then click Run.</li> In the Open box, type inetmgr, and then click OK.</li> In IIS Manager, expand the node of your computer name on the local computer.</li> Expand Web Sites.</li> Right-click Default Web Site, point to New, and then click Virtual Directory.</li> On the Welcome to the Virtual Directory Creation Wizard page, click Next.</li> In the Alias box, type navpost, and then click Next.</li> Click Browse.</li> Locate and then click the Navpostfolder folder that you created in step 1, and then click OK.</li> Click Next.</li> Click to select the Execute (such as ISAPI applications or CGI) check box, and then click Next.</li> Click Finish.</li></ol>

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: <ol> Start Microsoft Visual Studio .NET 2003 or Microsoft Visual Studio 2005.</li> On the File menu, point to New, and then click Project.

The New Project dialog box appears.</li> Under Project Types, click Visual C++ Projects.

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

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

By default, the Form1 form is created.</li> <li>Add the following controls to the Form1 form, and then set the Name property and the Text property as indicated in the following table.

</li> <li>In Solution Explorer, right-click the POSTData node, and then click Add Reference.</li> <li>In the Add Reference dialog box, click the COM tab.</li> <li>Double-click to select Microsoft Internet Controls (Shdocvw.dll), and then click OK.</li> <li> 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; </li> <li>On the View menu, click Designer to switch to design view.</li> <li> 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&quot;http://localhost/navpost/navpost.asp &quot;;

int selectedIndex = cboFlavor->SelectedIndex; Object * selectedItem = cboFlavor->SelectedItem; cFlavor = selectedItem->ToString; cParamFlavor = S&quot;Flavor=&quot;; cSeparator = S&quot;&&quot;; cParamName = S&quot;FName=&quot;; cPostData= String::Concat(cParamName,txtName->Text,cSeparator,cParamFlavor,cFlavor); vHeaders =String::Concat(S&quot;Content-Type: application/x-www-form-urlencoded&quot;, 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: <ol style="list-style-type: lower-alpha;"> <li>Click Project, and then click  Properties.</li> <li>Expand Configuration Properties, and then click General.</li> <li>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.</li></ol>

For more information about the common language runtime support compiler option, visit the following Microsoft Web site:

http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

</li> <li>Switch to design view, and then double-click the Form1 form.</li> <li> Add the following code to the Load event of Form1: cboFlavor->Items->Add(S&quot;Vanilla&quot;); cboFlavor->Items->Add(S&quot;Chocolate&quot;); cboFlavor->Items->Add(S&quot;Strawberry&quot;); cboFlavor->SelectedIndex = 0; </li> <li>Press CTRL+SHIFT+S to save the project.</li> <li>Press CTRL+SHIFT+B to build the solution.</li> <li>Press CTRL+F5 to run the program.</li> <li>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.</li></ol>

back to the top

<div class="references_section">