C# WebService 使用(簡單天氣查詢)

來源:互聯網
上載者:User

2010-03-08近日下班過早回來無聊,今天偶然看到C#訪問webService心血來潮寫了個簡單的天氣查詢。

首先添加WebReference :

http://www.webxml.com.cn/WebServices/WeatherWebService.asmx

 

擷取天氣Service類:

 using System;<br />using System.Collections.Generic;<br />using System.Text;</p><p>namespace WeatherQuery.SL<br />{<br /> class WeatherService<br /> {<br /> #region Variables<br /> private WeatherWebService.WeatherWebService ServiceClient = new WeatherQuery.WeatherWebService.WeatherWebService();<br /> #endregion</p><p> #region Methods<br /> /// <summary><br /> ///<br /> /// </summary><br /> /// <returns></returns><br /> public string [] GetAllServiceArea()<br /> {<br /> try<br /> {<br /> return ServiceClient.getSupportProvince();<br /> }<br /> catch (Exception ex)<br /> {<br /> throw ex;<br /> }<br /> }</p><p> /// <summary><br /> ///<br /> /// </summary><br /> /// <param name="areaName"></param><br /> /// <returns></returns><br /> public string[] GetAreaCitiesInArea(string areaName)<br /> {<br /> try<br /> {<br /> return ServiceClient.getSupportCity(areaName);<br /> }<br /> catch (Exception ex)<br /> {<br /> throw ex;<br /> }<br /> }</p><p> /// <summary><br /> ///<br /> /// </summary><br /> /// <param name="cityName"></param><br /> /// <returns></returns><br /> public string[] GetWeatherByCityName(string cityName)<br /> {<br /> try<br /> {<br /> return ServiceClient.getWeatherbyCityName(cityName);<br /> }<br /> catch (Exception ex)<br /> {<br /> throw ex;<br /> }<br /> }<br /> #endregion<br /> }<br />}<br />

BLL ServiceHelper類:

using System;<br />using System.Collections.Generic;<br />using System.Text;</p><p>using WeatherQuery.SL;</p><p>namespace WeatherQuery.BLL<br />{<br /> class WeatherServiceHelper<br /> {<br /> #region Variables<br /> private WeatherService weatherService = new WeatherService();<br /> #endregion</p><p> #region Methods<br /> public string[] GetAllSupportArea(out string errorMessage)<br /> {<br /> try<br /> {<br /> errorMessage = string.Empty;<br /> return weatherService.GetAllServiceArea();<br /> }<br /> catch (Exception ex)<br /> {<br /> errorMessage = ex.Message;<br /> return null;<br /> }<br /> }</p><p> public string[] GetAllCitiesByArea(string areaName, out string errorMessage)<br /> {<br /> if (string.IsNullOrEmpty(areaName))<br /> {<br /> errorMessage = "Can not query null area!";<br /> return null;<br /> }<br /> else<br /> {<br /> try<br /> {<br /> errorMessage = string.Empty;<br /> return weatherService.GetAreaCitiesInArea(areaName);<br /> }<br /> catch (Exception ex)<br /> {<br /> errorMessage = ex.Message;<br /> return null;<br /> }<br /> }<br /> }</p><p> public string[] GetWeatherMessageOfCity(string cityName, out string errorMessage)<br /> {<br /> if (string.IsNullOrEmpty(cityName))<br /> {<br /> errorMessage = "Can not query null city!";<br /> return null;<br /> }<br /> else<br /> {<br /> try<br /> {<br /> errorMessage = string.Empty;<br /> //cityName =="北京(xxxx)"<br /> string cName = cityName.Split(new char[] { '(' })[0];<br /> return weatherService.GetWeatherByCityName(cName);<br /> }<br /> catch (Exception ex)<br /> {<br /> errorMessage = ex.Message;<br /> return null;<br /> }<br /> }<br /> }<br /> #endregion<br /> }<br />}<br /> 

UI 層 (Winform 2.0) 

using System;<br />using System.Collections.Generic;<br />using System.ComponentModel;<br />using System.Data;<br />using System.Drawing;<br />using System.Text;<br />using System.Windows.Forms;</p><p>namespace WeatherQuery<br />{<br /> public partial class MainFrom : Form<br /> {<br /> public MainFrom()<br /> {<br /> InitializeComponent();<br /> }</p><p> private void btnOK_Click(object sender, EventArgs e)<br /> {<br /> string errorMessage;<br /> string[] weatherMessage = weatherServiceHelper.GetWeatherMessageOfCity(cBoxCity.Text, out errorMessage);<br /> if (weatherMessage == null)<br /> {<br /> MessageBox.Show(errorMessage);<br /> }<br /> else<br /> {<br /> lBoxWeatherMessage.DataSource=weatherMessage ;<br /> }<br /> }</p><p> private void btnCanel_Click(object sender, EventArgs e)<br /> {<br /> InitArea();<br /> }</p><p> private void MainFrom_Load(object sender, EventArgs e)<br /> {<br /> InitArea();<br /> }</p><p> private void cBoxArea_SelectedIndexChanged(object sender, EventArgs e)<br /> {<br /> string errorMessage;<br /> string[] cities = weatherServiceHelper.GetAllCitiesByArea(cBoxArea.Text, out errorMessage);<br /> if (cities == null)<br /> {<br /> MessageBox.Show(errorMessage);<br /> }<br /> else<br /> {<br /> cBoxCity.DataSource = cities;<br /> }<br /> }</p><p> private void InitArea()<br /> {<br /> string errorMessage;<br /> string[] arealist = weatherServiceHelper.GetAllSupportArea(out errorMessage);<br /> if (arealist == null)<br /> {<br /> MessageBox.Show(errorMessage);<br /> }<br /> else<br /> {<br /> cBoxArea.DataSource = arealist;<br /> lBoxWeatherMessage.DataSource = null;<br /> }<br /> }<br /> }<br />}<br />

namespace WeatherQuery<br />{<br /> partial class MainFrom<br /> {<br /> /// <summary><br /> /// 必需的設計器變數。<br /> /// </summary><br /> private System.ComponentModel.IContainer components = null;</p><p> /// <summary><br /> /// 清理所有正在使用的資源。<br /> /// </summary><br /> /// <param name="disposing">如果應釋放託管資源,為 true;否則為 false。</param><br /> protected override void Dispose(bool disposing)<br /> {<br /> if (disposing && (components != null))<br /> {<br /> components.Dispose();<br /> }<br /> base.Dispose(disposing);<br /> }</p><p> #region Windows 表單設計器產生的程式碼</p><p> /// <summary><br /> /// 設計器支援所需的方法 - 不要<br /> /// 使用代碼編輯器修改此方法的內容。<br /> /// </summary><br /> private void InitializeComponent()<br /> {<br /> this.cBoxArea = new System.Windows.Forms.ComboBox();<br /> this.cBoxCity = new System.Windows.Forms.ComboBox();<br /> this.btnOK = new System.Windows.Forms.Button();<br /> this.btnCanel = new System.Windows.Forms.Button();<br /> this.lBoxWeatherMessage = new System.Windows.Forms.ListBox();<br /> this.SuspendLayout();<br /> //<br /> // cBoxArea<br /> //<br /> this.cBoxArea.FormattingEnabled = true;<br /> this.cBoxArea.Location = new System.Drawing.Point(28, 70);<br /> this.cBoxArea.Name = "cBoxArea";<br /> this.cBoxArea.Size = new System.Drawing.Size(165, 20);<br /> this.cBoxArea.TabIndex = 0;<br /> this.cBoxArea.SelectedIndexChanged += new System.EventHandler(this.cBoxArea_SelectedIndexChanged);<br /> //<br /> // cBoxCity<br /> //<br /> this.cBoxCity.FormattingEnabled = true;<br /> this.cBoxCity.Location = new System.Drawing.Point(291, 70);<br /> this.cBoxCity.Name = "cBoxCity";<br /> this.cBoxCity.Size = new System.Drawing.Size(169, 20);<br /> this.cBoxCity.TabIndex = 1;<br /> //<br /> // btnOK<br /> //<br /> this.btnOK.Location = new System.Drawing.Point(118, 459);<br /> this.btnOK.Name = "btnOK";<br /> this.btnOK.Size = new System.Drawing.Size(75, 23);<br /> this.btnOK.TabIndex = 3;<br /> this.btnOK.Text = "Query";<br /> this.btnOK.UseVisualStyleBackColor = true;<br /> this.btnOK.Click += new System.EventHandler(this.btnOK_Click);<br /> //<br /> // btnCanel<br /> //<br /> this.btnCanel.Location = new System.Drawing.Point(291, 459);<br /> this.btnCanel.Name = "btnCanel";<br /> this.btnCanel.Size = new System.Drawing.Size(75, 23);<br /> this.btnCanel.TabIndex = 4;<br /> this.btnCanel.Text = "ReSelect";<br /> this.btnCanel.UseVisualStyleBackColor = true;<br /> this.btnCanel.Click += new System.EventHandler(this.btnCanel_Click);<br /> //<br /> // lBoxWeatherMessage<br /> //<br /> this.lBoxWeatherMessage.FormattingEnabled = true;<br /> this.lBoxWeatherMessage.HorizontalScrollbar = true;<br /> this.lBoxWeatherMessage.ItemHeight = 12;<br /> this.lBoxWeatherMessage.Location = new System.Drawing.Point(28, 130);<br /> this.lBoxWeatherMessage.Name = "lBoxWeatherMessage";<br /> this.lBoxWeatherMessage.ScrollAlwaysVisible = true;<br /> this.lBoxWeatherMessage.Size = new System.Drawing.Size(544, 316);<br /> this.lBoxWeatherMessage.TabIndex = 5;<br /> //<br /> // MainFrom<br /> //<br /> this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);<br /> this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;<br /> this.ClientSize = new System.Drawing.Size(607, 494);<br /> this.Controls.Add(this.lBoxWeatherMessage);<br /> this.Controls.Add(this.btnCanel);<br /> this.Controls.Add(this.btnOK);<br /> this.Controls.Add(this.cBoxCity);<br /> this.Controls.Add(this.cBoxArea);<br /> this.Name = "MainFrom";<br /> this.Text = "Simple weather query";<br /> this.Load += new System.EventHandler(this.MainFrom_Load);<br /> this.ResumeLayout(false);</p><p> }</p><p> #endregion</p><p> private System.Windows.Forms.ComboBox cBoxArea;<br /> private System.Windows.Forms.ComboBox cBoxCity;<br /> private System.Windows.Forms.Button btnOK;<br /> private System.Windows.Forms.Button btnCanel;<br /> private System.Windows.Forms.ListBox lBoxWeatherMessage;<br /> private WeatherQuery.BLL.WeatherServiceHelper weatherServiceHelper = new WeatherQuery.BLL.WeatherServiceHelper();<br /> }<br />}</p><p>

 

實在是屬於無聊,寫著玩的

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.