In this article, the second of three parts covering web services, we will build a web service client and discuss the various components and methods needed to make an application work.
Creating a Web Service Client with Delphi - The application (Page 3 of 4 )
Create a new application and add a memo, a button (caption it Check Weather), and an edit box. Go to the web services tab and drop an HTTPRIO component . Save this application in the same directory where you’ve saved the unit created earlier. In the implementation section of the form add the following - uses WeatherForecast;
Your form should now look something like this at design time:
The HTTPRIO component represents a remote invokable object over an HTTP connection. What this means is that our client will use this component to communicate with the web service over the Internet. The component has four key properties:
URL
Service
WSDLLocation
Port
Of these properties, you either use the URL property or the WSDLLocation, Service and Port properties. So let's fill these these properties as follows:
Most of these properties will already have information available to them when you click on the dropdown boxes. The HTTPRIO component will now contain all the methods of the web service.
Next, double click on the button and add the following code:
procedure TForm1.Button1Click(Sender: TObject); var wf:WeatherForecastS; res:ArrayOfWeatherData; i:integer; begin wf:=(htt as WeatherForecastSoap).GetWeatherByPlaceName (edit1.Text); if wf.PlaceName<> '' then res:=wf.Details; memo1.Lines.Add('The min and max temps in Fahrenheit is:'); memo1.Lines.Add(' '); for i:= 0 to high(res) do begin memo1.Lines.Add(res[i].Day+' - '+ ' Max Temp. Fahr: '+res [i].MaxTemperatureF+' - '+'Min Temp Fahr: '+res [i].MinTemperatureF); end end;
The code first takes the city name and then retrieves the weather data:
wf:=(htt as WeatherForecastSoap).GetWeatherByPlaceName (edit1.Text); if wf.PlaceName<> '' then res:=wf.Details; memo1.Lines.Add('The min and max temps in Fahrenheit is:'); memo1.Lines.Add(' ');
Then it simply loops through the results that are stored in the array and adds them to the memo:
for i:= 0 to high(res) do begin memo1.Lines.Add(res[i].Day+' - '+ ' Max Temp. Fahr: '+res [i].MaxTemperatureF+' - '+'Min Temp Fahr: '+res [i].MinTemperatureF);
Below is a screen shot of a test run of the application: