Learn Pre-requisite topic before implement below practical:
1. C#
2. Asp.Net
3. Web service Endpoint
4. Web service Request
Steps:
Many times we need to consume webservice in application without adding as web reference.
As per below steps we can consume service with no web service reference.
1. Create a class file which will call web service.
(In my application, I have created a console app)
Service endpoint :
http://webservices.oorsprong.org/websamples.countryinfo/CountryInfoService.wso?op=CapitalCity
Service Request:
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<CapitalCity xmlns="http://www.oorsprong.org/websamples.countryinfo">
<sCountryISOCode>string</sCountryISOCode>
</CapitalCity>
</soap12:Body>
</soap12:Envelope>
2. Include System.Net namespace. This namespace is used to make http web request.
3. Include System.IO namespace. This namespace is used to read/write from different sources like a file, memory, network, isolated storage, etc.
4. Create web request as per below step:
4.1 Web Request will create a request object for service endpoint.
4.2 Specify correct content type and accept parameter value as per request format. For example, json format will have “application/json; charset=utf-8” and XML format will have “text/xml; charset=utf-8”.
4.3 Specify web request method. It can be GET/POST/PUT.
4.4 This service has country ISO code as input and in output it will show country’s capital.
5. Take input for the service. In this, input has been taken from console.
6. Create object of HttpWebRequest and assign value of endpoint/content type and method.
7. Create a string to format request value with input value.
8. Add request body to web request object using StreamWriter.
9. Get response from the service by using request.GetResponse() method. This method will get response from the service and can be written to the variable by using StreamReader.
Final Result:
Added India ISO code ‘IND’ as input, we are receiving ‘New Delhi’ as response from the service.