Search code examples
.netweb-servicesdelphiwinapicom

How to access a .Net web service from a Delphi Win32 application?


What options do I have if I want to enable a Delphi Win32 application to consume a .Net webservice?

Is it possible to interact directly? Or do I have to resort to a middleman-software communicating to the Delphi application per COM for instance?

The Delphi application in question is written in Delphi 2006 but is planned to be updated to Delphi XE soon.


Solution

  • While your project is open in Delphi IDE, go to:

    File | New | Other... | Delphi Projects | WebServices | WSDL Importer

    Now WSDL importer wizard will start. Type in WSDL address for your webservice, and press Next. It will show you different options about how to process the WSDL. You can change the options if needed. Eventually, when the wizard is finished, you will have a new unit in your project which contains client-side wrapper classes and interfaces for your webservice. Now you can use this class in different ways. The simplest method is to call a function which is named Get(Your_WebService_Name). For example if your webservice name is TestWebService, the function will be named GetTestWebService.

    The function will return an interface which represents the same interface as your webservice, now you can call the methods of this interface, and it will automatically transfer the request to the remote server, and return the result back to you. An example source code could look like this:

    var
      MyTestService: ITestService;
    begin
      MyTestService := GetTestService();
      MyTestService.TestMethod;
    end;
    

    Another option is that you setup a THttpRio object manually, and use it. Actually that is what that Get(Your_WebService_Name) function does internally.