web服务是可以跨网络远程访问类的属性和方法,而这些属性和方法 是通过wsdl描述xml文件! 他是microsoft在web编程发展远景的重要组成部分
web服务特点 1。web服务基于开放的标准构建的,所以你可以把它当作是一个通用的语言来使用,从而在一个组织内连接 不同的平台 2。web服务不止允许站点之间通讯,还允许站点和某个应用桌面程序通讯,以及桌面应用程序之间通讯 3。web服务允许聚集,它可以聚集来自多个站点甚至多个web服务的内容!
[WebMethod()] 写在一个属性或方法之上 自定义特性,表示向外公布这个方法,必须使用自定义特性和public修饰符,该方法可以作为web服务方法 公开!
[WebService (Description="对web服务的描述信息",Nam="web服务的名称,如果不设置默认就是类名",NameSpace="为web服务指定xml命名空间")] 这个是写在类的前面
web services 1。用http-get调用 http-get用来传递url请求或者提交带有method="get"表单的标准http协议 缺点:不安全,url请求会暴露信息 2。用http-post调用 提交带有method="post"表单的标准http协议 缺点:只能处理简单数据,对二进制流,表,dataset等不支持或支持不好! 3。用soap调用 用soap simple object access protocal 调用 用它来传递一些不能使用前两种不能传递的数据类型,比如dataset,自定义类,数组,二进制文件等 另外他没有被绑定到http协议,所以可以使用其它的协议
web service: using System; using System.Web; using System.Collections; using System.Web.Services; using System.Web.Services.Protocols;
/// <summary> /// WebService 的摘要说明 /// </summary>
//为web服务添加描述信息 第一个参数是对web服务的描述;第二个参数是web服务的名称,如果没有设置就是默认是类名;第三个是为web服务指定xml命名空间 [WebService ( Description="I Love You!",Name="LanLan",Namespace="http://localhost/www.qq.com")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class WebService : System.Web.Services.WebService {
public WebService () {
//如果使用设计的组件,请取消注释以下行 //InitializeComponent(); }
//自定义特性,表示向外公布这个方法,该方法必须和public修饰符一起使用才可以对作为web服务的公开方法 [WebMethod()] //这里定义了一个带参数的返回weather数据类型的方法 public Weather GetWeather(string citys) { Weather a = new Weather (); Weather b = new Weather (); Weather c = new Weather ();
Weather[] w = {a,b,c};
a.city = "wuhan"; a.tianqi = "re"; a.wendu = 2;
b.city = "haerbin"; b.tianqi = "leng"; b.wendu = 1; c.city = "fuzhou"; c.tianqi = "taifeng"; c.wendu = 1;
Weather weather = null; for (int i = 0; i < w.Length; i++) { string bs = citys; if (citys ==w[i].city ) { weather = w[i]; } } return weather; } }
//这个类属于自定义的类,和string一样,只不过string是系统封装好的,自定义类也可以通过编译以后封装起来,通过添加引用来使用 public class Weather { public Weather() { }
//定义三个字段,表示weather的三个基本特征 public string city; public string tianqi; public double wendu; }
调用: protected void btnClick_Click(object sender, EventArgs e) { localhost.LanLan lan = new localhost.LanLan(); localhost.Weather w = new localhost.Weather(); w = lan.GetWeather(txtCity.Text.Trim());
lblTianQi.Text = w.tianqi; lblWenDu.Text = Convert.ToString(w.wendu); }