Search code examples
web-servicesdelphisoaphttpwebrequest

Accessing the original TWebRequest object in a Delphi SOAP Server


Summary: How do you access the original TWebRequest object in a Delphi Soap Server Application ?

My web service publishes a service ITest with a method CallMe:

ITest = interface(IInvokable)
['{AA226176-FFAD-488F-8768-99E706450F31}']
  function CallMe: string; stdcall;
end;
...
initialization
InvRegistry.RegisterInterface(TypeInfo(ITest));

This interface is implemented in a class:

TTest = class(TInvokableClass, ITest)
public
  function CallMe: string; stdcall;
end;
...
initialization
InvRegistry.RegisterInvokableClass(TTest, TestFactory);

How do I access the original TWebRequest object inside of the implementation of this method ? E.g. If I want to check what cookies were set, or read other properties on the request:

function TTest.CallMe: string;
begin
  // how to access TWebRequest object
  ...
end;

Solution

  • uses
      System.SysUtils,
      Web.HTTPApp,
      Soap.WebBrokerSOAP;
    
    function TTest.CallMe: string;
    var
      WebDispatcher: IWebDispatcherAccess;
    begin
      Result := '';
      if Supports(GetSOAPWebModule, IWebDispatcherAccess, WebDispatcher) then
        Result := Format('You are calling me from: %s', [WebDispatcher.Request.RemoteIP]);
    end;