public knowledge blog_load(sender Virendra)

                                                                                                                          Nothing shocks me, I am a Software Engineer.

Execute Web Service via GET Method

Posted by Virendra Dugar on September 8, 2009

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.

WebService

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

WebService1

It shows following error:

Error1

Above error is coming as method is expecting a value, and method tries to convert null value to string. If you refresh your browser then following error comes:

Error

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….

Advertisements

One Response to “Execute Web Service via GET Method”

  1. JimmyBean said

    I don’t know If I said it already but …Excellent site, keep up the good work. I read a lot of blogs on a daily basis and for the most part, people lack substance but, I just wanted to make a quick comment to say I’m glad I found your blog. Thanks, 🙂

    A definite great read..Jim Bean

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: