|
|
As you search the Web for web services or as you create your own services, there may be times when you will want to take the service for a test drive before you write a program that puts the service to use. As you will learn in this section, you can use your browser to interact with a web service. In addition, your browser will display the SOAP and HTTP messages the proxy will send to the web service, as well as the messages the service will return to the proxy that contains the result.
To understand how you can use your browser to test drive a web service, use your browser to view the Hello web service that you created in Chapter 2. As you may recall, the Hello web service returns a string that contains the text “Hello World.” Within your browser, enter the address http://localhost/Hello/Service1.asmx. Your browser, in turn, will display a page, as shown in Figure 4.6, that contains a link you can use to call the HelloWorld service.
Figure 4.6: Using a link to access a web service within a browser
If you click your mouse on the HelloWorld link, your browser will display a page that contains an Invoke button that you can click to call the service, as shown in Figure 4.7. The page will also show the SOAP messages the proxy and service will exchange. If you click your mouse on the Invoke button, your browser will call the web service, displaying the XML-based result within a window, as also shown in Figure 4.7. If you examine the XML-based data, you will see the Hello World message.
Figure 4.7: Viewing a web service’s XML-based result within a browser
Many web services require parameters. When you use your browser to interact with a web service, your browser will display a page that contains fields you can use to specify the parameter values, as shown in Figure 4.8.
Figure 4.8: Entering parameter values within a web page
In this case, the CalculateMortgage web service returns a monthly mortgage amount based on a principal amount, interest rate, and the number of monthly payments. If you specify parameter values using the fields, your browser will display the service’s result using XML-based data as shown in Figure 4.9.
Figure 4.9: Displaying the result of the CalculateMortgage web service within a browser.
To create the CalculateMortgage web service, perform these steps:
Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.
Within the New Project dialog box Project Types list, click Visual Basic Projects. Then, within the Templates field, click ASP.NET Web Service. Finally, within the Location field, specify the folder within which you want to store the program and the program name CalculateMortgage. Select OK. Visual Studio .NET will display a page onto which you can drag and drop the service’s components.
Select the View menu Code option. Visual Studio .NET will display the program’s source code. Within the source code add the following program statements:
<WebMethod()> Public Function Calc(ByVal Principal As Double, _ Ä ByVal Interest As Double, ByVal Years As Integer) As Double ÄDim Months = Years * 12 Interest = Interest / 1200.0 ' Monthly interest as a decimal number Calc = (Principal*Interest) / (1 - ((1 / (1+Interest))^ Months)) End Function
|
|