在Delphi中的SOAP Header中发送简单的字符串 [英] Send simple strings in SOAP Header in Delphi

查看:2040
本文介绍了在Delphi中的SOAP Header中发送简单的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要发送这样的东西:

 < soapenv:Header> 
< ser:userName> admin< / ser:userName>
< ser:userPassword> secret< / ser:userPassword>
< / soapenv:标题>

Delphi WSDL导入程序,生成此:

  userName2 = class(TSOAPHeader)
private
FValue:string;
发布
属性值:字符串读取FValue写入FValue;
结束

userName = type string;

WsService = interface(IInvokable)
函数调用(const userName:userName; const userPassword:userPassword);

并将类型注册为:

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

问题是,当我使用delphi生成的代码调用它时,会将userName和密码在SOAP消息的正文部分,而不是标题。



所以我尝试发送标题本身,像这样:



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

  userName = class(userName2); 

然后发送标题:

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

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

现在标题位于正确的位置,但正在发送:

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

几乎在那里,但是我不想要Value属性,我只想要



我该怎么做?



谢谢。



==编辑==



根据请求,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:标题>
< ser:userPassword>< / ser:userPassword>
< ser:userName>< / ser:userName>
< ser:keyClient>< / ser:keyClient>
< / soapenv:标题>
< soapenv:Body>
< ser:pesquisarSolicitacao>
<! - 您有这个级别的下一个2项目的选择 - >
< idSolicitacaoRef>< / idSolicitacaoRef>
< dataInicial>< / dataInicial>
< dataFinal>< / dataFinal>
< registroInicial> 1< / registroInicial>
<! - 可选: - >
< quantidadeRegistros> 50< / quantidadeRegistros>
< / ser:pesquisarSolicitacao>
< / soapenv:Body>
< / soapenv:Envelope>

此示例请求的工作原理很好,但我无法弄清楚如何在Delphi中进行此调用

解决方案

解决方案:



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

  const 
IS_TEXT = $ 0020;

type
TSimpleHeader = class(TSOAPHeader)
private
FValue:string;
发布
属性值:string Index(IS_TEXT)读取FValue写入FValue;
结束

userName = class(TSimpleHeader);

然后注册此标题:

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

手动发送标题:

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

(WS as ISOAPHeaders).Send(User);

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



令人难过的是,Index的关键工作用于不明显的东西,关于它的文档也很难找到并难以理解:


AS_ATTRIBUTE功能已被弃用。它仍然适用于旧代码,但首选方法是使用属性的索引值。 index属性允许您指定属性是属性,无界元素,可选元素,文本值,还是可以具有NULL值。


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


I need to send something like this:

   <soapenv:Header>
      <ser:userName>admin</ser:userName>
      <ser:userPassword>secret</ser:userPassword>
   </soapenv:Header>

Delphi WSDL importer, generated this:

  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);

and registered the type as:

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

The problem is that when I call it using the delphi generated code it puts the userName and password in the Body section of the SOAP message, not in the Header.

So I tried sending the Headers myself, like this:

Changed the type definition to inherit from the userName2 class because I can't send a string using the ISOAPHeaders.Send() method.

userName = class(userName2);        

Then sent the headers:

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

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

Now the headers are in the correct place, but they are being sent like this:

<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>

Almost there, but I don't want the "Value" property, I just want a plain simple tag in the header.

How can I do it?

Thanks.

== EDIT ==

As requested, the WSDL is here: http://desenvolvimento.lemontech.com.br:8081/wsselfbooking/WsSelfBookingService?wsdl

SOAP UI imported it and generated this sample request:

<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>

This sample request works just fine, but I can't figure out how to make this call in Delphi.

解决方案

Solution:

It wasn't much obvious, but I just had to add the IS_TEXT value to the Index and declare a new TSOAPHeader descendant, the solution was like this:

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);

Then register this header:

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

And send the Header manually:

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

    (WS as ISOAPHeaders).Send(User);

Basically, the IS_TEXT value in the Index prevents Delphi from creating a userName tag and a Value tag inside it. It just places the string of the Value property inside the userName tag.

It's sad that the Index keywork is used for something so not obvious, also the documentation about it is difficult to find and hard to understand:

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.

Source: http://docwiki.embarcadero.com/RADStudio/XE3/en/Using_Remotable_Objects

这篇关于在Delphi中的SOAP Header中发送简单的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆