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
topIntroduction
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
topRequirements
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
topCreate 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 %>
<HTML>
<%
cFlavor = Request("Flavor")
cName = Request("FName")
%>
<BODY>
Hello, <% =cName %>.
One scoop of <% =cFlavor %> coming right up!
</BODY>
</HTML>
- Save the file as Navpost.asp in the Navpostfolder folder.
- Click Start, and then click
Run.
- In the
Open box, type inetmgr, and then click OK.
- In IIS Manager, expand the node of your computer name on
the local computer.
- Expand Web Sites.
- Right-click Default Web Site, point to
New, and then click Virtual
Directory.
- On the Welcome to the Virtual Directory Creation
Wizard page, click Next.
- In the
Alias box, type navpost, and then click Next.
- Click Browse.
- Locate and then click the Navpostfolder folder
that you created in step 1, and then click OK.
- Click Next.
- Click to select the Execute (such as ISAPI applications
or CGI) check box, and then click Next.
- Click Finish.
back to the
topCreate an application to post form data
To automate Internet Explorer to post form data from a Visual C++ .NET application, follow these steps:
- Start Microsoft Visual Studio .NET 2003 or Microsoft Visual Studio 2005.
- On the File menu, point to
New, and then click Project.
The
New Project dialog box appears. - Under Project Types, click Visual
C++ Projects.
Note In Visual Studio 2005, Visual
C++ Projects is changed to Visual
C++. - Under Templates, click Windows
Forms Application (.NET).
Note In Visual Studio 2005, Windows
Forms Application (.NET) is changed to Windows
Forms Application. - In the Name box, type
POSTData, and then click
OK.
By default, the Form1 form is
created. - 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 name | Name property | Text
property |
Label | lblName | First Name |
Label | lblFlavor | Flavor |
CommandButton | cmdSubmit | Submit |
ComboBox | cboFlavor | |
TextBox | txtName | |
- In Solution Explorer, right-click the POSTData
node, and then click Add Reference.
- In the Add Reference dialog box, click the
COM tab.
- Double-click to select Microsoft Internet
Controls (Shdocvw.dll), and then click OK.
- 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;
- On the View menu, click
Designer to switch to design view.
- 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:
- Click Project, and then click <ProjectName> Properties.
- Expand Configuration Properties, and then click General.
- 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: - Switch to design view, and then double-click the
Form1 form.
- 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;
- Press CTRL+SHIFT+S to save the
project.
- Press CTRL+SHIFT+B to build the
solution.
- Press CTRL+F5 to run the
program.
- 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