Calling a web service with C#

This article will demonstrate how to build a simple application to call a web service and display the results.

For this example we’ll need a web service that everyone can access.  We are in luck because StrikeIron supplies several FREE web services that anyone can use (StrikeIron limits the number of executions to 100 executions during a 24 hour period…but that is plenty for this exercise).  One of their free web services is a simple “ZIP Code Information Lite” web service that returns the county, city and state information for a given ZIP Code.

We’ll build a simple application consisting of a form to allow the user to input the Zip Code, execute the web service and display the results.  For this project I’ll use Visual Studio 2008, C# and .Net 2.0 to build the application.

To begin, open Visual Studio 2008 and select File-New-Project from the menu (or Ctrl+Shift+N).  Then you will see this dialog box:

New Project Dialog

New Project Dialog

 

The upper right corner of this dialog is where you select the .Net version…select “.NET Framework 2.0.”  Next, select the project type by selecting “Visual C#” and then “Windows.”  Then select the “Windows Forms Application” template.  Finish things up by giving your project a name.  I called my project “ZipCodeSampleApp.”  Click Ok and Visual Studio will create your project.

Now that we’ve created our Visual Studio project, we need to import the web service information into the project.  Most web services have a “WSDL” (Web Services Description Language) file which describes where the web service is located, methods which can be executed and parameter details.  The WSDL for the StrikeIron Zip Code service is locate here (if you click on the link, depending on which browser you are using, you will see an XML document that describes the web service)  http://wslite.strikeiron.com/zipcodeinfolite01/ZIPCodeInfoLite.asmx?WSDL 

We need to import this information into our Visual Studio project.  Look at the Solution Explorer window and you will see a ‘References” node in the treeview:

Solution Explorer window

Solution Explorer window

 

Right mouse click on References and select “Add Web Reference” to bring up this dialog:

Add Web Reference

Add Web Reference Dialog

 

Enter the WSDL URL (http://wslite.strikeiron.com/zipcodeinfolite01/ZIPCodeInfoLite.asmx?WSDL) and click “Go” to analyze the WSDL.

Add Web Reference Dialog

Add Web Reference Dialog

 

You will notice that four methods were discovered.  For this exercise we are interested in the “GetZIPCodeInfo” method.

Change the web reference name from “com.strikeiron.wslite” to something you’ll remember like “ZIPCodeReference” (the code example I provide uses “ZIPCodeReferenc”…so if you use something different, you’ll need to change the code). Click “Add Reference” and Visual Studio will create the necessary wrapper classes for your project.

We are almost done.  Add a button and 5 textboxes to your form so that it looks something like this:

Basic Form

Basic Form

 

Add this code to the button click event:

   // Create an instance of the wrapper class
   ZIPCodeReference.ZIPCodeInfoLite zipinfo = new ZipCodeSampleApp.ZIPCodeReference.ZIPCodeInfoLite();

   // Call the web service
   ZIPCodeReference.ZIPCodeOutput output = zipinfo.GetZIPCodeInfo(textBox1.Text);

   // Check the response for errors (StrikeIron uses code similar to HTTP status codes...so
   // any result in the 200s is good)
   if (output.ServiceStatus.StatusNbr >= 200 && output.ServiceStatus.StatusNbr <= 300)
   {
       textBox2.Text = output.ServiceResult.ZIPCodes[0].County;
       textBox3.Text = output.ServiceResult.ZIPCodes[0].PreferredCityName;
       textBox4.Text = output.ServiceResult.ZIPCodes[0].State;
       textBox5.Text = output.ServiceResult.ZIPCodes[0].USPSClassification;
   }

   // Display the error if one exist
   else
       MessageBox.Show(output.ServiceStatus.StatusDescription, "Error");

 

That is all there is to it.  Now build and run your application.  Enter a ZIP Code and press the button.