Смекни!
smekni.com

Технологія SOAP (стр. 2 из 2)

<partname="Value" type="xs:double" />

</message>

-<message name="UkToEuro1Response">

<partname="return" type="xs:double" />

</message>

-<portType name="IEncodeDecode">

-<operation name="EuroToUk">

<inputmessage="tns:EuroToUk0Request" />

<outputmessage="tns:EuroToUk0Response" />

</operation>

-<operation name="UkToEuro">

<inputmessage="tns:UkToEuro1Request" />

<outputmessage="tns:UkToEuro1Response" />

</operation>

</portType>

-<binding name="IEncodeDecodebinding" type="tns:IEncodeDecode">

<soap:bindingstyle="rpc" transport="http://schemas.xmlsoap.org/soap/http" />

-<operation name="EuroToUk">

<soap:operationsoapAction="urn:u_Intrf-IEncodeDecode#EuroToUk" style="rpc" />

-<input message="tns:EuroToUk0Request">

<soap:bodyuse="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:u_Intrf-IEncodeDecode" />

</input>

-<output message="tns:EuroToUk0Response">

<soap:bodyuse="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:u_Intrf-IEncodeDecode" />

</output>

</operation>

-<operation name="UkToEuro">

<soap:operationsoapAction="urn:u_Intrf-IEncodeDecode#UkToEuro" style="rpc" />

-<input message="tns:UkToEuro1Request">

<soap:bodyuse="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:u_Intrf-IEncodeDecode" />

</input>

-<output message="tns:UkToEuro1Response">

<soap:bodyuse="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:u_Intrf-IEncodeDecode" />

</output>

</operation>

</binding>

-<service name="IEncodeDecodeservice">

-<port name="IEncodeDecodePort" binding="tns:IEncodeDecodebinding">

<soap:addresslocation="http://localhost/Scripts/SOAPServerProject.exe/soap/IEncodeDecode" />

</port>

</service>

</definitions>

Збережіть його у файлі IEncodeDecode.xml. Отже, тим або іншим способом файл з описом у форматі XML опинився у нас в руках, тепер необхідно експортувати його в Delphi. При експорті Delphi згенерує модуль з описом інтерфейсу на мові Object Pascal. Оберімо команду меню File | New | Other, перейдемо на закладку WebServices і оберімо ікону WSDL Importer. При цьому на екрані з'явиться діалог імпорту опису (мал. 4.8).

Використовуючи кнопку … діалогу, вкажемо одержаний раніше файл SOAPClient.xml, і натиснемо кнопку Finish. Модуль Delphi з описом інтерфейсу готовий.


Малюнок 8

// ************************************************************************ //

// The types declared in this file were generated from data read from the

// WSDL File described below:

// WSDL : D:&bsol;SOAP&bsol;SOAPClient&bsol;IEncodeDecode.xml

// Encoding : utf-8

// Version : 1.0

// (05.02.2006 10:06:04 - 1.33.2.5)

// ************************************************************************ //

unit IEncodeDecode1;

interface

uses InvokeRegistry, SOAPHTTPClient, Types, XSBuiltIns;

type

// ************************************************************************ //

// The following types, referred to in the WSDL document are not being represented

// in this file. They are either aliases[@] of other types represented or were referred

// to but never[!] declared in the document. The types from the latter category

// typically map to predefined/known XML or Borland types; however, they could also

// indicate incorrect WSDL documents that failed to declare or import a schema type.

// ************************************************************************ //

// !:double - "http://www.w3.org/2001/XMLSchema"

// ************************************************************************ //

// Namespace : urn:u_Intrf-IEncodeDecode

// soapAction: urn:u_Intrf-IEncodeDecode#%operationName%

// transport : http://schemas.xmlsoap.org/soap/http

// style : rpc

// binding : IEncodeDecodebinding

// service : IEncodeDecodeservice

// port : IEncodeDecodePort

// URL : http://localhost/Scripts/SOAPServerProject.exe/soap/IEncodeDecode

// ************************************************************************ //

IEncodeDecode = interface(IInvokable)

['{2F701C83-3E4D-2403-7EA6-5BC2C987131C}']

function EuroToUk(const Value: Double): Double; stdcall;

function UkToEuro(const Value: Double): Double; stdcall;

end;

function GetIEncodeDecode(UseWSDL: Boolean=System.False; Addr: string=''; HTTPRIO: THTTPRIO = nil): IEncodeDecode;

implementation

function GetIEncodeDecode(UseWSDL: Boolean; Addr: string; HTTPRIO: THTTPRIO): IEncodeDecode;

const

defWSDL = 'D:&bsol;SOAP&bsol;SOAPClient&bsol;IEncodeDecode.xml';

defURL = 'http://localhost/Scripts/SOAPServerProject.exe/soap/IEncodeDecode';

defSvc = 'IEncodeDecodeservice';

defPrt = 'IEncodeDecodePort';

var

RIO: THTTPRIO;

begin

Result := nil;

if (Addr = '') then

begin

if UseWSDL then

Addr := defWSDL

else

Addr := defURL;

end;

if HTTPRIO = nil then

RIO := THTTPRIO.Create(nil)

else

RIO := HTTPRIO;

try

Result := (RIO as IEncodeDecode);

if UseWSDL then

begin

RIO.WSDLLocation := Addr;

RIO.Service := defSvc;

RIO.Port := defPrt;

end else

RIO.URL := Addr;

finally

if (Result = nil) and (HTTPRIO = nil) then

RIO.Free;

end;

end;

initialization

InvRegistry.RegisterInterface(TypeInfo(IEncodeDecode), 'urn:u_Intrf-IEncodeDecode', 'utf-8');

InvRegistry.RegisterDefaultSOAPAction(TypeInfo(IEncodeDecode), 'urn:u_Intrf-IEncodeDecode#%operationName%');

end.

Переходимо до другого етапу - безпосередньому створенню клієнта. Створимо заготівку нового додатку командою File |New | Application. На головній формі розташуймо рядок введення, дві кнопки і компонент HTTPRIO із закладки WebServices (мал. 9).


Малюнок 4.9

Компонент HTTPRIO призначений для виклику серверів через SOAP. Вкажемо у властивості URL значення http://localhost/Scripts/SOAPServerProject.exe/soap/IEncodeDecode, - шлях до сервера. Далі включимо в проект модуль Delphi з описом інтерфейсу сервера і вкажемо його в секції uses головної форми проекту.

Тепер можна переходити до написання коду виклику методів сервера. Оброблювачі подій натиснення на кнопки UkToEuro і EuroToUk виглядатимуть так:

procedure TForm1.UkToEuroClick(Sender: TObject);

var

X:IEncodeDecode;

R:Currency;

begin

X := HTTPRIO1 as IEncodeDecode;

R := X.UkToEuro(StrToCurr(Summa.Text));

ShowMessage(CurrToStr(R)+' €');

end;

procedure TForm1.EuroToUkClick(Sender: TObject);

var

X:IEncodeDecode;

R:Currency;

begin

X := HTTPRIO1 as IEncodeDecode;

R := X.EuroToUk(StrToCurr(Summa.Text));

ShowMessage(CurrToStr(R)+'Грн');

end;

Залишилося запустити проект на виконання і переконатися в його працездатності (мал. 10).

Малюнок 10