Delphi .NET (2) Database (71) Delphi IDE (90) Network (39) Printing (3) Strings (12) VCL (83) Windows with Delphi (280)
Exchange Links About this site Links to us 
|
Make TWebBrowser post a form
11 comments. Current rating: (5 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
EncodedDataString := 'UserName='+HTTPEncode('MyName')+'&'+
'UserPass='+HTTPEncode('MyPassword');
PostData := VarArrayCreate([0, length(EncodedDataString)-1], varByte);
for i := 1 to length(EncodedDataString) do
PostData[i-1] := ord(EncodedDataString[i]);
Headers := 'Content-type: application/x-www-form-urlencoded'#10#13;
WebBrowser1.Navigate('http://www.mydomain.com/scripts/login.asp',
EmptyParam, EmptyParam, PostData, Headers);
end;
| |  | |  | You don't like the formatting? Check out SourceCoder then!
Comments:
|
|
|
|
how to use twebbrowser
|
|
|
|
|
Perfect solution, i was trying to do the same but from OnBeforeNavigation, but your sugestion is much better!
Kindest regards
|
|
|
|
|
Thanks, works a charm!
|
|
|
|
|
I have found this code to be very useful, works great. Thank you!
|
|
|
|
|
i try.. thanks
|
|
|
|
|
if TargetFrameName is set, this function resulting 'Variant or safe array is locked'.
|
|
|
|
|
'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.
|
|
|
|
|
How do you use this, if there is also file? (input type=file)
|
|
|
|
|
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.
|
|
|
|
|
Excelente!
It Works
|
|
anonymous from China
|
 |
|
|
very good
|
|