Microsoft KB Archive/815725

= How to use the WebBrowser Microsoft ActiveX control in Microsoft Visual C++ to post form data =

Article ID: 815725

Article Last Modified on 11/14/2007

-

APPLIES TO


 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ 2005 Express Edition

-





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



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

This article references the following .NET Framework Class Library namespaces:
 * &quot;System::Windows::Forms&quot;
 * &quot;System::Text&quot;

IN THIS TASK

 * SUMMARY
 * ASPX
 * Post Form Data
 * REFERENCES



SUMMARY
This step-by-step article describes how to use the WebBrowser Microsoft ActiveX control in Microsoft Visual C++ .NET orin Microsoft Visual C++ 2005 to post form data.

Visual C++ .NET or Visual C++ 2005 can use the WebBrowser ActiveX control to send data to a HTTP server, such as Microsoft Internet Information Services (IIS), by using the Post method.

To post data, you can use the Navigate method of the WebBrowser control. Alternatively, if the following parameters are the only relevant parameters, you can use the Navigate2 method of the WebBrowser control to post data:
 * URL
 * PostData
 * Headers

When you call the Navigate method to post form data to a HTTP server, the following conditions must be true:  The URL parameter must specify a valid address. The PostData parameter must contain a byte array.  The Headers parameter must contain a string that contains the following HTTP header: Content-Type: application/x-www-form-urlencoded This header indicates that the data that you post is encoded according to HTML specifications. 

The Microsoft Internet Explorer Script Object Model has a Window object. This Window object also has a Navigate method. This Navigate method only accepts a URL. You cannot use this method to post data to a Web server.

back to the top

Create an ASPX Page
To test the following sample code, save the following Active Server Pages (.aspx) file as the Navpost.aspx file in an IIS directory. IIS recognizes the directory as a virtual root with the Execute Permissions option enabled for scripts. <%@ Page language=&quot;vb&quot;%>  <% dim cFlavor As String dim cName As String cFlavor = Request(&quot;Flavor&quot;) cName = Request(&quot;FName&quot;) %>  Hello, <% =cName %>. One scoop of <% =cFlavor %> coming right up!   back to the top

Post Form Data
To see an example of the Post method in Visual C++ .NET or in Visual C++ 2005, follow these steps: <ol> Use Visual C++ .NET or Visual C++ 2005 to create a Microsoft Windows Forms Application project.

By default, the Form1 form is created.</li> Add the following controls to Form1:

</li> On the View menu, click Toolbox.</li> Right-click the toolbox, and then click Add/Remove Items.

The Customize Toolbox dialog box appears.

Note In Visual C++ 2005, right-click the toolbox, and then click Choose Items. The Choose ToolBox Items dialog box appears.</li> On the COM components tab, click to select the Microsoft Web Browser check box, and then click OK.</li> Add a WebBrowser control to Form1.

By default, the axWebBrowser1 control is added to Form1.</li>  Use the ASCIIEncoding class to convert a string to an array of bytes. Because the ASCIIEncoding class requires you to use the &quot;System::Text&quot; namespace, add the following statement to your Form1.h file to use the &quot;System::Text&quot; namespace: using namespace System::Text; </li> On the View menu, click Properties Window.</li> Add a Form1_Load event handler to Form1, and then add a cmdSubmit_Click event handler to Form1.</li>  In the Form1.h file, replace the Form1_Load event handler and the cmdSubmit_Click event handler with the following code:

Note In Visual C++ 2005, &quot;*&quot; should be changed to &quot;^&quot; in the following code. Additionally, (S&quot;xxx&quot;) should be changed to (&quot;xxx&quot;).

Form1_Load Code private: System::Void Form1_Load(System::Object * sender, System::EventArgs *  e) { cboFlavor->Items->Add(new String(S&quot;Vanilla&quot;)); cboFlavor->Items->Add(new String(S&quot;Chocolate&quot;)); cboFlavor->Items->Add(new String(S&quot;Strawberry&quot;)); cboFlavor->SelectedIndex = 0; } cmdSubmit_Click Code private: System::Void cmdSubmit_Click(System::Object * sender,                                       System::EventArgs *  e) { Object *vPost; Object *vHeaders;

String *cParamFlavor; String *cParamName; String *cSeparator; cParamFlavor = S&quot;Flavor=&quot;; cSeparator = S&quot;&&quot;; cParamName = S&quot;FName=&quot;; String *cFlavor; String *cPostData; cFlavor = cboFlavor->Text; Object *oEmpty = new String(S&quot;&quot;); Object *oURL= new String(S&quot;http://localhost/navpost.aspx&quot;);

cPostData = cParamName; cPostData = String::Concat(cPostData,textBox1->Text); cPostData = String::Concat(cPostData,cSeparator); cPostData = String::Concat(cPostData,cParamFlavor); cPostData = String::Concat(cPostData,cFlavor); vHeaders = String::Copy(S&quot;Content-Type: application/x-www-form-urlencoded\n\r&quot;); vPost = ASCIIEncoding::ASCII->GetBytes(cPostData); axWebBrowser1->Navigate2(&oURL, &oEmpty, &oEmpty, &vPost, &vHeaders); } Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile this code sample.

To do this, follow these steps: <ol> Click Project, and then click ProjectName Properties.

Note ProjectName represents the name of the project.</li> Expand Configuration Properties, and then click General.</li> Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting on the right pane, click Apply, and then click OK.</li></ol>

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

/clr (Common Language Runtime Compilation)

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

These steps apply to the whole article. </li> Modify the URL in the call to the Navigate2 method as appropriate.</li> On the Debug menu, click Start to run the application.

Form1 is displayed.</li> Type your name in the First Name text box, click a flavor, and then click Submit.

The data from Form1 is posted to the HTTP server. The response appears in the visible browser window.</li></ol>

back to the top

<div class="references_section">