Title: Delphi7 webbrowser + struts2 action
Author: jrq
Body:
Use the Delphi7 webbrowser client to call the struts2 action on the server and pass relevant parameters to the server.
It is a test of simple integration for heterogeneous systems.
Brief note.
1. The client webbrowser calls action in http post mode and passes Parameters
Client POST method 1:
Procedure webnavigate1 (Surl, spostdata: string; var wbwebbrowser: twebbrowser );
VaR
Vwebaddr, vpostdata, vflags, vframe, vheaders: olevariant;
I: integer;
Begin
If length (spostdata)> 0 then
Begin
// Header information
Vheaders: = 'content-type: Application/X-WWW-form-urlencoded' + #10 #13 #0;
// Create binary parameters
Vpostdata: = vararraycreate ([0, length (spostdata)], varbyte );
For I: = 0 to length (spostdata)-1 do
Begin
Vpostdata [I]: = ord (spostdata [I + 1]);
End;
// End character
Vpostdata [length (spostdata)]: = 0;
// Set the type of Variant
// Tvardata (vpostdata). vtype: = vararray; // comment this sentence. Otherwise, the type is incorrect.
End;
Vwebaddr: = Surl;
Wbwebbrowser. navigate2 (vwebaddr, vflags, vframe, vpostdata, vheaders );
// Wbwebbrowser. navigate (vwebaddr, emptyparam, emptyparam, vpostdata, vheaders );
End;
Client POST method 2:
Procedure webnavigate2 (Surl, spostdata: string; var wbwebbrowser: twebbrowser );
VaR postdata, headers: olevariant;
I: integer;
Begin
// Create a variable array in byte mode and convert the string.
Postdata: = vararraycreate ([0, length (spostdata)-1], varbyte );
For I: = 1 to length (spostdata) Do
Begin
Postdata [I-1]: = ord (spostdata [I]);
End;
// Header type
Headers: = 'content-type: Application/X-WWW-form-urlencoded' + #10 #13;
// Submit
Wbwebbrowser. navigate (Surl, emptyparam, emptyparam, postdata, headers );
End;
Webbrowser client call method:
Uses httpapp;
VaR aurl, apostdata: string;
Begin
// POST method 1
Aurl: = 'HTTP: // FIG: 8088/test/loginaction. action ';
Apostdata: = 'usercode = ABC & Password = abc ';
// Apostdata: = 'usercode = '+ httpencode ('username') +' & Password = abc ';
Webnavigate1 (aurl, apostdata, webbrowser1 );
// Method 2 of post
Aurl: = 'HTTP: // FIG: 8088/test/testaction. action ';
Apostdata: = 'paracode = '+ httpencode ('test') +' & paranumb = 100 ';
Webnavigate2 (aurl, apostdata, webbrowser1 );
End;
The server action obtains the following parameters:
String pusercode = request. getparameter ("usercode"). tostring ();
String ppassword = request. getparameter ("password"). tostring ();
String pparacode = request. getparameter ("paracode"). tostring ();
String pparanumb = request. getparameter ("paranumb"). tostring ();
Note:
Twebbrowser has multiple overloaded Methods navigate (). The prototype of the navigate () method used here is as follows:
Procedure twebbrowser. navigate (const URL: widestring; var flags: olevariant;
VaR targetframename: olevariant; var postdata: olevariant;
VaR headers: olevariant );
Begin
Defaultinterface. navigate (URL, flags, targetframename, postdata, headers );
End;
The help for navigate () is described as follows:
-----------------------
Use navigate to locate and download a specific resource.
Navigate can send an HTTP message to a specified URL and display the results or simply display the results of a specified file.
Postdata contains the data sent to the server when using navigate to generate an http post message.
If postdata is null, navigate generates an http get message.
Postdata is ignored if URL does not specify an http url.
-----------------------
If the navigate () method uses the postdata parameter, It is http post. If the postdata parameter is not set, the http get method is used.
The parameters passed in post mode must be binary encoded on the client. HTTP header information is configured.
In addition, if the postdata parameter is passed in Chinese, the client needs to use the uses httpapp unit, use httpencode () to encode the Chinese parameter, and then perform binary encoding.
The client can capture post parameters in the beforenavigate2 () event of webbrowser, as shown below:
procedure tform1.webbrowser1beforenavigate2 (Sender: tobject;
const Pdisp: idispatch; var URL, flags, targetframename, postdata,
headers: olevariant; var cancel: wordbool);
var
I, nlen: integer;
vpostdata: olevariant;
szdata: string;
begin
// unlock the postdata parameter
vpostdata: = postdata;
If varisarray (vpostdata, true) Then
begin
nlen: = vararrayhighbound (vpostdata, 1)-vararraylowbound (vpostdata, 1) + 1;
setlength (szdata, nlen);
for I: = 0 to nlen-1 do
begin
szdata [I + 1]: = CHR (byte (vpostdata [vararraylowbound (vpostdata, 1) + I]);
end;
Application. MessageBox (pchar ('postdata is: '+ szdata), 'test ');
End;
End;
2. The client webbrowser calls the action in http get mode and passes Parameters
The client webbrowser sends a request in get mode:
Uses httpapp;
VaR aurl: string;
Begin
Aurl: = 'http: // 127.0.0.1: 8088/test/loginaction. Action? Usercode = ABC & Password = ABC ';
Webbrowser1.navigate (aurl );
Aurl: = 'http: // 127.0.0.1: 8088/test/testaction. Action? Paracode = '+ httpencode ('test') +' & paranumb = 100 ';
Webbrowser1.navigate (aurl );
End;
The server action obtains the following parameters:
String pusercode = request. getparameter ("usercode"). tostring ();
String ppassword = request. getparameter ("password"). tostring ();
String pparacode = new string (request. getparameter ("paracode"). getbytes ("iso8859_1"); // encode and convert Chinese Parameters
String pparanumb = request. getparameter ("paranumb"). tostring ();
Note:
The webbrowser method of the client uses only one parameter: URL. This URL contains the parameters submitted to the server action.
The postdata parameter is not used in the navigate () method, and http get requests are used.
The prototype of the navigate () method used by the client webbrowser is as follows:
Procedure twebbrowser. navigate (const URL: widestring );
Begin
Defaultinterface. navigate (URL, emptyparam, emptyparam );
End;
If the URL contains a Chinese parameter, you must use the httpencode () function to encode the Chinese parameter.
In addition, the server action needs to encode and convert the parameter.
3. Some Ideas
1. Security:
In this case, post is safer than get. Because the POST method does not expose the passed parameter content in the URL.
2. About System Integration:
This case can be applied to the integration of multiple heterogeneous systems, including single sign-on implementation and Remote Desktop integrated access. Pai_^
[-- End --]
By jrq
2009/05/23 Yu Jing