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 Handle Data from a Post Form When Hosting WebBrowser Control


View products that this article applies to.

Summary

When you want to retrieve data from a POST form in Active Server Pages (ASP) pages, you can use the Request.Form collection. However, when hosting a WebBrowser control, there is no direct method to retrieve the data. You must do some conversion in the application.

This article demonstrates how to retrieve data as a string from a POST in a Microsoft Visual Basic and Visual C++ WebBrowser control application.

This article assumes you have knowledge of hosting the WebBrowser control and sink events with it.

↑ Back to the top


More information

You could retrieve data sent on an html page at the BeforeNavigate2 event of the WebBrowser control before the data is sent to the Web server. When an html form uses the GET method, the data are simply retrieved from the URL argument. However, when an html form uses the POST method, the data are not associated with the URL, but you can access them via the PostData argument.

The URL argument contains a string in Visual Basic (or a variant pointer to a BSTR in Visual C++). So you could directly access GET data from the URL argument. On the other hand, the PostData argument contains a SafeArray Bytes (or a variant pointer to a SAFEARRAY in Visual C++). So some conversion is necessary to retrieve the data. The following code segments show how this is done.

In Visual Basic

Private Sub ctrlWB_BeforeNavigate2(ByVal pDisp As Object, URL As Variant, Flags As Variant, TargetFrameName As Variant, PostData As Variant, Headers As Variant, Cancel As Boolean)

    If pDisp Is ctrlWB.object Then  
        
        Dim lCount As Long
        Dim lLen As Long
        Dim strPostData As String
        
        lLen = LenB(PostData) ' Use LenB to get the byte count
        
        If lLen > 0 Then    ' If it's a post form, lLen will be > 0
            For lCount = 1 To lLen
                
                strPostData = strPostData & Chr(AscB(MidB(PostData, lCount, 1))) ' Use MidB to get 1 byte at a time
            Next
        MsgBox strPostData
        End If
    
    End If

End Sub
				


In Visual C++

#include "Shlwapi.h"
STDMETHODIMP CWebOCWindow::BeforeNavigate2(IDispatch *pDisp, VARIANT *URL, 
	VARIANT *Flags, VARIANT *TargetFrameName, 
	VARIANT *PostData, VARIANT *Headers, 
	VARIANT_BOOL *Cancel) 
{
         if (PostData != NULL && PostData->vt == (VT_VARIANT|VT_BYREF) && PostData->pvarVal->vt != VT_EMPTY )
	{
		
		char *szTemp = NULL, *szPostData = NULL;
		long plLbound, plUbound;
		
		SAFEARRAY *parrTemp = PostData -> pvarVal->parray;
		SafeArrayAccessData(parrTemp , (void HUGEP **) &szTemp);
		
		SafeArrayGetLBound(parrTemp , 1, &plLbound);
		SafeArrayGetUBound(parrTemp , 1, &plUbound);
		
		szPostData = new char[plUbound - plLbound + 2];
	         StrCpyN(szPostData, szTemp, plUbound - plLbound + 1);
		szPostData[plUbound-plLbound] = '\0';
		SafeArrayUnaccessData(parrTemp);
					
		MessageBox(szPostData);
		
		delete[] szPostData;
	}
	return S_OK;
}
				

↑ Back to the top


References

For a Microsoft Visual Basic .NET version of this article, see 311294.
For more information about developing Web-based solutions for Microsoft Internet Explorer, visit the following Microsoft Web sites:For additional information, click the article number below to view the article in the Microsoft Knowledge Base:
167658 How To Automate Internet Explorer to POST Form Data

↑ Back to the top


Article Info
Article ID : 256195
Revision : 4
Created on : 1/1/0001
Published on : 1/1/0001
Exists online : False
Views : 271