none
How do I connect to a web service? RRS feed

  • Question

  • Hi everyone,

    I am New to this. I would like to use the weather web service http://wsf.cdyne.com/WeatherWS/Weather.asmx, and in particular the GetCityWeatherByZip method, in a form. The user enters a zip code and clicks the button, and returned values like State, City, Description, Temperature and such should nicely be displayed in the form.

    How can I achieve this? I use VB, in VS 2008.

    Thank you,

    Cooz

    Tuesday, September 24, 2013 1:40 PM

Answers

  • In order to call a web service in visual studio, you need to use the following steps:

    1. Add a Service Reference to the weather web service.  Right mouse click on the project, select Add Service Reference, put the address for the weather web service in the address textbox, and click go.  The "Weather" service will appear under Services.  You can change the Namespace to something more descriptive than ServiceReference1, such as MyWeather, and then click OK.  At this point, the Service References folder appears with your Namespace listed underneath.

    2. Somewhere in your code where you want this service called (command or click event for a button, etc.), set up a client for this service.  This particular service adds two endpoints to your app.config file, so you need to decide which endpoint is best for you (more in a minute).  In my sample code, I just added the endpoint with basicHttpBinding, which is named "WeatherSoap".  The customBinding endpoint is named "WeatherSoap12".  The code is as follows (assuming you have a TextBox for zip code and a TextBlock with results).

    Using client As New MyWeather.WeatherSoapClient("WeatherSoap")
        Dim result As MyWeather.WeatherReturn = New MyWeather.WeatherReturn
        result = client.GetCityWeatherByZIP(txtZip.Text)
        Dim strReturn As String
        strReturn = "City " & result.City & ": " & result.Description & vbCrLf
        strReturn = strReturn & " has a current temp of " & result.Temperature
        txtResult.Text = strReturn
    End Using

    Obviously, you will not use the strReturn that I did since you want to display the results in a nice format, so maybe some kind of datagrid would work better.

    3. To decide whether to use the basicHttpBinding or customBinding, you need to look at the differences between them and determine which best suites your needs.  The information is stored in the app.config file once you add this reference.  From a cursory glance, it seems that the customBinding is used if a significant amount of data is to be returned.  That would probably not be important for you.

    4. Finally, if you do want to investigate the service more, you can always click View / Object Browser to see all of your objects.  The MyWeather objects will be under your project in the treeview.  If you click on the various objects listed, you will be able to see different properties and methods that you can use. 

    Hope that helps!


    Christine A. Piffat

    • Marked as answer by Cooz2 Thursday, September 26, 2013 11:11 AM
    Tuesday, September 24, 2013 3:06 PM

All replies

  • In order to call a web service in visual studio, you need to use the following steps:

    1. Add a Service Reference to the weather web service.  Right mouse click on the project, select Add Service Reference, put the address for the weather web service in the address textbox, and click go.  The "Weather" service will appear under Services.  You can change the Namespace to something more descriptive than ServiceReference1, such as MyWeather, and then click OK.  At this point, the Service References folder appears with your Namespace listed underneath.

    2. Somewhere in your code where you want this service called (command or click event for a button, etc.), set up a client for this service.  This particular service adds two endpoints to your app.config file, so you need to decide which endpoint is best for you (more in a minute).  In my sample code, I just added the endpoint with basicHttpBinding, which is named "WeatherSoap".  The customBinding endpoint is named "WeatherSoap12".  The code is as follows (assuming you have a TextBox for zip code and a TextBlock with results).

    Using client As New MyWeather.WeatherSoapClient("WeatherSoap")
        Dim result As MyWeather.WeatherReturn = New MyWeather.WeatherReturn
        result = client.GetCityWeatherByZIP(txtZip.Text)
        Dim strReturn As String
        strReturn = "City " & result.City & ": " & result.Description & vbCrLf
        strReturn = strReturn & " has a current temp of " & result.Temperature
        txtResult.Text = strReturn
    End Using

    Obviously, you will not use the strReturn that I did since you want to display the results in a nice format, so maybe some kind of datagrid would work better.

    3. To decide whether to use the basicHttpBinding or customBinding, you need to look at the differences between them and determine which best suites your needs.  The information is stored in the app.config file once you add this reference.  From a cursory glance, it seems that the customBinding is used if a significant amount of data is to be returned.  That would probably not be important for you.

    4. Finally, if you do want to investigate the service more, you can always click View / Object Browser to see all of your objects.  The MyWeather objects will be under your project in the treeview.  If you click on the various objects listed, you will be able to see different properties and methods that you can use. 

    Hope that helps!


    Christine A. Piffat

    • Marked as answer by Cooz2 Thursday, September 26, 2013 11:11 AM
    Tuesday, September 24, 2013 3:06 PM
  • Hi Christine,

    This is exactly what I needed to know, explained in a clear and simple way.
    Thank you so much!

    Kind regards,
    Cooz

    Thursday, September 26, 2013 11:11 AM