Web服务 – 在Delphi中的SOAP Header中发送简单的字符串

前端之家收集整理的这篇文章主要介绍了Web服务 – 在Delphi中的SOAP Header中发送简单的字符串前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要发送这样的东西:
<soapenv:Header>
      <ser:userName>admin</ser:userName>
      <ser:userPassword>secret</ser:userPassword>
   </soapenv:Header>

Delphi WSDL导入程序,生成此:

userName2 = class(TSOAPHeader)
  private
    FValue: string;
  published
    property Value: string  read FValue write FValue;
  end;

  userName      =  type string;

  WsService = interface(IInvokable)
    function call(const userName: userName; const userPassword: userPassword);

并将其类型注册为:

InvRegistry.RegisterHeaderClass(TypeInfo(WsService),userName2,'userName','http://localhost/path/to/services');

问题是当我使用delphi生成代码调用它时,将userName和password放在SOAP消息的Body部分中,而不是在Header中.

所以我试着自己发送标题,就像这样:

将类型定义更改为继承自userName2类,因为我无法使用ISOAPHeaders.Send()方法发送字符串.

userName = class(userName2);

然后发送标题

user := userName.Create;
user.Value := 'admin';

WS := GetWsService;
(WS as ISOAPHeaders).Send(user);

现在头文件在正确的位置,但是它们是这样发送的:

<SOAP-ENV:Header>
    <NS1:userName xmlns:NS1="http://localhost/path/to/services">
        <Value xmlns="http://localhost/path/to/services">admin</Value>
    </NS1:userName>
</SOAP-ENV:Header>

几乎在那里,但是我不想要“Value”属性,我只想在标题中使用一个简单的简单标签.

我该怎么做?

谢谢.

== EDIT ==

根据要求,WSDL在这里:http://desenvolvimento.lemontech.com.br:8081/wsselfbooking/WsSelfBookingService?wsdl

SOAP UI导入并生成此示例请求:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://lemontech.com.br/selfbooking/wsselfbooking/services">
   <soapenv:Header>
      <ser:userPassword></ser:userPassword>
      <ser:userName></ser:userName>
      <ser:keyClient></ser:keyClient>
   </soapenv:Header>
   <soapenv:Body>
      <ser:pesquisarSolicitacao>
         <!--You have a CHOICE of the next 2 items at this level-->
         <idSolicitacaoRef></idSolicitacaoRef>
         <dataInicial></dataInicial>
         <dataFinal></dataFinal>
         <registroInicial>1</registroInicial>
         <!--Optional:-->
         <quantidadeRegistros>50</quantidadeRegistros>
      </ser:pesquisarSolicitacao>
   </soapenv:Body>
</soapenv:Envelope>

这个示例请求工作正常,但是我无法弄清楚如何在Delphi中进行调用.

解决方法

解:

这不是很明显,但是我只需要将IS_TEXT值添加到索引并声明一个新的TSOAPHeader后代,解决方案就是这样:

const
  IS_TEXT = $0020;

type
  TSimpleHeader = class(TSOAPHeader)
  private
    FValue: string;
  published
    property Value: string Index (IS_TEXT) read FValue write FValue;
  end;

  userName = class(TSimpleHeader);

然后注册标题

InvRegistry.RegisterHeaderClass(TypeInfo(WsService),userName,'http://localhost/path/to/services');

并手动发送标题

User := userName.Create;
    User.Value := 'username';

    (WS as ISOAPHeaders).Send(User);

基本上,索引中的IS_TEXT值可防止Delphi在其中创建一个userName标签和一个Value标签.它只是将Value属性的字符串放在userName标签中.

令人遗憾的是,索引的关键工作被用于不明显的东西,关于它的文档很难找到并且难以理解:

The AS_ATTRIBUTE feature has been deprecated. It still works for legacy code,but the preferred approach is to use the index value of a property. The index property allows you to specify whether a property is an attribute,an unbounded element,an optional element,a text value,or can have a value of NULL.

资料来源:http://docwiki.embarcadero.com/RADStudio/XE3/en/Using_Remotable_Objects

猜你在找的HTML相关文章