DelphiFAQ Home Search:

Make TWebBrowser post a form

 

comments17 comments. Current rating: 5 stars (7 votes). Leave comments and/ or rate it.

Question:

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?'

Answer:

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().

uses
  SHDocVw, Httpapp;

procedure TForm1.Button1Click(Sender: TObject);
var
  EncodedDataString: string;
  PostData: OleVariant;
  Headers: OleVariant;
  i: integer;
begin { TForm1.Button1Click }
  // First, create a URL encoded string of the data
  EncodedDataString := 'UserName='+HTTPEncode('MyName')+'&'+
                       'UserPass='+HTTPEncode('MyPassword');

  // 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.
  WebBrowser1.Navigate('http://www.mydomain.com/scripts/login.asp', 
                       EmptyParam, EmptyParam, PostData, Headers);
end; { TForm1.Button1Click }
You don't like the formatting? Check out SourceCoder then!

Comments:

You are on page 1 of 2, other pages: [1] 2
2006-04-01, 01:49:34
sjh7909@163.com from China  
rating
how to use twebbrowser
2006-05-13, 06:08:39
Delphi6 from Georgia  
rating
Perfect solution, i was trying to do the same but from OnBeforeNavigation, but your sugestion is much better!
Kindest regards
2006-06-02, 06:06:03
hein@cdesoftware.co.za from South Africa  
rating
Thanks, works a charm!
2006-09-16, 20:05:54
Jeremy from United States  
I have found this code to be very useful, works great. Thank you!
2007-02-18, 11:41:19
anonymous from Turkey  
i try.. thanks
2008-02-02, 11:21:19
anonymous  
if TargetFrameName is set, this function resulting 'Variant or safe array is locked'.
2008-02-17, 11:06:41
myst@home.pl from Poland  
'if TargetFrameName is set, this function resulting 'Variant or safe array is locked'.'

- You can unlock array by SafeArrayUnaccessData(PostData) function befor returning from procedure

Additionally, You should place WebBrowser.Navigate in 'try' statement if don't want see error message.
2008-03-19, 02:06:38
anonymous from Finland  
How do you use this, if there is also file? (input type=file)
2008-05-04, 14:48:44
anonymous from Greece  
Ok very usefull all this, but How can I catch the postdata variable on the BeforeNavigate2 event , and store it on a variable (OleVariant) to reuse it again in a later time? Can I use the statement v_postdata := postdata; ? Basically I want (using the webbrowser control) to read the post data from a web form when the user clicks on a submit button, cancel the submit on the BeforeNavigate2 event (here i want to read the generated postdata) and in a later time submit the form when the user wants it by clicking on a button on the delphi form which contain the webbrowser control.
2008-05-24, 20:57:38
[hidden] from Costa Rica  
rating
Excelente!
It Works
2008-06-15, 00:48:51
anonymous from China  
rating
very good
2009-03-26, 08:08:07
anonymous from Russian Federation  
rating
I am getting an error 'Undeclared Idntifier: HTTPEncode'.
help
2010-01-03, 23:45:35
jrdudynho@gmail.com from Brazil  
rating
I liked very much, excuse my English, I'm having a problem.
the password field is not being sent to the webbrowser.
Please help me because I need it very much.
I thank you very much.
2010-05-24, 08:23:07
Omair iqbal from Pakistan  
excelent answer
You are on page 1 of 2, other pages: [1] 2

 

 

NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.
 

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: