關於WebService的作用和好處,大家應該都了解。但如何在Asp.Net中添加Web Service等問題一直是新手朋友的一大難題。鑒於網上沒有一個像樣的實際案例,特將課程設計中運用到的WebService(在Asp.Net中添加實時天氣預報的案例)粘貼出來,並附上個人的理解。以便於新手朋友快速入門WebService,幫助新手朋友快速的掌握和理解如何在ASP.NET中建立這種引用,實現網頁上面的高大效果。對於本文,歡迎轉載,但請標明文章出處:itred.cnblogs.com。歡迎各位大神拍磚,指正!郵箱:it_red@sina.com。
本案例的開發環境是:visual studio 2010
后台開發的語言:C#
首先簡單介紹一下WebService:
我的理解就是引用別個程序的接口。通過這個提供web服務的接口方法在自己的網絡平台上實現需要應用的那個程序平台的服務。而這個應用是和語言無關,和開發平台無關,實際上就是通過這個服務,可以調用任何平台的任何功能程序,但是前提是這個應用程序是提供了這樣的接口供其他用戶調用。
我實現的這個DEMO就是通過自己的ASP.NET調用這個網站目錄下提供的WebService服務,其網址為:http://www.webxml.com.cn/zh_cn/index.aspx。頁面如下:
通過調用,在我自己的ASP.NET 頁面上顯示如上天氣預報的相關信息。
我認為應該了解的關於WebService的基本特征:
(1) 互操作性:任何的 Web Service都可以與其他 Web Service進行交互。由於有了SOAP這個所有主要供應商都支持的新標准協議,因而避免了在CORBA、DCOM 和其他協議之間轉換的麻煩。還因為可以使用任何語言來編寫 Web Service,因此開發者無需更改其開發環境,就可編寫和使用 Web Service;
(2) 普遍性:Web Service使用 HTTP 和 XML 進行通信。因此,任何支持這些技術的設備都可以擁有和訪問 Web Service。你可以通過Internet Explore瀏覽器來訪問它,你甚至也可以通過智能手機來訪問它;
(3) Web 服務的一個重要特征是支持松散耦合。一個應用程序可以分解為多個邏輯服務組件,其中的每個服務組件都可以使用結構框架構建,也可以在物理上分布於多個計算機之間。松散耦合的另一個重要方面是 Web 服務只在需要時實時集成。
下面是我的DEMO的實現過程:
- 在visual studio 2010中新建一個C#語言的空網站;
- 添加Web引用: 鼠標右擊該網站項目,添加Web引用;
3. 添加一些配置信息:
第一步:在URL中添加提供這個WebService的網址,本DEMO中的天氣預報服務的網址是:http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx;
第二步:在Web引用名中,我們采用默認給出的名稱:cn.com.webxml.webservice;
第三步:點擊添加引用,即確認了該WebService.
4. 我們需要顯示天氣的圖標,此時,我們需要在webservice 提供的服務上面下載一些相關的圖標和一些說明性文檔;
點擊進入后看到的頁面如下示:
打開這個pdf的文檔,找到下面的網址,下載關於天氣的圖標:
具體的網址可復制如下:

相關鏈接:
部分JAVA開發工具在調用由.NET開發的WEB服務時WSDL出錯請訪問以下鏈
接:http://www.webxml.com.cn/h/6C533855614658756258513D.aspx
Web Services GET 方法調
用:http://www.webxml.com.cn/h/7A636E424F763363446B303D.aspx
Java調用.Net WebService 發生異常:服務器未能識別 HTTP 頭 SOAPAction 的值:
http://www.webxml.com.cn/h/5A416E4C4F6A31526D4D453D.aspx
JAVA讀 .NET開發的WEB服務 DataSet 數據可參考以下鏈
接:http://download.csdn.net/source/698391 , http://www.webxml.com.cn/d/44587637
472B4E426377453D.aspx,http://www.webxml.com.cn/h/4F65627450716E5A6832493D
.aspx
C# 轉 VB.net:http://labs.developerfusion.co.uk/convert/csharp-to-vb.aspx
VB.net 轉 C#:http://labs.developerfusion.co.uk/convert/vb-to-csharp.aspx
服務支持 http://www.webxml.com.cn/zh_cn/support.aspx
下載中心 http://www.webxml.com.cn/zh_cn/download_center.aspx
天氣圖標下載:http://www.webxml.com.cn/images/weather.zip
天氣現象和圖例:http://www.webxml.com.cn/zh_cn/weather_icon.aspx
部分城市/地區代表圖片:http://www.webxml.com.cn/files/city_photo.zip
部分城市/地區介紹和氣候背景資料:http://www.webxml.com.cn/files/about_city.zip
下載后,將weather文件夾拷貝到自己建立的網站目錄下,新建一個測試性的Asp.net網頁Default.aspx,現在的網站基本目錄結構如下圖所示:
5.根據說明文檔,建立自己需要的數據信息數組,寫自己的網頁顯示。調用給出的數據信息。
頁面布局如下:
在后台的頁面中需要將dropdownlist的屬性進行如下設置:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" onselectedindexchanged="DropDownList1_SelectedIndexChanged"> </asp:DropDownList> <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" onselectedindexchanged="DropDownList2_SelectedIndexChanged"> </asp:DropDownList>
這個aspx頁面后台的C#源代碼為:

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; public partial class _Default : System.Web.UI.Page { cn.com.webxml.webservice.WeatherWS proxy1; String[] weathers; string cityCode; DataSet ds; protected void Page_Load(object sender, EventArgs e) { proxy1 = new cn.com.webxml.webservice.WeatherWS();//生成代理 if (!Page.IsPostBack) { ds = proxy1.getRegionDataset(); DropDownList1.DataSource = ds.Tables[0]; DropDownList1.DataTextField = "RegionName"; DropDownList1.DataValueField = "RegionID"; DropDownList1.DataBind(); ds = proxy1.getSupportCityDataset(DropDownList1.SelectedItem.ToString()); DropDownList2.DataSource = ds.Tables[0]; DropDownList2.DataTextField = "CityName"; DropDownList2.DataValueField = "CityID"; DropDownList2.DataBind(); } } protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { ds = proxy1.getSupportCityDataset(DropDownList1.SelectedItem.ToString()); DropDownList2.DataSource = ds.Tables[0]; DropDownList2.DataTextField = "CityName"; DropDownList2.DataValueField = "CityID"; DropDownList2.DataBind(); } protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e) { cityCode = DropDownList2.SelectedItem.ToString(); weathers = proxy1.getWeather(cityCode, ""); Label1.Text = weathers[8]; Label2.Text = weathers[13]; Label3.Text = weathers[18]; Label4.Text = weathers[9]; Label5.Text = weathers[14]; Label6.Text = weathers[19]; Label7.Text = weathers[7]; Label8.Text = weathers[12]; Label9.Text = weathers[17]; Image1.ImageUrl = "weather/" + weathers[10]; Image2.ImageUrl = "weather/" + weathers[15]; Image3.ImageUrl = "weather/" + weathers[20]; } }
6. 運行效果如下圖所示:
至此,一個關於WebService的案例就順利完成了。我相信你也應該能理解這種服務的特點了吧。
若有任何問題,歡迎至郵:it_red@sina.com
-----------------------------------------版權所有! 轉載請標明出處:itred.cnblogs.com-----------------------------------------