Programming is Passion,Software Development is an Adventure- Willy David Jr

Programming is Passion,Software Development is an Adventure- Willy David Jr

Consuming Web Service from Remote Machine

 

Consuming simple web service and understanding web service is a little bit difficult at first. It takes some time to understand the theory behind it. In my part, reading will be a good start. Read some articles and basics behind it. But as you move along, consuming will just be a basic part. You need to extend your web service so that it can be used by other application if not from your local, it can be a network, or in the internet.

On this simple article I've created, we will create a simple web service and use it on a local area network. Here are the steps:

1.) Create your web service in your VS 2005 environment, I prefer File - New - Web Site. Then choose ASP.NET Web Service template. Make sure you choose HTTP as your location.

2.) Create your web methods. Sample code below:

[WebMethod]public int Add(int a, int b)

{

return (a + b);

}

 

[
WebMethod]public System.Single Subtract(System.Single A, System.Single B)

{

return (A - B);

}

 

3.) Add a web config file. Add these nodes after system.web so that you can access this web service from a remote machine:

   <webServices>
        <protocols>
            <add name="HttpGet"/>
            <add name="HttpPost"/>
        </protocols>
    </webServices>

If you didn't put this, you will encounter this specific error: "The test form is only available for requests from the local machine" if you accessed this web service from a remote machine.

4.) Lastly, because your web service was running on your IIS, you can now accessed it and consumed your web service. For example, I have this remote machine, and my local machine where I published the above web service has a computer named PH-WWDTWDAVID, then I will just add a web reference like this link:

http://ph-wwdtwdavid/WebService/Service.asmx

5.) To use the code:

int xx; 

localhost.
Service myService = new localhost.Service();

xx=myService.Add(3, 3);

Response.Write(xx.ToString());

 

Regards,

Willy David Jr

Comments

Sonia said:

Hi david,

Ur Article is quite helpfull luckily coz im trying to smthing similar but just a quic question is it possible to call consum a webservice created on your loaclhost  to the client on intrnet if so how could not actually get your 5th point u would be really helpfull if u can explain it a bit more claerly.

Thanks in advance.

# April 30, 2008 12:30 PM

willydavidjr said:

Hi Sonia, sorry for my late reply, I edited my blog and check it out if it will works for you. If it doesn't contact me on email: willydavidjr[at]yahoo[dot]com. Thanks!

# May 5, 2008 7:20 PM

willydavidjr said:

Hi Sonia, have you added a web reference to your site just what I did on step number 4?

# May 5, 2008 7:22 PM

willydavidjr said:

Make sure also that your solution on your server machine where you are running your web service is running and up. Thanks!

# May 5, 2008 10:26 PM