Calling External Web Services from Salesforce.com – Part II: Opening your web service to the internet

Part I: Hosting your web service
Part II: Opening your web service to the internet
Part III: Building an Apex class that calls your web service

I have to be honest – I did not know how to do this. Having always programmed in an enterprise environment, I never gave it a thought on hosting something that is open to the internet. Fine, everybody can’t know everything, right ? Anyways, I figured there would be others as ignorant as I am (was :)), so here’s step-by-step instructions on exposing your webservice to  the internet.

  • Disclaimer – the below description is for a specific setup – where you are on a home computer with a wireless router. Be prepared to adjust the process if your setup is different.
  • First, make sure you do this on your home computer, or a place where you have full control over the network setup, environment etc.
  • We will be using port forwarding to open up the web service hosted on the IIS on your machine to the internet. To understand what is port forwarding, start by looking at the diagram below. Your home computer is actually hosted behind a few things – firewall, modem, maybe a wireless router.
  • This ensures that nothing on the internet can directly connect to your computer. It will always have to go through the modem, and the firewall. If any malicious attempt is made, the firewall will block it. The exact details are much more sophisticated than this, but this much suffices for what we are trying to do.

image

  • This setup also affects the IP address of your computer. Open command prompt (Start->{type cmd}), and run “ipconfig”. Notice your IP Address - it will be something like 192.168.1.3. This is not a real IP address on the internet – instead, it is the “internal” IP address assigned to your computer by your modem.
  • So what is your real IP address ? Go to http://www.whatsmyip.org/ – this will show your real IP address. I found mine- it was 75.123.65.169. This again is a temporary IP address – the internet service provider can switch it at any time.

image

  • Given that the modem is our “face” to the internet, any request that comes to the IP Address 75.123.65.169 is routed to my modem. The modem may choose to either discard the request, OR, it may choose to forward the request to specific devices connected to it.
  • This is where port forwarding comes in – we basically “tell” the modem/router to forward any requests that come in from the internet on a specific port, to our computer. If we turn this on for port 80, which is used by web services, it means that any incoming web service call will be forwarded to our computer.
  • Enabling port forwarding is different for each setup, so I will walk you through what I did, and leave it to you to fill in the gaps for your particular network setup. First, I opened the administration page for my wireless router- this can be usually accessed by using the first 3 digits of your IP address + 1. For e.g., in my case, since my computer’s IP was 192.168.1.3, I opened my wireless router admin screen by visiting http://192.168.1.1 in the browser.
  • It may ask you for username/password – username is usually “admin”, you will need to know the password here – I did not know of any other way around it.
  • Once you are logged in, the screen looks like this for me:-

image

  • Next, I went to the “Advanced” tab –> “Advanced Settings” –> Port forwarding”. Here I configured it forward all HTTP requests (port 80) to my computer. This is how my setup looks like -

image

  • That’s it, all incoming HTTP requests will be forwarded to your computer now. However, we’re not done yet – these incoming requests will be blocked by your computer’s firewall. We need to open port 80 on the firewall as well.
  • Open Windows Firewall configuration - If you are on Windows 7, go to Control Panel->System & Security->Windows Firewall. From the left hand menu, choose “Allow a program or feature through Windows firewall”.

image

  • In the list of services that comes up, go to the last one (World Wide Web Services (HTTP)), and check both the private and public checkboxes.

image

  • Now we’re done – your web service is now callable from the internet.
  • To test this, visit http://www.webpagetest.org/, and put your web service URL. Your web service URL is - http://{your ip address}/MyWebService/Service1.asmx. For e.g., my web service IP address is - http://75.123.65.169/MyWebService/Service1.asmx.
  • So there you go, your web service should now be accessible from the internet. It is also now ready to be called by Salesforce. In my next post, we will go over the basics of calling this web service from Salesforce.com.

2 comments:

  1. Published website on IIS and able to test it using localhost but on site http://www.webpagetest.org/ it gives navigation error

    ReplyDelete
  2. You don't have port-forwarding enabled on your router. The site www.webpagetest.org is probably hitting your IP address (that is, your modem), but you need to configure it to forward any website requests (on port 80), to your internal IP Address.

    Go through the steps for port forwarding again, and you should be able to get it to work.

    ReplyDelete