Building a Web Service from Scratch with Delphi - Testing the service
(Page 4 of 4 )
Now, to use the service we need to create a client, but before we do this make sure that the service is running. We need to create the client in the same way that we created the other client in the previous article. Go to File|New|Other and select the Web services tab. Then select WSDL importer, and enter the location of the WSDL file that you saved previously. The wizard should now create a unit for you. Save this unit in a new folder.
Then start a new application and save it in the same folder. On the form drop an edit, a button and an httprio component (rename it htt). Now, you might ask why we are using the HTTPRIO component since its main purpose is to connect a client to a web service that is located on a server on some other part of the planet? It's very simple: the HTTPRIO component regards the (local) web server (in my case IIS) as remote. So let's set the parameters for the HTTPRIO component:
WSDLLocation : IMyWebServer.xml
Port : IMyWebServerPort
Service: IMyWebServerservice
Obviously your details might be different depending on what name you gave your WSDL file earlier. Now double click on the button and add the following code:
procedure TForm1.Button1Click(Sender: TObject);
begin
(htt as IMyWebServer).SetTxt(edit1.Text);
showmessage('The text entered is: '+(htt as
IMyWebServer).GetTxt);
end;
All that happens in this code is that the settxt function is called with the text available in the edit1 box and then gettxt is called to show the text.
That's all there is to it when creating web services. Delphi makes it all very easy for us by doing all the donkey work so we can easily work with a web service with two lines of code!
Conclusion
I'm well aware that the example service I created here is very simple. This is entirely intentional. This code can easily be modified and made more useful. It is a good starting point for someone who is just starting to learn web services. A good next step would be to perhaps introduce a database to the service and display random quotes. Visit the xmethods.com website for some inspiration. In the meantime have fun using and learning web services.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |