Delphi下使用Webservice的用户验证
在Delphi下使用Webservice时,通常我们使用Delphi自带的功能对webservice接口生成调用pas,其中对用户名密码的设置是通过对THTTPRIO对象的HTTPWebNode属性进行设置,
RIO := THTTPRIO.Create(nil);
try
RIO.WSDLLocation := sWSDLLocation;
RIO.Service := sService;
RIO.Port := nPort;
RIO.HTTPWebNode.UserName := UserName;
RIO.HTTPWebNode.Password := Password;
Result := (RIO as WebService);
finally
RIO.Free;
end;
以服务器为java为例,webservice处理类可以通过MessageContext获取到用户名密码。
另外有一种更灵活方法,客户端可使用soapheader来传输用户名密码,当然也可以传输更多灵活的数据,服务器再对header进行分析。
Delphi在这里的实现,需要做如下步骤
1 定义一个专用于存储header数据的类,
2 注册此类
3 在调用的时候创建该类对象,并给属性赋值。
创建如下类
AuthHeader = class(TSOAPHeader)
private
FUserName: string;
FPassword: string;
public
published
property UserName: string read FUserName write FUserName;
property Password: string read FPassword write FPassword;
end;
在initialization段增加如下注册语句
InvRegistry.RegisterHeaderClass(TypeInfo(WebService), AuthHeader, 'AuthHeader', 'urn:WebServiceIntf');
RemClassRegistry.RegisterXSClass(AuthHeader, 'urn:WebServiceIntf', 'AuthHeader');
在调用的时候加入如下语句
auth.UserName := UserName;
auth.Password := Password;
(rio as ISoapHeaders).Send(auth); //rio是THTTPRIO对象
即可在请求的soapheader中加入AuthHeader类的两个属性,即用户名和密码。
可以看到最终生成的soap如下
<?xml version="1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Header SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:NS1="urn:WebServiceIntf">
<NS1:AuthHeader xsi:type="NS1:AuthHeader">
<UserName xsi:type="xsd:string">username</UserName>
<Password xsi:type="xsd:string">password</Password>
</NS1:AuthHeader>
</SOAP-ENV:Header>
<SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
……
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
可以看到UserName和Password作为NS1:AuthHeader的两个子节点出现。
在服务器段分析header即可获得内容。
RIO := THTTPRIO.Create(nil);
try
RIO.WSDLLocation := sWSDLLocation;
RIO.Service := sService;
RIO.Port := nPort;
RIO.HTTPWebNode.UserName := UserName;
RIO.HTTPWebNode.Password := Password;
Result := (RIO as WebService);
finally
RIO.Free;
end;
以服务器为java为例,webservice处理类可以通过MessageContext获取到用户名密码。
另外有一种更灵活方法,客户端可使用soapheader来传输用户名密码,当然也可以传输更多灵活的数据,服务器再对header进行分析。
Delphi在这里的实现,需要做如下步骤
1 定义一个专用于存储header数据的类,
2 注册此类
3 在调用的时候创建该类对象,并给属性赋值。
创建如下类
AuthHeader = class(TSOAPHeader)
private
FUserName: string;
FPassword: string;
public
published
property UserName: string read FUserName write FUserName;
property Password: string read FPassword write FPassword;
end;
在initialization段增加如下注册语句
InvRegistry.RegisterHeaderClass(TypeInfo(WebService), AuthHeader, 'AuthHeader', 'urn:WebServiceIntf');
RemClassRegistry.RegisterXSClass(AuthHeader, 'urn:WebServiceIntf', 'AuthHeader');
在调用的时候加入如下语句
auth.UserName := UserName;
auth.Password := Password;
(rio as ISoapHeaders).Send(auth); //rio是THTTPRIO对象
即可在请求的soapheader中加入AuthHeader类的两个属性,即用户名和密码。
可以看到最终生成的soap如下
<?xml version="1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Header SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:NS1="urn:WebServiceIntf">
<NS1:AuthHeader xsi:type="NS1:AuthHeader">
<UserName xsi:type="xsd:string">username</UserName>
<Password xsi:type="xsd:string">password</Password>
</NS1:AuthHeader>
</SOAP-ENV:Header>
<SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
……
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
可以看到UserName和Password作为NS1:AuthHeader的两个子节点出现。
在服务器段分析header即可获得内容。