You are currently offline, waiting for your internet to reconnect

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



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:
  • "System::Windows::Forms"
  • "System::Text"

IN THIS TASK

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="vb"%> <HTML><%dim cFlavor As String dim cName As StringcFlavor = Request("Flavor")cName = Request("FName")%><BODY>Hello, <% =cName %>. <br>One scoop of <% =cFlavor %> coming right up!</BODY></HTML>
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:
  1. Use Visual C++ .NET or Visual C++ 2005 to create a Microsoft Windows Forms Application project.

    By default, the Form1 form is created.
  2. Add the following controls to Form1:

    ObjectNameText
    LabelIbINameFirst Name
    LabelIbIFLavorFlavor
    ButtoncmdSubmitSubmit
    ComboboxcboFlavor
  3. On the View menu, click Toolbox.
  4. 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.
  5. On the COM components tab, click to select the Microsoft Web Browser check box, and then click OK.
  6. Add a WebBrowser control to Form1.

    By default, the axWebBrowser1 control is added to Form1.
  7. Use the ASCIIEncoding class to convert a string to an array of bytes. Because the ASCIIEncoding class requires you to use the "System::Text" namespace, add the following statement to your Form1.h file to use the "System::Text" namespace:
    using namespace System::Text;
  8. On the View menu, click Properties Window.
  9. Add a Form1_Load event handler to Form1, and then add a cmdSubmit_Click event handler to Form1.
  10. 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, "*" should be changed to "^" in the following code. Additionally, (S"xxx") should be changed to ("xxx").

    Form1_Load Code
    private: System::Void Form1_Load(System::Object *  sender, System::EventArgs *  e){    cboFlavor->Items->Add(new String(S"Vanilla"));    cboFlavor->Items->Add(new String(S"Chocolate"));    cboFlavor->Items->Add(new String(S"Strawberry"));    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"Flavor=";cSeparator = S"&";cParamName = S"FName=";String *cFlavor;String *cPostData;cFlavor = cboFlavor->Text;Object *oEmpty = new String(S"");Object *oURL= new String(S"http://localhost/navpost.aspx");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"Content-Type: application/x-www-form-urlencoded\n\r");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:
    1. Click Project, and then click ProjectName Properties.

      Note ProjectName represents the name of the project.
    2. Expand Configuration Properties, and then click General.
    3. 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.
    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.
  11. Modify the URL in the call to the Navigate2 method as appropriate.
  12. On the Debug menu, click Start to run the application.

    Form1 is displayed.
  13. 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.
back to the top
REFERENCES
For more information about the WebBrowser control and about the methods, properties, and events that the WebBrowser control exposes, visit the following Microsoft Web site:back to the top
Properties

Article ID: 815725 - Last Review: 11/14/2007 06:26:28 - Revision: 3.2

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

  • kbwindowsforms kbwebbrowser kbcominterop kbcomctrls kbhowtomaster KB815725
Feedback
soft.com/c.gif?DI=4050&did=1&t=">ml>