Tuesday, December 21, 2004

WebServices Series Part - 3 (Accessing with HTTP POSTLOCALHOST, POST and GET)

we will look how we can access web services with HTTP-GET and HTTP-POST protocols.


.Net Framework 1.0 had all the three protocols i.e. HTTP-POST,HTTP-GET AND HTTP-SOAP enabled by default. But in version 1.1 only HTTP-SOAP is enabled by default. Hence if you want to access the web service my HTTP-POST or GET then you will have to enable it explicitly.



Since HTTP-POST is not enabled by default in .Net framework 1.1 to test web service on the local machine there is a new protocol that has been added i.e. HTTPPOSTLOCALHOST. It is because of this protocol that while testing a web service you are able to view a test form on your local machine and not on a remote computer.


Let us check out each of these protocols individually: -



HTTPPOSTLOCALHOST: - This is enabled by default in .Net Framework version 1.1. Hence if you access your website as http://localhost you will get the test form but if you test the same web service with http:// from some other computer than the test form will not be displayed.



e.g. Try and access our trigonometry web service from your machine and then access it from any other machine. You will notice that the test form will not be displayed.



Before we look at HTTP-POST and HTTP-GET we need to checkout DefaultWsdlHelpGenerator.aspx.



DefaultWsdlHelpGenerator.aspx is the default wsdl help file located at



:\WINNT\Microsoft.NET\Framework\v1.1.4322\CONFIG.



Open DefaultWsdlHelpGenerator.aspx and checkout showPost flag.


bool showPost = true; // search for this



Depending the value of showPost flag HTTP-GET or HTTP-POST protocol is used.Set its value to true if you want to see a POST test form and false to view a GET test form.



Along with this you will have to include the appropriate protocols in the web services tag in Web.config.



HTTP-POST: - To enable our web service with HTTP-POST we will have to make the following changes.



1. Open the web.config and add the tag under as follows to enable the HTTP-POST protocol.











2. Open DefaultWsdlHelpGenerator.aspx and set the value of showPost to true. By default it is set to true.
3. Run the web service and checkout the HTTP-POST form.
4. On execution you will see the xml output of your webservice. Checkout the URL of output in the browser it will not show any querystring parameters.



http://namrathas/Trigonometery/MyService.asmx/CoSine



5. Execute your web service from some other computer and you will be able to view HTTP-POST test form.



HTTP-GET: - To enable our web service with HTTP-GET we will have to make the following changes.



1. Open the web.config and add to protocols tage s follows to enable our webservice with HTTP-GET protocol also.












2. Open DefaultWsdlHelpGenerator.aspx and set the value of showPost to false. So now HTTP-POST form will be displayed. All the parameters will be passed as querystring.



http://namratas/Trigo/MyService.asmx/CoSine?angel=5



3. Run the web service and checkout the HTTP-GET form. Its looks the same as HTTP-POST form. You will notice the difference on execution.
4. Execute the web service and you will see the xml output of your webservice. Checkout the URL of output in the browser it will show query string parameters passed to the web method.



HTTP-SOAP: - Whenever our webservice is accessed from any application HTTP-SOAP protocol is used. HTTP-SOAP is enabled by default. We will look in to more details of HTTP-SOAP when we look into consuming webservices.



Hence if want our webservice to be enabled for all the four protocols we need to add them in the tag.












No comments: