DelphiFAQ Home Search:
General :: Programming :: Delphi :: Network
Network (local network and Internet) programming under Windows with Delphi.


This list is sorted by recent document popularity (not total page views).
New documents will first appear at the bottom.

Featured Article

Make TWebBrowser post a form


I use TWebBrowser to automate some data retrieval from a web server. How can I simulate a click on SUBMIT in a form? If submitting the form would cause a GET request, then I could assemble a URL containing the parameters, but unfortunately the receiving script only handles POST requests - thus my question 'how can I create a POST request with TWebBrowser?'


You need to create the proper header for your request, which means that the data is sent form encoded (application/x-www-form-urlencoded).

Your form data needs to be encoded with HTTPEncode() and stored in an array of variants.

The code below shows how to do this, you only need to modify the field names and values and of course the domain and path to the script. You need unit HTTPApp for function HTTPEncode().

   SHDocVw, Httpapp;
 procedure TForm1.Button1Click(Sender: TObject);
   EncodedDataString: string;
   PostData: OleVariant;
   Headers: OleVariant;
   i: integer;
 begin { TForm1.Button1Click }
   // First, create a URL encoded string of the data
   EncodedDataString := 'UserName='+HTTPEncode('MyName')+'&'+
   // The PostData OleVariant needs to be an array of bytes
   // as large as the string (minus the 0 terminator)
   PostData := VarArrayCreate([0, length(EncodedDataString)-1], varByte);
   // Now, move the Ordinal value of the character into the PostData array
   for i := 1 to length(EncodedDataString) do
     PostData[i-1] := ord(EncodedDataString[i]);
   Headers := 'Content-type: application/x-www-form-urlencoded'#10#13;
   // Parameters 2 and 3 are not used, thus EmptyParam is passed.
                        EmptyParam, EmptyParam, PostData, Headers);
 end; { TForm1.Button1Click }
You don't like the formatting? Check out SourceCoder then!
Generated 8:01:31 on Jun 26, 2019