Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

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


View products that this article applies to.

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: 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.
<HTML>
<%
Dim cFlavor, cName
cFlavor = Request("Flavor")
cName = Request("FName")
%>
<BODY>
Hello, <% =cName %>. <br>
One scoop of <% =cFlavor %> coming right up!
</BODY>
</HTML>
				

Visual C# 2005 or Visual C# .NET

To demonstrate a Post method in C#, follow these steps:
  1. 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
    					
  2. 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.
  3. Add the WebBrowser control to the form.

    AxWebBrowser1 is the default name.
  4. Import Namespace System.Text. The ASCIIEncoding class provides a method to convert the string into an array of bytes:
    using System.Text;
    					
  5. Insert the following code into Form1.cs:
    private void Form1_Load(object sender, System.EventArgs e)
    {                 
            cboFlavor.Items.Add("Vanilla");
            cboFlavor.Items.Add("Chocolate");
            cboFlavor.Items.Add("Strawberry");
            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 = "";
    object oURL="http://<server>/navpost.aspx";
    cFlavor = cboFlavor.SelectedItem.ToString();
    cParamFlavor = "Flavor=";
    cSeparator = "&";
    cParamName = "FName=";
    cPostData = cParamName + txtBoxName.Text + cSeparator + cParamFlavor + cFlavor;
    vHeaders = "Content-Type: application/x-www-form-urlencoded" + "\n" + "\r";
    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("http://<server>/navpost.aspx", ref oEmpty, ref oEmpty, ref vPost, ref vHeaders);
    }
    					
  6. Modify the URL in the call to Navigate2, as appropriate.
  7. 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.

↑ Back to the top


References

For more information, click the following article number to view the article in the Microsoft Knowledge Base:
174923 How to use the PostData parameter in WebBrowser control
For more information about the WebBrowser control and the methods, properties, and events that it exposes, visit the following Microsoft Web site: For the HTML specification for Form content types, visit the following W3C Web site: For more information about developing Web-based solutions for Microsoft Internet Explorer, visit the following Microsoft Web sites:

↑ Back to the top


Keywords: KB313068, kbsample, kbinfo, kbhowtomaster

↑ Back to the top

Article Info
Article ID : 313068
Revision : 11
Created on : 5/13/2007
Published on : 5/13/2007
Exists online : False
Views : 506