【问题标题】:How to set attribute for SOAP elements in Delphi?如何在 Delphi 中为 SOAP 元素设置属性?
【发布时间】:2021-06-14 17:03:21
【问题描述】:

我使用 WSDL 导入器来创建一个类 (Delphi 10.3)。但是在调用 Web 服务方法时出错。在使用 Fiddler 检查调用后,我注意到缺少一个属性。

在 WSDL 中我们有:

  <s:complexType name="ArrayOfAnyType">
    <s:sequence>
      <s:element minOccurs="0" maxOccurs="unbounded" name="anyType" nillable="true" type="s:string" />
    </s:sequence>
  </s:complexType>

工作的 SOAP 信封如下所示:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <FidelioSPMSWSXML xmlns="http://fideliocruise.de/">
      <psFunction>Login</psFunction>
      <poParam>
        <anyType xsi:type="xsd:string">sasa</anyType>
        <anyType xsi:type="xsd:string">F45731E3D39A1B2330BBF93E9B3DE59E</anyType>
      </poParam>
    </FidelioSPMSWSXML>
  </s:Body>
</s:Envelope>

Delphi 像这样创建信封:

<?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">
  <SOAP-ENV:Body>
    <FidelioSPMSWSXML xmlns="http://fideliocruise.de/">
      <psFunction>Login</psFunction>
      <psSessionID/>
      <poParam>
        <anyType>username</anyType>
        <anyType>password</anyType>
      </poParam>
    </FidelioSPMSWSXML>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Delphi 调用不起作用的唯一原因是 anyType 元素中缺少 xsi:type="xsd:string" 。如何强制 Delphi 添加该属性?

【问题讨论】:

  • 看起来像是导入器中的错误。不能修复生成的代码吗?
  • 不,我正在尝试更改 htpr1BeforeExecute 中的 SOAP 信封

标签: delphi


【解决方案1】:

好吧,我发现对我来说最简单的方法是在表单上添加 THTTPRIO 组件并使用它的 onBeforeExecute 事件来更改 SOAPRequest,如下所示:

procedure TForm2.htpr1BeforeExecute(const MethodName: string;
  SOAPRequest: TStream);
var
  sl: TStringList;
begin
  sl:=TStringList.Create;
  try
    SOAPRequest.Position := 0;
    sl.LoadFromStream(SOAPRequest);
    sl.Text := StringReplace(sl.Text,'<anyType>','<anyType xsi:type="xsd:string">',[RfReplaceAll]);
    sl.SaveToFile('SOAPRequest.txt'); //for debug
    SOAPRequest.Position := 0;
    sl.SaveToStream(SOAPRequest);
  finally
    sl.Free;
  end;
end;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 2019-09-24
    相关资源
    最近更新 更多