gpt4 book ai didi

web-services - Delphi 2007 SOAP 故障处理

转载 作者:行者123 更新时间:2023-12-03 15:54:18 25 4
gpt4 key购买 nike

我正在 Delphi 2007 中编写一个 SOAP 客户端来执行简单的海关放行检查。我向 SOAP 服务器发送一些信息,如果服务器无法找到我发送的信息,我应该收到有关海关放行的详细信息或 SOAP 错误。第一部分工作正常,但故障处理却不行。 WSDL 指定自定义 SOAP 异常(这包含在主 WSDL 中 - 未显示整个 WSDL):

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsd:schema targetNamespace="http://trips.crownagents.com/wsexception/message"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://trips.crownagents.com/wsexception/message">
<xsd:element name="WSException" type="WSException" nillable="true"/>
<xsd:complexType name="WSException">
<xsd:sequence>
<xsd:element name="ErrorCode" type="xsd:string" minOccurs="0" maxOccurs="1"/>
<xsd:element name="ErrorDescription" type="xsd:string" minOccurs="0" maxOccurs="1"/>
<xsd:element name="Stack" type="xsd:string" minOccurs="0" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>

我得到的 SOAP 响应似乎引用了异常:

<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns: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:ns0="http://trips.crownagents.com/wsexception/message"
xmlns:ns1="http://trips.crownagents.com/external/customs/release/message"
xmlns:ns2="http://trips.crownagents.com/external/common/message">
<env:Body>
<env:Fault xsi:type="env:Fault">
<faultcode>env:Server</faultcode>
<faultstring xsi:nil="1"/>
<detail>
<ans1:WSExceptionResponse xmlns:ans1="http://msgsvr.trips.crownagents.com/">
<ErrorCode>0002</ErrorCode>
<ErrorDescription>Invalid Declaration</ErrorDescription>
<Stack>getSingleResult() did not retrieve any entities.</Stack>
</ans1:WSExceptionResponse>
</detail>
</env:Fault>
</env:Body>
</env:Envelope>

但是,我的代码从未看到 WSExceptionResponse。相反,我得到了一个通用的 ERemotableException:

Try
Res := Rel.releaseStatus(RelInfo);
Except
On E: WSExceptionResponse Do // This never fires
Status('Release check error (' + E.ErrorCode + ' - ' +
E.ErrorDescription + ').', True);
Else
Status('Release check error (' + Exception(ExceptObject).Message +
').', True);
End;

我读到 Delphi 2007 中的 SOAP 处理存在一些问题 ( https://groups.google.com/forum/#!msg/borland.public.delphi.webservices.soap/71t3P-vPMbk/qw9JVTEVS3YJ ),并且我已更改 OPToSOAPDomConv.pas 文件以按照建议恢复它,但这没有帮助。有人知道我可能做错了什么吗?

最佳答案

对于仍在使用 Delphi 2007 并遇到此问题的其他人,这就是我解决此问题的方法。

首先,将 OPToSOAPDomConv.pas 和 InvokeRegistry.pas 从 Delphi 源目录 (\Program Files< (x86)>\CodeGear\RAD Studio\5.0\source\Win32\soap) 复制到项目目录。将这两个文件添加到您的项目中,因为您将自定义源代码,并且您将需要这些文件与您的项目一起重新编译,而不是使用 Delphi 附带的预编译 DCU。

在 OPToSOAPDomConv.pas 文件中,找到 ProcessFault 过程并将其替换为以下内容:

procedure TOPToSoapDomConvert.ProcessFault(FaultNode: IXMLNode);
var
FA, FC, FD, FS, CustNode: IXMLNode;
I, J: Integer;
AMessage: WideString;
AClass: TClass;
URI, TypeName: WideString;
Count: Integer;
PropList: PPropList;
Ex: ERemotableException;

function GetNodeURIAndName(const Node: IXMLNode; var TypeURI,
ElemName: InvString): boolean;
var
Pre: InvString;
begin
ElemName := Node.NodeName;
if IsPrefixed(ElemName) then
begin
Pre := ExtractPrefix(ElemName);
ElemName := ExtractLocalName(ElemName);
TypeURI := Node.FindNamespaceURI(Pre);
end
else
TypeURI := Node.NamespaceURI;
Result := True;
end;

begin
FA := nil;
FC := nil;
FD := nil;
FS := nil;
Ex := nil;
for I := 0 to FaultNode.ChildNodes.Count - 1 do
begin
if SameText(ExtractLocalName(FaultNode.ChildNodes[I].NodeName), SSoapFaultCode) then
FC := FaultNode.ChildNodes[I]
else if SameText(ExtractLocalName(FaultNode.ChildNodes[I].NodeName), SSoapFaultString) then
FS := FaultNode.ChildNodes[I]
else if SameText(ExtractLocalName(FaultNode.ChildNodes[I].NodeName), SSoapFaultDetails) then
FD := FaultNode.ChildNodes[I]
else if SameText(ExtractLocalName(FaultNode.ChildNodes[I].NodeName), SSoapFaultActor) then
FA := FaultNode.ChildNodes[I];
end;

{ Retrieve message from FaultString node }
if FS <> nil then
AMessage := FS.Text;

{ If there's a <detail> node, try to map it to a registered type }
if FD <> nil then
begin
{ Some SOAP stacks, including Delphi6 and others (see
http://softwaredev.earthweb.com/script/article/0,,12063_641361_2,00.html)
use the approach of putting custom fault info right at the <detail> node:

Listing 4 - Application Fault Details
<SOAP-ENV:Fault>
<faultcode>300</faultcode>
<faultstring>Invalid Request</faultstring>
<runcode>1</runcode>
<detail xmlns:e="GetTemperatureErr-URI"
xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
xsi:type="e:GetTemperatureFault">
<number>5575910</number>
<description>Sensor Failure</description>
<file>GetTemperatureClass.cpp</file>
<line>481</line>
</detail>
</SOAP-ENV:Fault>

However, much more common is the approach where the type and namespace
are on the childnode of the <detail> node. Apache, MS and the SOAP spec.
seem to lean towards that approach:

Example 10 from the SOAP 1.1 Spec:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<SOAP-ENV:Fault>
<faultcode>SOAP-ENV:Server</faultcode>
<faultstring>Server Error</faultstring>
<detail>
<e:myfaultdetails xmlns:e="Some-URI">
<message>My application didn't work</message>
<errorcode>1001</errorcode>
</e:myfaultdetails>
</detail>
</SOAP-ENV:Fault>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

For interop reasons we favor the later approach but we'll support both here!!
}
CustNode := nil;
if GetElementType(FD, URI, TypeName) then
CustNode := FD
else
begin
if ntElementChildCount(FD) > 0 then
begin
CustNode := ntElementChild(FD, 0);
if not GetElementType(CustNode, URI, TypeName) and
not GetNodeURIAndName(CustNode, URI, TypeName) then
CustNode := nil;
end;
end;

if (CustNode <> nil) then
begin
AClass := RemClassRegistry.URIToClass(URI, TypeName);
if AClass <> nil then
begin
if AClass.InheritsFrom(ERemotableException) then
begin
Ex := ERemotableExceptionClass(AClass).Create(AMessage);
LoadObject(Ex, FaultNode, CustNode);
end;
end;
end;
end;

{ Create default SOAP invocation exception if no suitable class was found }
if Ex = nil then
Ex := ERemotableException.Create(AMessage);
if FA <> nil then
Ex.FaultActor := FA.Text;
if FC <> nil then
Ex.FaultCode := FC.Text;
if FD <> nil then
Ex.FaultDetail := FD.XML;
raise Ex;
end;

接下来,找到 GetElementType 函数并将其替换为以下内容:

function TSOAPDomConv.GetElementType(Node: IXMLNode; var TypeURI, TypeName: InvString): Boolean;
var
Idx: Integer;
S : InvString;
V: Variant;
Pre: InvString;
begin
TypeURI := '';
TypeName := '';
Result := False;
if (Node.NamespaceURI = SSoap11EncodingS5) and
(Node.LocalName = SSoapEncodingArray) then
begin
TypeURI := SSoap11EncodingS5;
TypeName := SSoapEncodingArray;
Result := True;
end
else
begin
V := GetTypeBySchemaNS(Node, XMLSchemaInstNameSpace);
if VarIsNull(V) then
V := Node.GetAttribute(SSoapType);
if not VarIsNull(V) then
begin
S := V;
Idx := Pos(':', S); { do not localize }
if Idx <> 0 then
begin
TypeName := Copy(S, Idx + 1, High(Integer));
Pre := Copy(S, 1, Idx - 1);
TypeURI := Node.FindNamespaceURI(Pre);
end
else
begin
TypeName := S;
TypeURI := '';
end;
Result := True;
end;
end
end;

最后,打开InvokeRegistry.pas文件并找到GetExternalPropName函数。更改显示以下内容的行:

if Info.Kind = tkClass then

对此:

if (Info.Kind = tkClass) and Assigned(GetTypeData(info).ParentInfo) then

编译并运行您的应用程序,您应该可以了。

所有功劳都归于该线程中的用户 http://www.codenewsfast.com/cnf/article/859054074/permalink.art-ng1920q2368还有这个http://www.delphigroups.info/2/7/342954.html .

关于web-services - Delphi 2007 SOAP 故障处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28091823/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com