Microsoft KB Archive/815724

= How to automate Internet Explorer to POST form data by using Visual C# .NET or Visual C# 2005 =

Article ID: 815724

Article Last Modified on 12/11/2006

-

APPLIES TO


 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual C# 2005 Express Edition

-





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

IN THIS TASK

 * SUMMARY
 * More Information
 * Steps to Implement Technique
 * REFERENCES



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 top

More 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 %>  <%     cFlavor = Request(&quot;Flavor&quot;) cName = Request(&quot;FName&quot;) %>      Hello, <% =cName %>. One scoop of <% =cFlavor %> coming right up!    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.</li> Expand Web Sites.</li> Right-click Default Web Site, point to New, and then click Virtual Directory.</li> On the Welcome to the Virtual Directory Creation Wizard page, click Next.</li> Type navpost in the Alias box, and then click Next.</li> Click Browse. Locate and select the Navpostfolder folder that you created, and then click OK.</li> Click Next, click to select Execute(such as ISAPI applications or CGI), and then click Next.</li> Click Finish.</li></ol>

The following steps explain how to automate Internet Explorer to perform a POST of form data from a Visual C# .NET application: <ol> 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.</li> 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: </li> On the Project menu, click Add Reference.</li> 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.</li>  On the View menu, click Code, and then add the following statement to the top of the Form1 code window: using System.Text; </li>  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=&quot;http://localhost/navpost/navpost.asp &quot;; cFlavor = cboFlavor.Items[cboFlavor.SelectedIndex].ToString; cParamFlavor = &quot;Flavor=&quot;; cSeparator = &quot;&&quot;; cParamName = &quot;FName=&quot;; cPostData = cParamName + txtName.Text + cSeparator + cParamFlavor + cFlavor; vHeaders = &quot;Content-Type: application/x-www-form-urlencoded&quot; + 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);   </li>  Add the following code to the Load event of Form1: cboFlavor.Items.Add(&quot;Vanilla&quot;); cboFlavor.Items.Add(&quot;Chocolate&quot;); cboFlavor.Items.Add(&quot;Strawberry&quot;); cboFlavor.SelectedIndex = 0; </li> On the Debug menu, click Start.</li> <li>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.</li></ol>

back to the top

<div class="references_section">