使用httprio发送SOAP标头

时间:2017-04-13 13:57:29

标签: delphi soap

我有一个C#应用程序,我很难在Delphi中模拟,因为身份验证失败了。我相信这是一个不正确的身份验证标头。 (相关的,有效的)C#代码是这样的:

public class CustomMessageInspector : IClientMessageInspector
{
    public CustomMessageInspector()
    { }

    public void AfterReceiveReply(ref Message reply, object correlationState)
    { }

    public object BeforeSendRequest(ref Message request, IClientChannel channel)
    {
        AddHeaderToMessage(ref request, "Authentication", Constants.AUTH_VALUE);
        AddHeaderToMessage(ref request, "Application", Constants.APP_NAME);

        return null;
    }

    private void AddHeaderToMessage(ref Message message, string name, string value)
    {
        var header = new MessageHeader<string>(value);

        var untyped = header.GetUntypedHeader(name, Constants.NAME_SPACE);

        message.Headers.Add(untyped);
    }
} 

我的Delphi等价物看起来像这样:

unit Main;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Soap.InvokeRegistry, Vcl.StdCtrls,
  Soap.Rio, Soap.SOAPHTTPClient, SOAPHTTPTrans,
  MembershipService;

type
  TStringHeader = class(TSOAPHeader)
  private
    FVal : string;
  public
    property Value : string read FVal write FVal;
  end;

  AuthHeader = class(TStringHeader);
  AppHeader  = class(TStringHeader);

  TForm3 = class(TForm)
    GetUserButton: TButton;
    HTTPRIO1: THTTPRIO;
    HttpRio: THTTPRIO;
    procedure GetUserButtonClick(Sender: TObject);
    procedure HttpRioHTTPWebNode1BeforePost(const HTTPReqResp: THTTPReqResp; Data: Pointer);
  private
    _currUser : User2;
    function svc: IMembershipService;
  public
  end;

var
  Form3: TForm3;

implementation

{$R *.dfm}

procedure TForm3.HttpRioHTTPWebNode1BeforePost(const HTTPReqResp: THTTPReqResp;  Data: Pointer);
var
  Header : TStringHeader;
begin
  Header := AuthHeader.Create();
  try
    Header.Value := Constants.AUTH_VALUE;
    (svc as ISOAPHeaders).Send(Header);
  finally
    FreeAndNil(Header);
  end;

  Header := AppHeader.Create();
  try
    Header.Value := Constants.APP_NAME;
    (svc as ISOAPHeaders).Send(Header);
  finally
    FreeAndNil(Header);
  end;
end;

function TForm3.svc : IMembershipService;
begin
  Result := HttpRio as IMembershipService;
end;


initialization
  InvRegistry.RegisterHeaderClass(TypeInfo(IMembershipService), AuthHeader, 'Authentication', Constants.NAME_SPACE);

  InvRegistry.RegisterHeaderClass(TypeInfo(IMembershipService), AppHeader, 'Application', Constants.NAME_SPACE);

端。

我认为它可能与“命名空间”值有关,但我不确定。任何人都可以建议吗?

0 个答案:

没有答案