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/en-us/library/aa752094(VS.85).aspx
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: http://msdn.microsoft.com/en-us/library/aa752094(VS.85).aspx
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:- 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;
- 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); }
- Modify the URL in the call to Navigate2, as appropriate.
- 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.