您的位置:首页 > 编程语言 > Delphi

使用Delphi 通过WebServices 要想实现到.Net 或者是Java的WebServices 头验证.

2009-11-24 09:25 756 查看
转http://hi.baidu.com/chendongzhou/blog/item/ae90d301036a8cd1267fb511.html

在使用Delphi 通过WebServices 要想实现到.Net 或者是Java的WebServices 头验证. 信信有多朋友都试过就是难以实现. 其实可可能是它们之间使用的WebServices XML数据包不一样的格式导致而已. 要想用delphi . 以下是一比较土的办法.就是直接修改Delphi 的 ISOAPHeaders 类,使之产生的内容符合我们想要的Header 结构.
这个可以用过其它式(如SOAP Toolkit )取得发送时的SOAP XML 请求结构. 把正常的请求内容取下来.最终我们把这些内容的结构修改为Delphi 直接赋值的结内容.

以下是我们对ISOAPHeaders 进行修改.

OPToSOAPDomConv:pas 把 WriteHeader 方法改一下.改为我们想要的内容,并取消原有的内容


procedure TSoapDomConv.WriteHeader(const Header: TObject; RootNode, ParentNode: IXMLNode);
var
HeadChild: IXMLNode;
begin

if (Header is TStringList) then
Begin
//修改部份内容
ParentNode:= RootNode.AddChild('SOAP-ENV:Header');
HeadChild:= ParentNode.AddChild('ns1:sessionId');
HeadChild.Attributes['SOAP-ENV:actor']:='http://schemas.xmlsoap.org/soap/actor/next';
HeadChild.Attributes['SOAP-ENV:mustUnderstand']:='0';
HeadChild.Attributes['xmlns:ns1']:='http://10.248.112.23/mlp/ws/header';
HeadChild.Attributes['xmlns:soapenc']:='http://schemas.xmlsoap.org/soap/encoding/';
HeadChild.NodeValue:=(Header as TStringList).Strings[0];
end else
begin
//modify by Oscar,chen header create by TStringList line by line
//以下内容为原始内容
Options := Options + [soXXXXHdr];
try
ConvertNativeDataToSoap(RootNode, ParentNode,
Header.ClassName,
Header.ClassInfo,
Header, 0);
finally
Options := Options - [soXXXXHdr];
end;
end;
end;

及828行的 InvContextToMsg 方法

function TOPToSoapDomConvert.InvContextToMsg(const IntfMD: TIntfMetaData; MethNum: Integer;
Con: TInvContext; Headers: THeaderList): TStream;
var
XMLDoc: IXMLDocument;
EnvNode, HeaderNode, BodyNode, MethNode: IXMLNode;
I: Integer;
SoapMethNS: InvString;
MethMD: TIntfMethEntry;
P: Pointer;
Indir: Integer;
URI, ExtMethName, ExtParamName: InvString;
Header: TObject;
begin
MethMD := IntfMD.MDA[MethNum];

{ Here we update the WSDLView to inform it of
the Operation we're about to execute }
if Assigned(WSDLView) then
begin
WSDLView.Operation := MethMD.Name;
WSDLView.IntfInfo := IntfMD.Info;
end;

XMLDoc := NewXMLDocument;
XMLDoc.Encoding := FEncoding;
EnvNode := Envelope.MakeEnvelope(XMLDoc, Options);

{ Result MultiRef IDs are we're about to create new request }
FIDS := 1;
FAttachments.Clear;

{ Any headers }
if (Headers <> nil) and (Headers.Count > 0) then
begin

//生成特定的XML格式
if not (Headers[0] is TStringList) then
begin
HeaderNode := Envelope.MakeHeader(EnvNode);
if not (soDocument in Options) then
HeaderNode.SetAttributeNS(SSoapEncodingAttr, SSoapNameSpace, SSoap11EncodingS5);
end;
//if修改结束

for I := 0 to Headers.Count-1 do
begin
Header := Headers[I];
WriteHeader(Header, EnvNode, HeaderNode);
end;
end;

BodyNode := Envelope.MakeBody(EnvNode);

{ If we're sending literal params, then skip the method node }
if not (soLiteralParams in Options) then
begin
SoapMethNS := GetSoapNS(IntfMD);
{ Add Method node with appropriate namespace }
ExtMethName := InvRegistry.GetMethExternalName(IntfMD.Info, MethMD.Name);
if not (soDocument in Options) then
begin
MethNode := BodyNode.AddChild(ExtMethName, SoapMethNS, (SoapMethNS <> ''));
{ Use encoding style defined by SOAP 1.1 section 5 }
{ NOTE: We used to put this on the method node; it seems more intuitive on
the body node; Keep this in mind when investigating interop issues }
BodyNode.SetAttributeNS(SSoapEncodingAttr, SSoapNameSpace, SSoap11EncodingS5);
end
else
begin
{ In document mode, SoapMethNS is the default namespace }
MethNode := BodyNode.AddChild(ExtMethName, SoapMethNS);
end;
end
else
begin
MethNode := BodyNode;
end;

try
{ Add each parameter to the method node }
for I := 0 to MethMD.ParamCount - 1 do
begin
if not (pfOut in MethMD.Params[I].Flags) then
begin
{ In doc|lit mode, we use the typename for the node }
if (soDocument in Options) and (soLiteralParams in Options) then
RemTypeRegistry.TypeInfoToXSD(MethMD.Params[I].Info, URI, ExtParamName)
else
ExtParamName := InvRegistry.GetParamExternalName(IntfMD.Info, MethMD.Name, MethMD.Params[I].Name);
P := Con.GetParamPointer(I);
Indir := 1;
if IsParamByRef(MethMd.Params[I].Flags, MethMD.Params[I].Info, MethMD.CC) then
Inc(Indir);
ConvertNativeDataToSoap(BodyNode, MethNode, ExtParamName,
MethMD.Params[I].Info, P, Indir);
end;
end;
FinalizeMultiRefNodes;
finally
ResetMultiRef;
end;

Result := TMemoryStream.Create();
DOMToStream(XMLDoc, Result);
end;

InvokeRegistry.pas 单元. 对TSOAPHeaders.Send 加上自已特有的方法,把它赋过去的内容能动太生成你想要的用户户及密码. 最后在工程里引用相应的路,编译即可. 当然以上只是我的办法, 你也可以按同样的原理做出合符你的应用.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐