DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Accessing A Web Service

06.19.2008
| 7280 views |
  • submit to reddit
        1. In Visual Studio, add a web reference in the project explorer.

2. Create an class describing an object that represents the web service.

    public class CurrentWeather
    {

        #region attributes

        private String location;
        private String time;
        private String wind;
        private String visibility;
        private String temperature;
        private String dewPoint;
        private String relativeHumidity;
        private String pressure;
        private String status;

        #endregion


        #region properties

        public String Location { get { return this.location; } set { this.location = value; } }
        public String Time { get { return this.time; } set { this.time = value; } }
        public String Wind { get { return this.wind; } set { this.wind = value; } }
        public String Visibility { get { return this.visibility; } set { this.visibility = value; } }
        public String Temperature { get { return this.temperature; } set { this.temperature = value; } }
        public String DewPoint { get { return this.dewPoint; } set { this.dewPoint = value; } }
        public String RelativeHumidity { get { return this.relativeHumidity; } set { this.relativeHumidity = value; } }
        public String Pressure { get { return this.pressure; } set { this.pressure = value; } }
        public String Status { get { return this.status; } set { this.status = value; } }

        #endregion

    }


Use a method like this to call the service

        private void btRead_Click(object sender, EventArgs e)
        {
            net.webservice.www.MyWeather gw = new net.webservice.www.MyWeather ();
            this.lblShowStatus.Text = "Accessing Service";
            this.Refresh();
 
            Objects.CurrentWeather cwo = new Objects.CurrentWeather();
            System.Xml.Serialization.XmlSerializer xs =
                new System.Xml.Serialization.XmlSerializer(typeof(Objects.CurrentWeather));

            // Deserialize
            try
            {
                System.IO.TextReader tr = new System.IO.StringReader(gw.GetWeather(this.tbCity.Text, this.tbCountry.Text));
                cwo = (Objects.CurrentWeather)xs.Deserialize(tr);
                this.lblShowStatus.Text = cwo.Status;
                this.lbShowHumidity.Text = cwo.RelativeHumidity;
                this.lbShowPressure.Text = cwo.Pressure;
                this.lbShowWind.Text = cwo.Wind;
                this.lbShowTemperature.Text = cwo.Temperature;
            }
            catch
            {
                this.lblShowStatus.Text = String.Empty;
                this.lbShowHumidity.Text = String.Empty;
                this.lbShowPressure.Text = String.Empty;
                this.lbShowWind.Text = String.Empty;
                this.lbShowTemperature.Text = String.Empty;
                this.lblShowStatus.Text = "Error";
            }
        }