Share via


Accessing XML Web Services from a Browser

After you publish an XML Web service created using ASP.NET, you can test its functionality by using a browser to call one of its XML Web service methods. To do so, deploy your XML Web service to a Web server and then link to the XML Web service from a browser. Once the .asmx file is deployed to a Web server, you can test your XML Web service in a browser using either HTTP-GET or HTTP-POST.

**Note   **By default, an XML Web service created using ASP.NET is able to support multiple protocols, including HTTP-GET, HTTP-POST, and HTTP-SOAP.

To test an XML Web service in a browser using HTTP-GET

  1. Deploy your XML Web service to a Web server. For more information, see XML Web Service Publishing and Deployment.

  2. Access a Web browser and enter the URL for your XML Web service in the address bar, using the following format:

    https://servername/apppath/webservicename.asmx
    
    Parameter Value
    servername The name of the server on which your XML Web service was deployed.
    Apppath The name of your virtual directory and the rest of the Web application path.
    webservicename.asmx The name of the XML Web service .asmx file.

    The XML Web service's HTML description file is displayed.

  3. The XML Web service's HTML description page shows you all the XML Web service methods supported by a particular XML Web service. Link to the desired XML Web service method and enter the necessary parameters.

    For example, suppose you have published an XML Web service named StockServices. StockServices contains an XML Web service method called GetQuote, which requires a stock symbol as a parameter. When published, the base URL for this service is http://<servername>/apppath/StockServices.asmx. You could test this service by entering this HTTP-GET request in the browser's URL line:

    http://<servername>/apppath/StockServices.asmx
    

    The server, in response to this request, displays the XML Web service's HTML description page. This page lists each method in the XML Web service. It also provides a means for you to test the methods and see their XML responses.

    You could also be more specific, entering the exact method you want to test. For example, you could enter the following to test your service's GetQuote method:

    http://<servername>/apppath/StockServices.asmx/GetStockQuote?tickerName=MSFT
    

    The server shows an XML response that gives the current price of the stock you request. If the return data type for GetQuote is a double-precision floating-point number, the result might look like the following:

    <?xml version="1.0" ?>
    <double>74.5</double>
    

Alternative method for testing an XML Web service in a browser using HTTP-GET

  1. Deploy your XML Web service to a Web server. For more information, see XML Web Service Publishing and Deployment.

  2. Access a Web browser and enter the URL for your XML Web service in the address bar, using this format:

    https://servername/vdir/webservicename.asmx/Methodname?parameter=value
    
    Parameter Value
    servername The name of the server on which your XML Web service is deployed.
    Apppath The name of your virtual directory and the rest of the Web application path.
    webservicename.asmx The name of the XML Web service .asmx file.
    Methodname The name of a public method that is exposed by your XML Web service. If left blank, the XML Web service's description page is shown, listing each public method available in the .asmx file. (Optional)
    parameter The appropriate parameter name and value for any parameters required by your method. If left blank, the XML Web service's description page is shown, listing each public method available in the .asmx file. (Optional)

    Note   The XML Web service method name in this syntax is case sensitive, but the server, project, and XML Web service names are not.

For example, suppose you have published an XML Web service named StockServices. StockServices contains an XML Web service method called GetQuote, and the XML Web service method accepts a stock symbol as a parameter, returning the price as a double-precision floating-point number. When published, the base URL for this service is http://<servername>/apppath/StockServices.asmx. You can test this service by entering the following HTTP-GET request in the browser's address field:

http://<servername>/apppath/StockServices.asmx/GetStockQuote?tickerName=MSFT

The server shows an XML response with the current price of the stock you request. If the return data type for GetQuote is a double-precision floating-point number, the result might look like the following:

<?xml version="1.0" ?>
<double>74.5</double>

To test an XML Web service in a browser using HTTP-POST

In order to test an XML Web service using HTTP-POST, you need to create an HTML page with a form, which has its method attribute set to POST. The following steps show you how to create an HTTP-POST client for the following XML Web service.

<%@ WebService Language="C#" Class="Math" %>
using System.Web.Services;
public class Math : WebService {
     [ WebMethod ]
     public int Add(int num1, int num2) {
          return num1+num2;
     }

     [ WebMethod ]
     public int Subtract(int num1, int num2) {
          return num1-num2;
     }
}
[Visual Basic]
<%@ WebService Language="VB" Class="Math" %>
Imports System.Web.Services

Public Class Math
    Inherits WebService

    <WebMethod> _
    Public Function Add(num1 As Integer, num2 As Integer) As Integer
        Return num1 + num2
    End Function    
    
    <WebMethod> _
    Public Function Subtract(num1 As Integer, num2 As Integer) As Integer
        Return num1 - num2
    End Function
End Class
  1. Deploy your XML Web service to a Web server. For more information, see XML Web Service Publishing and Deployment.

  2. Create an HTML document with a form, using the following format:

       <form method=POST action='https://www.contoso.com/math.asmx/Subtract'>
           <input type="text" size="5" name='num1'\"></td> -
           <input type="text" size="5" name='num2'\"></td> =
           <input type=submit value="Subtract"> </td>
       </form>
    
    Parameter Value
    method POST. If you want to test your XML Web service using HTTP-POST, use POST.
    action URL to the XML Web service method. In the previous example, math.asmx is the XML Web service and Subtract is the XML Web service method.
    type="text" For each parameter of the XML Web service method, create input tags with the type attribute set to "text". This allows you to type a parameter value in the text input control.
    name='num1' The name of the XML Web service method parameter. Add as many text input controls on the form as there are parameters in the XML Web service method. For instance, if an XML Web service method has three parameters, three text input controls are needed that each have their name attribute set to the name of the parameter.
    type=submit Add a submit button so you can post the data back to the XML Web service method.
  3. Access a Web browser and enter the URL for the HTML document you created in the previous step.

    The HTML document created in the previous step is displayed.

  4. Enter the appropriate values for the XML Web service method in the text boxes and click the submit button.

    For example, if you entered 6 and then 3 into the two text boxes for the Subtract XML Web service method above, the following result is returned:

    <?xml version="1.0" ?> 
    <int xmlns="http://tempuri.org/">3</int>
    

See Also

Building XML Web Service Clients | Discovering XML Web Services | Creating Clients for XML Web Services | Exploring Existing XML Web Services Created Using ASP.NET | Accessing XML Web Services from a Browser