gpt4 book ai didi

web-services - 在 Delphi 中的 SOAP header 中发送简单字符串

转载 作者:行者123 更新时间:2023-12-03 14:38:30 26 4
gpt4 key购买 nike

我需要发送这样的内容:

   <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生成的代码调用它时,它将用户名和密码放在SOAP消息的主体部分,而不是标题中

所以我尝试自己发送 header ,如下所示:

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

userName = class(userName2);        

然后发送 header :

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

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

现在 header 位于正确的位置,但它们的发送方式如下:

<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”属性,我只想在标题中添加一个简单的标签

我该怎么做?

谢谢。

==编辑==

根据要求,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 中进行此调用。

最佳答案

您可以覆盖任何 TSOAPHeader 类的序列化。只需重写其 ObjectToSOAP 函数即可。我想出了这个:

unit Unit16;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, WsSelfBookingService, StdCtrls,
InvokeRegistry, SOAPHTTPClient, opCOnvertOptions, XMLIntf, XSBuiltIns;

type
TForm1 = class(TForm)
Memo2: TMemo;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

type
TSOAPCredentials = class(TSoapHeader)
private
FPassword: string;
FUsername: string;
FKeyClient: string;
public
function ObjectToSOAP(RootNode, ParentNode: IXMLNode;
const ObjConverter: IObjConverter;
const NodeName, NodeNamespace, ChildNamespace: InvString; ObjConvOpts: TObjectConvertOptions;
out RefID: InvString): IXMLNode; override;
published
property userName : string read FUsername write Fusername;
property userPassword : string read FPassword write FPassword;
property keyClient : string read FKeyClient write FKeyClient;
end;



var
Form1: TForm1;

implementation

{$R *.dfm}

{ TSOAPCredentials }

function TSOAPCredentials.ObjectToSOAP(RootNode, ParentNode: IXMLNode; const ObjConverter: IObjConverter; const NodeName,
NodeNamespace, ChildNamespace: InvString; ObjConvOpts: TObjectConvertOptions; out RefID: InvString): IXMLNode;
begin
Result := ParentNode.AddChild('userName');
Result.Text := FUsername;
Result := ParentNode.AddChild('userPassword');
Result.Text := FPassword;
Result := ParentNode.AddChild('keyClient');
Result.Text := FKeyClient;
end;

procedure TForm1.Button1Click(Sender: TObject);

var
ws : WsSelfBooking;
Req : pesquisarSolicitacao;
Resp : pesquisarSolicitacaoResponse;
Rio : THTTPRIO;
Cred : TSOAPCredentials;

begin
Rio := THttpRIO.Create(nil);
ws := GetWsSelfBooking(false, '', Rio);
Cred := TSOAPCredentials.Create;
Cred.userName := 'admin';
Cred.userPassword := 'secret';
Cred.keyClient := 'key';
Rio.SOAPHeaders.Send(cred);
Req := pesquisarSolicitacao.Create;
Req.registroInicial := 1;
Req.quantidadeRegistros := 50;
Resp := ws.pesquisarSolicitacao(Req);
end;

end.

此请求 header 的结果:

<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:Header>
<SOAP-ENV:userName>admin</SOAP-ENV:userName>
<SOAP-ENV:userPassword>secret</SOAP-ENV:userPassword>
<SOAP-ENV:keyClient>key</SOAP-ENV:keyClient>
</SOAP-ENV:Header>

关于web-services - 在 Delphi 中的 SOAP header 中发送简单字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13751289/

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