SUMMARY
An automation controller, such as Microsoft Visual C#, can
automate Microsoft Internet Explorer to send data by using the POST method to
an HTTP server (such as Microsoft Internet Information Services [IIS]). This
article explains how to automate Internet Explorer to perform a POST of form
data from a Visual C# .NET or Visual C# 2005 application. Although the examples illustrate
automation of the whole Internet Explorer application, the same techniques can
be applied to the Web Browser ActiveX control when it is hosted in an
application.
back to the
topMore Information
Several methods are available for sending data to an HTTP
server. GET and POST are currently the most common two. POST is typically used
to submit form data to an HTTP server when that data exceeds the maximum
allowable transfer limit on the GET method. This limit is typically 2 kilobytes
(KB).
Internet Explorer exposes the IWebBrowser(App) interface to
automation controllers. The IWebBrowser(App) interface has a
Navigate method. In Interface Definition Language (IDL) syntax (a
universal representation from which both the Visual Basic and Visual C++ syntax
is derived), the
Navigate method looks like the following:
HRESULT Navigate([in] BSTR URL, [in] VARIANT* Flags,
[in] VARIANT* TargetFrameName, [in] VARIANT* PostData,
[in] VARIANT* Headers);
For the purposes of performing a POST, only the URL, PostData, and
Headers parameters are relevant.
To call Navigate and 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 that contains the following HTTP header:
Content-Type:
application/x-www-form-urlencoded
This header indicates that the data
being posted is encoded according to the rules specified in the HTML
specification.
Note that the Internet Explorer Script Object Model
window object has a
navigate method as well. This
navigate method will only accept a URL and cannot be used to POST data to
a Web server.
back to the
top Steps to Implement Technique
The following steps explain how to create an ASP file
navpost.asp, in a directory on a Microsoft Windows Operating system running
Internet Information Services(IIS). The directory should be recognized by IIS
as a virtual root with execute permissions:
- Create a folder named Navpostfolder.
Note You must have Internet Information Services (IIS) installed on the computer. - Open a text editor, such as Notepad, and copy the following code into 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, click
Run, type inetmgr in the Open box, 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.
- Type
navpost in the Alias box, and then click
Next.
- Click Browse. Locate and select the Navpostfolder folder that you created, and then click OK.
- Click
Next, click to select Execute(such as ISAPI
applications or CGI), and then click Next.
- Click Finish.
The following steps explain how to automate Internet
Explorer to perform a POST of form data from a Visual C# .NET application:
- In Visual Studio .NET or in Visual Studio 2005, start a new Windows application
using Visual C# .NET or Visual C# 2005. Form1 is created by default.
- From Toolbox, drag the 5 following
controls onto Form1, and then set the Name property and the Text property as indicated in the following table:
Control | Name | Text |
---------- | ------------ | ------------- |
Label | lblName | First Name |
Label | lblFlavor | Flavor |
CommandButton | cmdSubmit | Submit |
ComboBox | cboFlavor | |
TextBox | txtName | |
- On the Project menu, click Add
Reference.
- Click the COM tab, click to select
Microsoft Internet Controls (Shdocvw.dll), click
Select, and then click OK.
Note In Visual Studio 2005, you do not have to click Select. - On the View menu, click
Code, and then add the following statement to the top of the Form1 code
window:
using System.Text;
- Add the following code to the Click event of the cmdSubmit button:
SHDocVw.InternetExplorer ie;
object vPost,vHeaders,vFlags,vTargetFrame,vUrl;
string cFlavor,cParamFlavor,cParamName,cPostData,cSeparator ;
vFlags=null;
vTargetFrame=null;
vUrl="http://localhost/navpost/navpost.asp ";
cFlavor = cboFlavor.Items[cboFlavor.SelectedIndex].ToString();
cParamFlavor = "Flavor=";
cSeparator = "&";
cParamName = "FName=";
cPostData = cParamName + txtName.Text + cSeparator + cParamFlavor + cFlavor;
vHeaders = "Content-Type: application/x-www-form-urlencoded" + Convert.ToChar(10) + Convert.ToChar(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 SHDocVw.InternetExplorer();
ie.Visible = true;
ie.Navigate2(ref vUrl ,ref vFlags, ref vTargetFrame ,ref vPost,ref vHeaders);
- Add the following code to the Load event of Form1:
cboFlavor.Items.Add("Vanilla");
cboFlavor.Items.Add("Chocolate");
cboFlavor.Items.Add("Strawberry");
cboFlavor.SelectedIndex = 0;
- On the Debug menu, click
Start.
- Type the name in the box, select a flavor from the list, and then click Submit. The data from the Visual C#
form will be posted to the HTTP server, and the response will appear in the
visible browser window.
back to the
topREFERENCES
For additional
information, click the following article number to view the article in the
Microsoft Knowledge Base:
165298
HOW TO: Simulate a Form POST Request Using WinInet
For additional informations, click the following article number to
view the article in the Microsoft Knowledge Base:
165800
PostMon.exe Demonstrates How to Use URL Moniker to POST Data
313068 HOW TO: Host a WebBrowser Control in Visual C# .NET to Post Form Data
back to the
top