二、 客户端发送Header
我们还假设ISoapAuth是服务端提供的服务接口,它提供了GetInfo()这么一个服务。
客户端程序片段:
procedure TClientForm.GetInfoButtonClick(Sender: TObject);
var
aIntf: ISoapAuth;
Headers: ISOAPHeaders;
H: TAuthHeader;
Begin
aIntf := (HTTPRio as ISoapAuth);
H := TAuthHeader.Create;
H.UserName := ‘piao’ ; //这里只是举个例子
H.PassWord := ‘840717’;
Try
Headers := (aIntf as ISOAPHeaders);
Headers.Send(H); //发送Soap Header
aIntf.GetInfo; //调用服务
finally
aIntf := nil;
H.Free;
End;
end;
客户端的工作就是这些了,能否调用服务还要看服务端的处理结果了。
三、 服务端接收处理Header
服务端程序片段:
function TSoapAuth.GetServerInfo: WideString;
var
Headers: ISoapHeaders;
H: TAuthHeader;
begin
Headers := Self as ISoapHeaders;
Headers.Get(TAuthHeader, TSoapHeader(H)); //先获取SoapHeader
try
if H = nil then //SoapHeader 为空
raise ERemotableException.Create('No authentication header')
else
if not CheckUser(H.UserName, H.PassWord) then //验证失败
raise ERemotableException.Create('No acess to call on service!');
finally
H.Free;
end;
Result := 'Hello World!';
end;
以上,TSoapAuth是继承于TInvokableClass 实现 ISoapAuth 的类。
CheckUser()是用来验证用户是否具有访问权限的函数,在服务端定义。
这只是个简单的返回字符串的服务。