Hemanth Kumar

Hemanth Kumar

  • 650
  • 1.4k
  • 318.6k

Testing a WebMethod in WebService

Aug 1 2014 6:34 AM
Hi,
 
I have created a public WebMethod "Add" in a web service which adds two integers and returns the result. Below is the code
 
namespace WebServicesDemo
{
/// <summary>
/// Summary description for CalculatorWebServices
/// </summary>
[WebService(Namespace = "Hemanth")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class CalculatorWebServices : System.Web.Services.WebService
{
[WebMethod]
public int Add(int firstNumber, int secondNumber)
{
return firstNumber + secondNumber;
}
}
}
 
 Now when i run this and click the "Add" method it gives the below screen asking me to enter the two integers. If you see below the empty fields, it gives the samples for SOAP 1.1, SOAP 1.2, HTTP POST request and response. But by default, it is tesing the operation HTTP POST which is shown in red in the below screen and when we invoke the add method it shows the result as HTTP response. Is it possible to change the way we want to see the result ( say i want to see the result as the response of SOAP1.1 or SOAP1.2 and also i would like to know why this is using HTTP POST by default?
 
Thanks in advance. 
 

Add

Test

To test the operation using the HTTP POST protocol, click the 'Invoke' button.
Parameter Value
firstNumber:
secondNumber:
 

SOAP 1.1

The following is a sample SOAP 1.1 request and response. The placeholders shown need to be replaced with actual values.

POST /CalculatorWebServices.asmx HTTP/1.1 Host: localhost Content-Type: text/xml; charset=utf-8 Content-Length: length SOAPAction: "Hemanth/Add"  <?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>     <Add xmlns="Hemanth">       <firstNumber>int</firstNumber>       <secondNumber>int</secondNumber>     </Add>   </soap:Body> </soap:Envelope>
HTTP/1.1 200 OK Content-Type: text/xml; charset=utf-8 Content-Length: length  <?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>     <AddResponse xmlns="Hemanth">       <AddResult>int</AddResult>     </AddResponse>   </soap:Body> </soap:Envelope>

SOAP 1.2

The following is a sample SOAP 1.2 request and response. The placeholders shown need to be replaced with actual values.

POST /CalculatorWebServices.asmx HTTP/1.1 Host: localhost Content-Type: application/soap+xml; charset=utf-8 Content-Length: length  <?xml version="1.0" encoding="utf-8"?> <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">   <soap12:Body>     <Add xmlns="Hemanth">       <firstNumber>int</firstNumber>       <secondNumber>int</secondNumber>     </Add>   </soap12:Body> </soap12:Envelope>
HTTP/1.1 200 OK Content-Type: application/soap+xml; charset=utf-8 Content-Length: length  <?xml version="1.0" encoding="utf-8"?> <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">   <soap12:Body>     <AddResponse xmlns="Hemanth">       <AddResult>int</AddResult>     </AddResponse>   </soap12:Body> </soap12:Envelope>

HTTP POST

The following is a sample HTTP POST request and response. The placeholders shown need to be replaced with actual values.

POST /CalculatorWebServices.asmx/Add HTTP/1.1 Host: localhost Content-Type: application/x-www-form-urlencoded Content-Length: length  firstNumber=string&secondNumber=string
HTTP/1.1 200 OK Content-Type: text/xml; charset=utf-8 Content-Length: length  <?xml version="1.0" encoding="utf-8"?> <int xmlns="Hemanth">int</int>
 
 

Answers (1)