Microsoft KB Archive/311293

= HOW TO: Automate Internet Explorer to POST Form Data =

Article ID: 311293

Article Last Modified on 7/30/2003

-

APPLIES TO


 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition

-



This article was previously published under Q311293





For a Microsoft Visual Basic 6.0 version of this article, see 167658.

IN THIS TASK
SUMMARY
 * More Information
 * Steps to Implement Technique

REFERENCES



SUMMARY
An automation controller, such as Microsoft Visual Basic, 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 Basic .NET 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
  Save the following Active Server Pages (ASP) code into a file, Navpost.asp, in a directory on a Microsoft Windows NT 4.0 or Microsoft Windows 2000 server running IIS version 4.0 or 5.0. The directory should be recognized by IIS as a virtual root with execute permissions: <%@ Language=VBScript %>  <%     cFlavor = Request(&quot;Flavor&quot;) cName = Request(&quot;FName&quot;) %>      Hello, <% =cName %>. One scoop of <% =cFlavor %> coming right up!     Start a new Standard EXE project in Visual Basic .NET. Form1 is created by default. Add the following controls to Form1:   Object                     Name                  Caption ---  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, scroll down to select Microsoft Internet Controls (Shdocvw.dll), and then click Select.  Import the System.Text namespace. The ASCIIEncoding class provides a method to convert strings into an array of bytes. Imports System.Text </li>  Insert the following code inside Class Form1 of Form1.vb: Private Sub cmdSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSubmit.Click

Dim ie As SHDocVw.InternetExplorer Dim vPost As Object Dim vHeaders As Object Dim cFlavor As String Dim cParamFlavor As String Dim cParamName As String Dim cPostData As String Dim cSeparator As String

cFlavor = cboFlavor.Items(cboFlavor.SelectedIndex) 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; + Chr(10) + Chr(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(&quot;http:// /navpost.asp&quot;,, , vPost, vHeaders)

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

cboFlavor.Items.Add(&quot;Vanilla&quot;) cboFlavor.Items.Add(&quot;Chocolate&quot;) cboFlavor.Items.Add(&quot;Strawberry&quot;) cboFlavor.SelectedIndex = 0

End Sub </li> Modify the URL in the call to Navigate, as appropriate. Quit all running instances of Internet Explorer, and then on the Run menu, click Start.</li> Type your name in the first name box, select a flavor, and then click Submit.

The data from the Visual Basic 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">

For additional information about how to use WinInet to simulate a form POST request, click the article number below to view the article in the Microsoft Knowledge Base:

165298 HOW TO: Simulate a Form POST Request Using WinInet

For additional information about how to use a URL Moniker to simulate a form POST request, click the article number below to view the article in the Microsoft Knowledge Base:

165800 PostMon.exe Demonstrates How to Use URL Moniker to POST Data

311294 HOW TO: Host a WebBrowser Control in Visual Basic .NET to Post Form Data

back to the top

Additional query words: kbwebbrowser kbpost

Keywords: kbhowtomaster KB311293

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.