C#开发和调用Web Service

Web Service基本概念

  Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级的独立的通讯技术。是通过SOAP在Web上提供的软件服务,使用WSDL文件进行说明,并通过UDDI进行注册。

  XML:(Extensible Markup Language)扩展型可标记语言。面向短期的临时数据处理、面向万维网络,是Soap的基础。

  Soap:(Simple Object Access Protocol)简单对象存取协议。是XML Web Service 的通信协议。当用户通过UDDI找到你的WSDL描述文档后,他通过可以SOAP调用你建立的Web服务中的一个或多个操作。SOAP是XML文档形式的调用方法的规范,它可以支持不同的底层接口,像HTTP(S)或者SMTP。

  WSDL:(Web Services Description Language) WSDL 文件是一个XML文档,用于说明一组SOAP 消息以及如何交换这些消息。大多数情况下由软件自动生成和使用。

  UDDI (Universal Description, Discovery, and Integration) 是一个主要针对Web服务供应商和使用者的新项目。在用户能够调用Web服务之前,必须确定这个服务内包含哪些商务方法,找到被调用的接口定义,还要在服务端来编制软件,UDDI是一种根据描述文档来引导系统查找相应服务的机制。UDDI利用SOAP消息机制(标准的XML/HTTP)来发布,编辑,浏览以及查找注册信息。它采用XML格式来封装各种不同类型的数据,并且发送到注册中心或者由注册中心来返回需要的数据。

Web Service开发

  .net平台内建了对Web Service的支持,包括Web Service的构建和使用。与其它开发平台不同,使用.net平台,你不需要其他的工具或者SDK就可以完成Web Service的开发了。.net Framework本身就全面支持Web Service,包括服务器端的请求处理器和对客户端发送和接受SOAP消息的支持。下来我们就一步一步的用Microsoft Visual Studio .net 2005(后面简称VS.Net 2005)创建和使用一个简单的Web Service。

  1、新建web服务(vs2013)

2、打开Service.cs文件  修改成如下图所示:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.Services;
 6 
 7 /// <summary>
 8 /// WebService 的摘要说明
 9 /// </summary>
10 [WebService(Namespace = "TextWebService")]
11 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
12 // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 
13 // [System.Web.Script.Services.ScriptService]
14 public class WebService : System.Web.Services.WebService {
15 
16     public WebService () {
17 
18         //如果使用设计的组件,请取消注释以下行 
19         //InitializeComponent(); 
20     }
21 
22     [WebMethod]
23     public string HelloWorld() {
24         return "Hello World";
25     }
26     [WebMethod(Description="获取姓名")]
27     public string GetName() 
28     {
29         return "wangming";
30     }
31     [WebMethod(Description = "求和的方法")]
32     public double Add(double i,double j)
33     {
34         return i + j;
35     }
36     [WebMethod(Description = "求差的方法")]
37     public double subtract(double i, double j)
38     {
39         return i - j;
40     }
41     [WebMethod(Description = "求积的方法")]
42     public double multiplication(double i, double j)
43     {
44         return i * j;
45     }
46     [WebMethod(Description = "求商的方法")]
47     public double division(double i, double j)
48     {
49         if (j != 0)
50         {
51             return i / j;
52         }
53         else
54         {
55             return 0;
56         }
57     }
58     //[WebMethod(Description = "连接数据库")]
59     //public void ConntDatabase(string connStr)
60     //{
61  
62     //}
63 }
View Code

3、webservice的发布

  在项目上点击右键,点击菜单中的发布

3、开始运行Service.asmx 文件

4、添加引用

点击高级,如下图所示:

至此,添加引用完成。

下面介绍一下webservice的调用:

新建一个网页页面,添加几个控件:

 1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
 2 
 3 <!DOCTYPE html>
 4 
 5 <html xmlns="http://www.w3.org/1999/xhtml">
 6 <head runat="server">
 7     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 8     <title>Webservice调用实例</title>
 9 </head>
10 <body>
11     <form id="form1" runat="server">
12         <div>
13             <asp:TextBox ID="Num1" runat="server"></asp:TextBox>
14             <select id="selectOper" runat="server">
15                 <option>+</option>
16                 <option>-</option>
17                 <option>*</option>
18                 <option>/</option>
19             </select>
20             <asp:TextBox ID="Num2" runat="server"></asp:TextBox>
21             <span id="E" runat="server">=</span>
22             <asp:TextBox ID="Result" runat="server"></asp:TextBox>
23             <br />
24 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
25             <asp:Button ID="Button_Submitt" runat="server" OnClick="Button_Submitt_Click" Text="Submitt" />
26         </div>
27     </form>
28 </body>
29 </html>
View Code

后台代码如下:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.UI;
 6 using System.Web.UI.WebControls;
 7 
 8 public partial class _Default : System.Web.UI.Page
 9 {
10     protected void Page_Load(object sender, EventArgs e)
11     {
12     }
13     protected void Button_Submitt_Click(object sender, EventArgs e)
14     {
15         if (Num1.Text != "" && Num2.Text != "")
16         {
17             double i = Convert.ToDouble(Num1.Text.ToString());
18             double j = Convert.ToDouble(Num2.Text.ToString());
19 
20             //实例化WebService实例对象
21             localhost.WebService WebserviceInstance = new localhost.WebService();
22             int Oper = selectOper.SelectedIndex;
23             switch (Oper)
24             {
25                 case 0:
26                     Result.Text = WebserviceInstance.Add(i,j).ToString();
27                     break;
28                 case 1:
29                     Result.Text = WebserviceInstance.subtract(i,j).ToString();
30                     break;
31                 case 2:
32                     Result.Text = WebserviceInstance.multiplication(i, j).ToString();
33                     break;
34                 case 3:
35                     Result.Text = WebserviceInstance.division(i,j).ToString();
36                     break;
37             }
38         }
39     }
40 }
View Code

  到此一个一个简单的WebService的开发和调用就已经完成了,在实际应用中可以根据自己的需要,写一些功能强大的,复杂的WebService,不管多么复杂,整个流程都是这样的。

参考:http://blog.csdn.net/h0322/article/details/4776819

原文地址:https://www.cnblogs.com/wmcoder/p/5230744.html