How To Handle Data from a Post Form When Hosting WebBrowser Control (256195)



The information in this article applies to:

  • Microsoft Internet Explorer (Programming) 4.0
  • Microsoft Internet Explorer (Programming) 4.01
  • Microsoft Internet Explorer (Programming) 4.01 SP1
  • Microsoft Internet Explorer (Programming) 4.01 SP2
  • Microsoft Internet Explorer (Programming) 5
  • Microsoft Internet Explorer (Programming) 5.01
  • Microsoft Internet Explorer (Programming) 5.5

This article was previously published under Q256195

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.

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;
}
				

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


Modification Type:MajorLast Reviewed:5/11/2006
Keywords:kbhowto kbWebBrowser KB256195