How to use the WebBrowser Microsoft ActiveX control in Microsoft Visual C++ to post form data (815725)
The information in this article applies to:
- Microsoft Visual C++ .NET (2003)
- 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:
- "System::Windows::Forms"
- "System::Text"
IN THIS TASKSUMMARYThis 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: When you call the Navigate method to post form data to a HTTP server, the following conditions must be true: 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
topCreate an ASPX PageTo 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 String
cFlavor = Request("Flavor")
cName = Request("FName")
%>
<BODY>
Hello, <% =cName %>. <br>
One scoop of <% =cFlavor %> coming right up!
</BODY>
</HTML>
back to the topPost Form DataTo see an example of the Post method in Visual C++ .NET or in Visual C++ 2005, follow these steps:
- Use Visual C++ .NET or Visual C++ 2005 to create a Microsoft Windows Forms
Application project.
By default, the Form1 form is
created. - Add the following controls to Form1:
Object | Name | Text | Label | IbIName | First Name | Label | IbIFLavor | Flavor | Button | cmdSubmit | Submit | Combobox | cboFlavor |
- On the View menu, click
Toolbox.
- 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. - On the COM components tab, click to select
the Microsoft Web Browser check box, and then click
OK.
- Add a WebBrowser control to Form1.
By default, the axWebBrowser1 control is added to
Form1. - 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; - On the View menu, click Properties
Window.
- Add a Form1_Load event handler to Form1,
and then add a cmdSubmit_Click event handler to
Form1.
- 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:
- Click Project, and then click ProjectName Properties.
Note ProjectName represents the name of the project. - Expand Configuration Properties, and then click General.
- 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:
These steps apply to the whole article.
- Modify the URL in the call to the
Navigate2 method as appropriate.
- On the Debug menu, click
Start to run the application.
Form1 is
displayed. - 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
topREFERENCES 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
Modification Type: | Major | Last Reviewed: | 1/5/2006 |
---|
Keywords: | kbWindowsForms kbWebBrowser kbCOMInterop kbComCtrls kbHOWTOmaster KB815725 kbAudDeveloper |
---|
|
|
©2004 Microsoft Corporation. All rights reserved.
|
|