Delphi .NET (2) Database (71) Delphi IDE (89) 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
17 comments. Current rating: (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
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:
| You are on page 1 of 2, other pages: [1] 2 | |
sjh7909@163.com from China
|
 |
|
|
how to use twebbrowser
|
|
Delphi6 from Georgia
|
 |
|
|
Perfect solution, i was trying to do the same but from OnBeforeNavigation, but your sugestion is much better!
Kindest regards
|
|
hein@cdesoftware.co.za from South Africa
|
 |
|
|
Thanks, works a charm!
|
|
Jeremy from United States
|
|
|
|
I have found this code to be very useful, works great. Thank you!
|
|
anonymous from Turkey
|
|
|
|
i try.. thanks
|
|
|
|
|
if TargetFrameName is set, this function resulting 'Variant or safe array is locked'.
|
|
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.
|
|
anonymous from Finland
|
|
|
|
How do you use this, if there is also file? (input type=file)
|
|
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.
|
|
[hidden] from Costa Rica
|
 |
|
|
Excelente!
It Works
|
|
anonymous from China
|
 |
|
|
very good
|
|
anonymous from Russian Federation
|
 |
|
|
I am getting an error 'Undeclared Idntifier: HTTPEncode'.
help
|
|
jrdudynho@gmail.com from Brazil
|
 |
|
|
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.
|
|
Omair iqbal from Pakistan
|
|
|
|
excelent answer
|
| You are on page 1 of 2, other pages: [1] 2 |
|