Execute Web Service via GET Method.

Virendradugar
Posted by in ASP.NET category on for Beginner level | Views : 20666 red flag

This article explains how you can execute the web service via HTTP GET method.
Introduction

When we create any web service via Vistual Studio, by Default it gets executed via HTTP Post. This article explains how we can configure our web service so it runs on both the method that is GET and POST.

Demonstration

When we created any web service via Vistual Studio, we always get a default method named “Hello world”.

When you hit this URL in your browser, http://localhost/SampleWebService/Service.asmx, it shows list of all the web method. Click on Hello World and then Invoke. It gives following output:

 

I am assuming that you have hosted your web service in IIS.

 

   <?xml version="1.0" encoding="utf-8" ?>

  <string xmlns="http://tempuri.org/">Hello World</string>

 

Looks good. Now let’s go and modify our service.cs file and add a new method which takes one parameter.

[WebMethod]

 public string HelloWorldWithParam(int a)

 {

        return "Hello World " + a.ToString();

 }

 

Now, hit this URL http://localhost/SampleWebService/Service.asmx. It is showing both the methods.




Click on HelloWorldWithParam. It asks for parameter value. Without giving any value just click invoke.


 

It shows following error:


Above error is coming as method is expecting a value, and method tries to convert null value to string. If you refresh your browser or try to modify the URL by adding querystring then also following error comes:

 

 

Now let’s provide the parameter value via URL only. Hit this URL, http://localhost/SampleWebService/Service.asmx/HelloWorldWithParam?a=1 , and then also you will receive the above error.

 

Reason: By default, Web service created via Visual Studio executes through HTTP Post method. When Post method is used, querystring is not visible. We need to configure our web service, so it works with HTTP GET and POST method. How can we do this?

 

Go to web.config and add this code in system.web section.

 

<webServices>

      <protocols>

        <add name="HttpGet"/>

        <add name="HttpPost"/>

      </protocols>

</webServices>

 

Good to go now. Hit this URL again and this time you will see “Hello World 1”.

 

Enjoy….

 

Page copy protected against web site content infringement by Copyscape

About the Author

Virendradugar
Full Name: Virendra Dugar
Member Level: Silver
Member Status: Member,MVP
Member Since: 8/11/2009 4:14:05 AM
Country: India

http://jquerybyexample.blogspot.com
Virendra Dugar is experienced Senior Software Developer with over 5 years of hands-on experience working with Microsoft .NET technology (ASP.NET, C#, VB.NET,SQL Server). He is always keen to learn new technology. He holds a Master's Degree in Computer Application & Information technology from Gujarat University in india.In free time, he loves to listen music, read books, play games and do blogging etc. Visit his blogs : http://jquerybyexample.blogspot.com

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)