Testing an XML Web Service from a Browser


After you create a Web service, you can test it in a browser that supports XML, such as Internet Explorer version 5.0 or higher. Just enter the address of the Web service in your Web browser. For example, if you saved TemperatureService.asmx in a subdirectory of your Web site called Services , you can enter the following address:

http://localhost/Services/TemperatureService.asmx

When you access a Web service directly from a Web browser, the Web Service Help page is displayed (see Figure 22.1). This page lists all the properties and methods of the Web service.

Figure 22.1. The Web Service Help Page.

graphics/22fig01.jpg

The Help page for TemperatureService , for example, lists the ToCelsius and ToFahrenheit methods as hypertext links. If you click either link, you are transferred to another page that contains more information about the method.

NOTE

You can modify the appearance of the Web Service Help page by modifying the ASP.NET page at the following location:

 
 \WINNT\Microsoft.NET\Framework\[version]\CONFIG\DefaultWsdlHelpGenerator.aspx 

If you navigate to the description page for the ToCelsius method, you see sample requests and responses for two protocols: SOAP and HTTP-Post. These are two of the standard wire formats for transmitting Web service requests and responses. When exposing a Web service to the outside world, you normally use SOAP. However, you can test your Web service on the local server by using the HTTP-Post protocol (see the next section).

NOTE

ASP.NET version 1.0 enabled you to expose a Web service using SOAP, HTTP-Post, and HTTP-Get. In ASP.NET 1,1, by default, a Web service can only be accessed using SOAP (you can use HTTP-Post only when requesting a Web service from the local machine). The HTTP-Get and HTTP-Post protocols are disabled for security reasons. If you want to enable HTTP-Get and HTTP-Post from remote computers, then you need to add the following Web.Config file to your application:

 
 <configuration>   <system.web>     <webServices>     <protocols>       <add name="HttpPost" />       <add name="HttpGet" />     </protocols>     </webServices>   </system.web> </configuration> 

Invoking an XML Web Service with HTTP-Get

HTTP-Get is the standard HTTP protocol for transmitting requests for URLs or posting a form with METHOD="Get" . You could invoke the ToCelsius method of the TemperatureService Web service by adding the following hypertext link in an HTML document:

 
 <a href="/Services/TemperatureService.asmx/ToCelsius?TF=32">Convert</a> 

Alternatively, you could type the following URL directly into the address bar of your Web browser:

http://localhost/Services/TemperatureService.asmx/ToCelsius?TF=32

In either case, the ToCelsius Web method would be invoked by passing the TF parameter with the value 32 . The Web service would return the following XML document representing the results of converting the value 32 into degrees Celsius:

 
 <?xml version="1.0" encoding="utf-8" ?>   <double xmlns="http://tempuri.org/">0</double> 

Because 0 degrees Celsius is equivalent to 32 degrees Fahrenheit, the ToCelsius method returns the value .

CAUTION

Web service method names are case sensitive. So, attempting to invoke the TOCELSIUS method would generate an error.


Invoking an XML Web Service with HTTP-Post

HTTP-Post is the standard HTTP protocol for transmitting form data submitted with METHOD="Post" . For example, you could invoke the ToCelsius method of TemperatureService by using the HTML page in Listing 22.4.

Listing 22.4 PostInvoke.html
 <form method="post"   action="/Services/TemperatureService.asmx/ToCelsius"> <input name="TF" value="32"> <input type="submit" value="Convert!"> </form> 

The C# version of this code can be found on the CD-ROM.

Posting the form in Listing 22.4 would invoke the ToCelsius method of the TemperatureService Web service. The following XML document would be returned:

 
 <?xml version="1.0" encoding="utf-8" ?>   <double xmlns="http://tempuri.org/">0</double> 

You can test a Web service using HTTP-Post directly from the Web Service Help page, which includes a form for each Web service method that enables you to enter a value and invoke the Web service. Invoking a method from the form opens a new browser window that displays the return value from the method (see Figure 22.2).

Figure 22.2. Invoking a Web service method.

graphics/22fig02.jpg

NOTE

You cannot use the Web Service Help page when testing Web services that accept complex types, such as DataSets , as input parameters or when testing Web services that require special headers.


Invoking an XML Web Service with SOAP

The Simple Object Access Protocol (SOAP) enables you to transmit more complex types of messages across a network. You can transmit data types with SOAP that you cannot transmit by using either HTTP-Get or HTTP-Post. For example, you can use SOAP to transmit DataSets , custom classes, and binary files.

SOAP is an XML-based protocol backed by a number of large companies including Microsoft, IBM, and Ariba. A SOAP message contains an envelope section that contains the data to be transported.

A SOAP request that invokes the ToCelsius method of TemperatureService , for example, looks like this:

 
[View full width]
 
[View full width]
<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap graphics/ccc.gif /envelope/"> <soap:Body> <ToCelsius xmlns="http://tempuri.org/"> <TF>32</TF> </ToCelsius> </soap:Body> </soap:Envelope>

This SOAP request invokes the ToCelsius method by passing a TF parameter with the value 32 . The Web service would return the following SOAP response:

 
 <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">   <soap:Body>     <ToCelsiusResponse xmlns="http://tempuri.org/">       <ToCelsiusResult>0</ToCelsiusResult>     </ToCelsiusResponse>   </soap:Body> </soap:Envelope> 

Unlike HTTP-Get and HTTP-Post, the SOAP protocol is not tied to the HTTP protocol. Although you use SOAP over the HTTP protocol in all the examples in this book, by posting SOAP messages, you also can use SOAP over other protocols such as SMTP.



ASP.NET Unleashed
ASP.NET 4 Unleashed
ISBN: 0672331128
EAN: 2147483647
Year: 2003
Pages: 263

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net