您的位置:首页 > 其它

WebService入门案例

2014-05-30 11:32 309 查看
关于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。页面如下:

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-----------------------------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: