Originally did WebService, later forgot, today learned a bit, make a note, for later browsing.
WebService is called by the server to provide the interface to the client side. When developing WebService with Delphi, there are several steps:
The following is the DelphiXE3 development of the WebService, the server provides directory files for the client to query, the client will display the selected file, image file as an example
First, the service side
1. In the menu "File", "New" and "Other", "WebService", such as
2. To set up a server model, let's start with the first example, so that you can debug and later change to another format
3. Create an external interface template (Soaptest)
4. Add the following function and implement it in the interface and the class implementing the interface (GetFileNames is to list the PNG files in all directories, GetFile is to pass the file to be displayed to the client)
The code for the interface implementation:
{invokable Implementation File for Tsoaptest which implements Isoaptest}unit usoaptestimpl;interfaceuses Soap.InvokeReg Istry, System.types, Soap.xsbuiltins, usoaptestintf,sysutils,vcl.forms,system.classes,system.ioutils; type { Tsoaptest} tsoaptest = Class (Tinvokableclass, Isoaptest) Private function getserverpath:string; Public Function GetFileNames (out icount:integer): Tstringdynarray;stdcall; function GetFile (const afile:string; out Isize:integer): Tbytedynarray;stdcall; end;implementation{tsoaptest}function tsoaptest.getfile (const afile:string; Out Isize:integer): Tbytedynarray;var mem:tmemorystream; Atmpfile:string;begin isize:=0; SetLength (result,isize); Atmpfile:=getserverpath+afile; If not fileexists (atmpfile) then raise Exception.create (' Cou ' t find the afile '); Mem:=tmemorystream.create; Try Mem. LoadFromFile (Atmpfile); Isize:=mem. Size; SetLength (result,isize); Move (Mem. Memory^,result[0],isize); Finally Mem. Free; End;end;function Tsoaptest.getfilenames (out Icount:integer): Tstringdynarray;var i:integer;begin result:= TDirectory.GetFiles ( Getserverpath, ' *.png ', tsearchoption.sotopdirectoryonly); Icount:=length (Result); For I: = 0 to Icount-1 do Result[i]:=extractfilename (Result[i]); End;function Tsoaptest.getserverpath:string;begin Res Ult:= '. \imgs\ '; end;initialization{invokable classes must be registered} invregistry.registerinvokableclass (TSoapTest); end.
5. Run the server side, for example, after opening the browser, get the WSDL file
Click on the isoaptest[wsdl] link to get the slice
6. Save the above page and name the file in WSDL format for the client to invoke
Ii. Development of the client
1. Build a common application and put the Httprio component on the interface to invoke the server-side interface function. For example, the listbox displays the client's file name, and the image to the right for the point selection.
2. In the menu "Component->import WSDL" opens an interface, select the WSDL file you just saved in the location to generate the interface unit (remember to open the server), and save to the client, let the client reference
3. Write the following code in the client
Unit umain;interfaceuses winapi.windows, Winapi.messages, System.sysutils, system.variants, System.Classes, Vcl.graphics, Vcl.controls, Vcl.forms, Vcl.dialogs,vcl.imaging.pngimage, Vcl.extctrls, Vcl.StdCtrls, Soap.invokeregistry, Soap.rio, Soap.soaphttpclient,soaptest;type TForm2 = Class (Tform) Listbox1:tlistbox; Image1:timage; Httprio1:thttprio; Button1:tbutton; Procedure Button1Click (Sender:tobject); Procedure Formcreate (Sender:tobject); Procedure Listbox1click (Sender:tobject); Private {Private declarations} public {public declarations} End;var Form2:tform2;implementation uses System. Types; {$R *.dfm}procedure Tform2.button1click (sender:tobject); var Filesarr:tstringdynarray; I:integer; Str:string;begin listbox1.clear; Filesarr:= (HTTPRIO1 as Isoaptest). GetFileNames (i); Caption:= ' The total Files are: ' +inttostr (i); For STR in Filesarr do ListBox1.Items.Add (str), End;procedure tform2.formcreate (sender:tobject); begin//with Httprio1 do//begin//wsdllocation:= ' soaptest.wsdl ';//service:= ' isoaptestservice ';//port:= ' isoaptestport ';//end; HTTPRIO1. url:= ' http://localhost:8080/soap/'; end;procedure showimg (const i:integer); var Mem:tmemorystream; Bytearr:tbytedynarray; afile:string; Isize:integer;begin Afile:=form2.listbox1.items.strings[i]; Bytearr:= (Form2.httprio1 as Isoaptest). GetFile (afile,isize); Mem:=tmemorystream.create; Try Mem. SetSize (isize); Move (Bytearr[0],mem. Memory^,isize); Mem. SaveToFile (Afile); Form2.Image1.Picture.LoadFromFile (Afile); Finally Mem. Free; End;end;procedure Tform2.listbox1click (sender:tobject); var i:integer;begin i:=listbox1.itemindex; Showimg (i); end;end.
Description
When developing webserver, minimizing network traffic, it is best not to enter the server URL directly in the wsdllocation attribute of Httprio, otherwise it will generate two network loops for Get and post.
The WSDL file saved by the Web page is placed on the client for Httprio to invoke, as the following code, service and port can view the cell files generated on the client, case sensitive. This can be reduced once the network loop, call the other language written webservice can do so.
With HTTPRIO1 do begin wsdllocation:= ' soaptest.wsdl '; service:= ' Isoaptestservice '; port:= ' Isoaptestport '; End
In addition, if it is Delphi write WebService, it is best to write Httprio URL, url and wsdllocation is mutually exclusive, URL calls when soap has been optimized, so relative to the WSDL, the pressure on the network is smaller, if the URL, The service and Port properties are not written. The URL can end with no interface name
The format of the WSDL is: http (s)://hostname: Port/Running Program (virtual directory name)/wsdl/interface name, such as example of Wsdl:http://localhot:8080/wsdl/isoaptest
The format of the URL is: http (s)://hostname: Port/Running Program (virtual directory name)/soap/interface name, such as example of Url:http://localhost:8080/soap/isoaptest, can actually put the last interface name to eliminate, namely Http://localhost:8080/soap
If you are dealing with a database, you can add a SOAP server data module to the service side and put the components to access the database, such as Sqlconnection,sqldataset, The Soapconnection connection component is placed on the client, which is mainly used to connect the data components of the service side. The URL of the soapconnection can be found in the client-generated interface unit, or the last interface name can be omitted.
If you are not correct please correct me.
Some design of WebService