2006. március 19., vasárnap

Retrieving POST data in a TWebBrowser


Problem/Question/Abstract:

How do I get the POST data in a TWebBrowser event?

Answer:

In the BeforeNavigate2 event of TWebBrowser, you receive the PostData and Header data as OleVariant.  If you simply assign the OleVariant type to a string, you may get part of the data or garbage.  

You can convert the OleVariant to String using this function:

function VariantToString(AVar: OleVariant): string;
var
  i: integer;
  V: olevariant;
begin
  Result := '';
  if VarType(AVar) = (varVariant or varByRef) then
    V := Variant(TVarData(AVar).VPointer^)
  else
    V := AVar;

  if VarType(V) = (varByte or varArray) then
  try
    for i := VarArrayLowBound(V, 1) to VarArrayHighBound(V, 1) do
      Result := Result + Chr(Byte(V[i]));
  except;
  end
  else
    Result := V;
end;

Nincsenek megjegyzések:

Megjegyzés küldése