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