Microsoft KB Archive/313068

= How to host a WebBrowser control in Visual C# 2005 or in Visual C# .NET to post form data =

Article ID: 313068

Article Last Modified on 5/13/2007

-

APPLIES TO


 * Microsoft Visual C# 2005
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Internet Explorer (Programming) 6.0
 * Microsoft Internet Explorer 5.5

-



This article was previously published under Q313068





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



SUMMARY
This step-by-step article shows you how to use the WebBrowser ActiveX control in Microsoft Visual C# 2005 or in Microsoft Visual C# .NET to perform a post of form data.

Visual C# 2005 or Visual C# .NET can use the WebBrowser ActiveX control to send data by using the Post method to an HTTP server such as Microsoft Internet Information Server (IIS).

For the purposes of performing a post, you can use the Navigate or Navigate2 method of the WebBrowser control, where only the URL, PostData, and Headers parameters are relevant.

For more information about the other parameters and the Navigate2 method, visit the following Microsoft Web site:

Navigate2 Method

http://msdn.microsoft.com/library/default.asp?url=/workshop/ browser/webbrowser/reference/methods/navigate2.asp

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 byte array, and 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 being posted is encoded according to HTML specifications.

Note that the Internet Explorer Script Object Model has a Window object, which has a Navigate method as well. This Navigate method accepts only a URL and cannot be used to post data to a Web server.

ASPX
To test the following sample code, the following Active Server Pages (.aspx) file should be saved to the Navpost.aspx file in a directory on IIS. The directory should be recognized by IIS as a virtual root with execute permissions for the scripts.  <% Dim cFlavor, cName cFlavor = Request(&quot;Flavor&quot;) cName = Request(&quot;FName&quot;) %>  Hello, <% =cName %>. One scoop of <% =cFlavor %> coming right up!  

Visual C# 2005 or Visual C# .NET
To demonstrate a Post method in C#, follow these steps:   Start a Windows application in Visual C# .NET. Form1 is created by default. Add the following controls to Form1:   Object                     Name                  Text ---

Label                     lblName                First Name Label                     lblFlavor              Flavor Button                    cmdSubmit              Submit

Combo box                 cboFlavor Text box                  txtBoxName  Click View, and then click Toolbox. Right-click the Toolbox to bring up the Customize Toolbox dialog box. On the COM components tab, click to select the Microsoft Web Browser check box, and then click OK.

Note If you are using Visual C# 2005, click View, and then click Toolbox. Right-click the Toolbox, and then click Choose Items to open the Choose Toolbox Items dialog box. On the COM components tab, click to select the Microsoft Web Browser check box, and then click OK. Add the WebBrowser control to the form.

AxWebBrowser1 is the default name.  Import Namespace System.Text. The ASCIIEncoding class provides a method to convert the string into an array of bytes: using System.Text; </li>  Insert the following code into Form1.cs: private void Form1_Load(object sender, System.EventArgs e) { cboFlavor.Items.Add(&quot;Vanilla&quot;); cboFlavor.Items.Add(&quot;Chocolate&quot;); cboFlavor.Items.Add(&quot;Strawberry&quot;); cboFlavor.SelectedIndex = 0; }

private void cmdSubmit_Click(object sender, System.EventArgs e) { object vPost; object vHeaders; string cFlavor; string cParamFlavor; string cParamName; string cPostData; string cSeparator; object oEmpty = &quot;&quot;; object oURL=&quot;http:// /navpost.aspx&quot;; cFlavor = cboFlavor.SelectedItem.ToString; cParamFlavor = &quot;Flavor=&quot;; cSeparator = &quot;&&quot;; cParamName = &quot;FName=&quot;; cPostData = cParamName + txtBoxName.Text + cSeparator + cParamFlavor + cFlavor; vHeaders = &quot;Content-Type: application/x-www-form-urlencoded&quot; + &quot;\n&quot; + &quot;\r&quot;; vPost = ASCIIEncoding.ASCII.GetBytes(cPostData); axWebBrowser1.Navigate2(ref oURL, ref oEmpty, ref oEmpty, ref vPost, ref vHeaders); //OR You can use the Navigate method as follows //axWebBrowser1.Navigate(&quot;http:// /navpost.aspx&quot;, ref oEmpty, ref oEmpty, ref vPost, ref vHeaders); }                   </li> Modify the URL in the call to Navigate2, as appropriate.</li> Type your name in the First Name text box, choose a flavor, and then click the Submit command button.

The data from the form will be posted to the HTTP server, and the response will appear in the visible browser window.</li></ol>

<div class="references_section">