Send simple strings in SOAP Header in Delphi

前端 未结 3 1606
灰色年华
灰色年华 2021-02-08 01:49

I need to send something like this:

   
      admin
      secret         


        
3条回答
  •  醉梦人生
    2021-02-08 01:59

    You can override the serialization for any TSOAPHeader class. Just override its ObjectToSOAP function. I came up with this:

    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.
    

    results in this request header:

    
    
     admin
     secret
     key
    
    

提交回复
热议问题