WebService
在构建和使用Web Service时,主要用到以下几个关键的技术和规则:
1.XML:描述数据的标准方法.
2.SOAP:表示信息交换的协议.
3.WSDL:Web服务描述语言.
4.UDDI(Universal Description, Discovery and Integration):通用描述、发现与集成,它是一种独立于平台的,基于XML语言的用于在互联网上描述商务的协议。
OK 说一点实际的
新建一个网站项目, 然后创建 Web服务
微软创建了一个列子
1 /// <summary> 2 /// Test 的摘要说明 3 /// </summary> 4 [WebService(Namespace = "http://tempuri.org/")] 5 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 6 [System.ComponentModel.ToolboxItem(false)] 7 // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 8 // [System.Web.Script.Services.ScriptService] 9 public class Test : System.Web.Services.WebService 10 { 11 12 [WebMethod(Description="测试",MessageName="测试1")] 13 public string HelloWorld() 14 { 15 } 17 }
运行一下,就有结果了
WebMethod 以前也没怎么用,干脆一并看了。
.Description
.EnableSession
.MessageName
.TransactionOption
.CacheDuration
.BufferResponse
有这六个属性
1.Description 描述,类似注释,比如我调用某个 webService 这就是我能看见的注释
2.EnableSession:是否启用Session
3.MessageName: 重名。
4.TransactionOption:指示 XML Web services 方法的事务支持
5.RequiresNew:指示 XML Web services 方法需要新事务 PS: 4,5我不是很懂。 有用到再说
6.CacheDuration: 缓存, 如果一个Services 被大量调用,而你的数据又不是时时更新的. 那就需要他
7.BufferResponse : 类似HTML 里面的? 当有大量数据的时候 配置WebService方法是否等到响应被完全缓冲完,才发送信息给请求端
OK 开搞,先创建一个WebService
/// <summary> /// WebService1 的摘要说明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 // [System.Web.Script.Services.ScriptService] public class WebService1 : System.Web.Services.WebService { public static int count = 0; [WebMethod(Description="累计访问人数")] public int HelloWorld() { count++; return count; } }
OK 创建好了
我重新打开一个工程。 对我现在创建的 WebService 添加引用
然后你就可以调用了
protected void Page_Load(object sender, EventArgs e) { localhost.WebService1 w = new test1.localhost.WebService1(); Response.Write(w.HelloWorld()); }
结果就是 每次刷新加1。 就是这样
做的这里就想啊。 万一哪天我不紧紧是要返回点儿 整数怎么办
[System.SerializableAttribute()] public class a { public string message = "测试1"; public string message2 = "测试2"; public a() { } }
创建好 允许序列化 就ok了.
[WebMethod(Description = "a")] public a test1() { a o = new a(); return o; }
当然 不管你怎么变,他都是序列化一串xml 传给对方
然后我测试了一下网上的一个Service
http://www.webxml.com.cn/WebServices/WeatherWebService.asmx
还是先添加引用
tianqi.WeatherWebService w = new test1.tianqi.WeatherWebService(); var a = w.getWeatherbyCityName("成都"); for (var i = 0; i < a.Length; i++) { Response.Write(a[i]+"<br />"); }
OK
下面
通过WSDL文件生成WebService 代码
http://www.cnblogs.com/liyi93/archive/2012/01/30/2332320.html
很多时候 只给你一个WSDL 的时候 就靠他了。
坑_JS 调用WebService
最后. WebService 已经逐渐被淘汰了, 都用WCF了.
但是作为技术新人, 稍微花点儿时间还是有必要的。 更何况项目还要用。
参考资料 感谢你们的分享。
http://www.w3school.com.cn/webservices/
http://baike.baidu.com/view/837392.htm
http://www.cnblogs.com/capricornus/archive/2010/08/17/1801596.html
http://www.cnblogs.com/liubiqu/archive/2005/06/06/168623.html
http://www.cnblogs.com/liyi93/archive/2012/01/30/2332320.html
http://webservices.seekda.com/browse
PS: 查资料的时候,在评论里面看到 有人做了WebServices 的搜索引擎 所以也拖了过来