C#调用webservice

Web Service开发

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

2.1、用创建一个最简单的Web Service

首先,打开VS2005,打开"文件-新建-网站",选择"ASP.NET Web服务"

 

查看Service.cs代码,你会发现VS.Net 2005已经为Web Service文件建立了缺省的框架。原始代码为:

 1 view plaincopy to clipboardprint?
 2 using System;   
 3 using System.Linq;   
 4 using System.Web;   
 5 using System.Web.Services;   
 6 using System.Web.Services.Protocols;   
 7 using System.Xml.Linq;   
 8 [WebService(Namespace = "http://tempuri.org/")]   
 9 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]   
10 // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。   
11 // [System.Web.Script.Services.ScriptService]   
12 public class Service : System.Web.Services.WebService   
13 {   
14     public Service () {   
15         //如果使用设计的组件,请取消注释以下行    
16         //InitializeComponent();    
17     }   
18     [WebMethod]   
19     public string HelloWorld()   
20     {   
21         return "Hello World";   
22     }   
23 }  
24 using System;
25 using System.Linq;
26 using System.Web;
27 using System.Web.Services;
28 using System.Web.Services.Protocols;
29 using System.Xml.Linq;
30 [WebService(Namespace = "http://tempuri.org/")]
31 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
32 // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
33 // [System.Web.Script.Services.ScriptService]
34 public class Service : System.Web.Services.WebService
35 {
36     public Service () {
37         //如果使用设计的组件,请取消注释以下行 
38         //InitializeComponent(); 
39     }
40     [WebMethod]
41     public string HelloWorld()
42     {
43         return "Hello World";
44     }
45 } 
Service.cs

默认工程里面已经有一个Hello World的方法了,直接运行看看效果,

 

点击显示页面上图中的"HelloWorld"超链接,跳转到下一页面:

 

点击"调用"按钮,就可以看到用XML格式返回的Web Service结果下图。说明我们的Web Service环境没有问题,而且还初步接触了一下最简单的Web Service。

2.2、创建一个简单带有功能的Web Service

上面我们宏观的了解了webservice,其实它就是个对外的接口,里面有函数可供外部客户调用(注意:里面同样有客户不可调用的函数).假若我们是服务端,我们写好了个webservice,然后把它给了客户(同时我们给了他们调用规则),客户就可以在从服务端获取信息时处于一个相对透明的状态.即是客户不了解(也不需要)其过程,他们只获取数据.在代码文件里,如果我们写了一个函数后,希望此函数成为外部可调用的接口函数,我们必须在函数上面添上一行代码[WebMethod(Description="函数的描述信息")],如果你的函数没有这个申明,它将不能被用户引用.下来我们开始编写一个简单的Web Service 的例子。

先把默认的HelloWorld方法注释掉,简单的写了求加减乘除运算的四个方法;

 1 view plaincopy to clipboardprint?
 2 using System;   
 3 using System.Linq;   
 4 using System.Web;   
 5 using System.Web.Services;   
 6 using System.Web.Services.Protocols;   
 7 using System.Xml.Linq;   
 8 [WebService(Namespace = "http://tempuri.org/")]   
 9 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]   
10 // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。   
11 // [System.Web.Script.Services.ScriptService]   
12 public class Service : System.Web.Services.WebService   
13 {   
14     public Service () {   
15         //如果使用设计的组件,请取消注释以下行    
16         //InitializeComponent();    
17     }   
18     //[WebMethod]   
19     //public string HelloWorld()   
20     //{   
21     //    return "Hello World";   
22     //}   
23     [WebMethod(Description = "求和的方法")]   
24     public double addition(double i, double j)   
25     {   
26         return i + j;   
27     }   
28     [WebMethod(Description = "求差的方法")]   
29     public double subtract(double i, double j)   
30     {   
31         return i - j;   
32     }   
33     [WebMethod(Description = "求积的方法")]   
34     public double multiplication(double i, double j)   
35     {   
36         return i * j;   
37     }   
38     [WebMethod(Description = "求商的方法")]   
39     public double division(double i, double j)   
40     {   
41         if (j != 0)   
42             return i / j;   
43         else  
44             return 0;   
45     }   
46 }  
47 using System;
48 using System.Linq;
49 using System.Web;
50 using System.Web.Services;
51 using System.Web.Services.Protocols;
52 using System.Xml.Linq;
53 [WebService(Namespace = "http://tempuri.org/")]
54 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
55 // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
56 // [System.Web.Script.Services.ScriptService]
57 public class Service : System.Web.Services.WebService
58 {
59     public Service () {
60         //如果使用设计的组件,请取消注释以下行 
61         //InitializeComponent(); 
62     }
63     //[WebMethod]
64     //public string HelloWorld()
65     //{
66     //    return "Hello World";
67     //}
68     [WebMethod(Description = "求和的方法")]
69     public double addition(double i, double j)
70     {
71         return i + j;
72     }
73     [WebMethod(Description = "求差的方法")]
74     public double subtract(double i, double j)
75     {
76         return i - j;
77     }
78     [WebMethod(Description = "求积的方法")]
79     public double multiplication(double i, double j)
80     {
81         return i * j;
82     }
83     [WebMethod(Description = "求商的方法")]
84     public double division(double i, double j)
85     {
86         if (j != 0)
87             return i / j;
88         else
89             return 0;
90     }
91 }
92  
Hello word

运行可以看到我们自己写的可以被调用的方法,如下图:

 

同样点击addition方法,进入addition方法的调用页。

 

在参数上面输入参数i=3,j=3,如上图,点击调用,就可以看到用XML格式返回的Web Service结果(i与j相加的结果)下图

 到这里,我们会发现,其实webservice并不是那么的神秘,它也不过只是个接口,对我们而言,侧重点就是是接口函数的编写.

2.3、用ASP.NET调用Web Service
首先,打开VS2005,打开"文件-新建-网站",选择"ASP.NET网站"。

 

选好存储位置,语言后点击确定,进入默认页面。然后先添加Web引用,把WebService引到当前的工程里面。方法是:在资源管理器中点击右键,选择添加Web 引用,调出对话框:

 

在URL中填入,前面写好的WebService运行后浏览器上面显示的地址(即:WebService发布后的访问地址 ),点击"前往"按钮,如上图,就会显示出所引用的WebService中可以调用的方法,然后点击"添加引用",就将webservice引用到了当前的工程里面 ,如下图,解决方案中会出现引进来的WebService文件

 

我们在这就练习调用webservice的四个方法,做一个简单的调用的例子,先在网站的前台添加几个控件,代码如下:

 1 view plaincopy to clipboardprint?
 2 <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>   
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">   
 4 <html xmlns="http://www.w3.org/1999/xhtml">   
 5 <head runat="server">   
 6     <title>无标题页</title>   
 7 </head>   
 8 <body>   
 9     <form id="form1" runat="server">   
10     <div>   
11         <asp:TextBox ID="Num1" runat="server"></asp:TextBox>   
12              <select id="selectOper" runat = "server">   
13                  <option>+</option>   
14                  <option>-</option>   
15                  <option>*</option>   
16                  <option>/</option>   
17              </select>   
18              <asp:TextBox ID="Num2" runat="server"></asp:TextBox>   
19         <asp:Button ID="Button1" runat="server" Text="=" onclick="Button1_Click" />   
20              <asp:TextBox ID="Result" runat="server"></asp:TextBox>   
21     </div>   
22     </form>   
23 </body>   
24 </html>  
25 <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
26 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
27 <html xmlns="http://www.w3.org/1999/xhtml">
28 <head runat="server">
29     <title>无标题页</title>
30 </head>
31 <body>
32     <form id="form1" runat="server">
33     <div>
34         <asp:TextBox ID="Num1" runat="server"></asp:TextBox>
35              <select id="selectOper" runat = "server">
36                  <option>+</option>
37                  <option>-</option>
38                  <option>*</option>
39                  <option>/</option>
40              </select>
41              <asp:TextBox ID="Num2" runat="server"></asp:TextBox>
42         <asp:Button ID="Button1" runat="server" Text="=" onclick="Button1_Click" />
43              <asp:TextBox ID="Result" runat="server"></asp:TextBox>
44     </div>
45     </form>
46 </body>
47 </html>
48  
调用webService方法

然后在后台写调用的代码,调用之前和使用其它的对象一样,要先实例化,实例化的方法是localhost.Service a = new localhost.Service();然后就可以通过a来访问WebService里面提供的方法了。在这个例子里面,动态的创建了一个button控件来触发WebService的调用,后台代码如下:
运行后可以看到效果,如下图所示,在前面两个Textbox里面输入两个操作数,在中间的下拉列表中选择操作符,然后点击"="号,将计算的结果输出到第三个Textbox里面。

 

而整个计算并不是在本地进行的,是在Web服务端进行计算的然后将结果通过XML返还给了调用方的,所以,在运行该程序的时候,WebService程序还必须启动,否则会报无法连接远程服务器的异常,如下图:

 

后台代码如下:

 1 view plaincopy to clipboardprint?
 2 using System;   
 3 using System.Configuration;   
 4 using System.Data;   
 5 using System.Linq;   
 6 using System.Web;   
 7 using System.Web.Security;   
 8 using System.Web.UI;   
 9 using System.Web.UI.HtmlControls;   
10 using System.Web.UI.WebControls;   
11 using System.Web.UI.WebControls.WebParts;   
12 using System.Xml.Linq;   
13 public partial class _Default : System.Web.UI.Page    
14 {   
15     protected void Page_Load(object sender, EventArgs e)   
16     {   
17     }   
18     protected void Button1_Click(object sender, EventArgs e)   
19     {   
20         string selectFlag = selectOper.Value;   
21         localhost.Service web = new localhost.Service();   
22         if (selectFlag.Equals("+"))   
23         {   
24             Result.Text =(web.addition(double.Parse(Num1.Text),double.Parse(Num2.Text))).ToString();   
25         }   
26         else if (selectFlag.Equals("-"))   
27         {   
28             Result.Text = (web.subtract(double.Parse(Num1.Text), double.Parse(Num2.Text))).ToString();   
29         }   
30         else if (selectFlag.Equals("*"))   
31         {   
32             Result.Text = (web.multiplication(double.Parse(Num1.Text), double.Parse(Num2.Text))).ToString();   
33         }   
34         else if (selectFlag.Equals("/"))   
35         {   
36             Result.Text = (web.division(double.Parse(Num1.Text), double.Parse(Num2.Text))).ToString();   
37         }   
38     }   
39 }  
40 using System;
41 using System.Configuration;
42 using System.Data;
43 using System.Linq;
44 using System.Web;
45 using System.Web.Security;
46 using System.Web.UI;
47 using System.Web.UI.HtmlControls;
48 using System.Web.UI.WebControls;
49 using System.Web.UI.WebControls.WebParts;
50 using System.Xml.Linq;
51 public partial class _Default : System.Web.UI.Page 
52 {
53     protected void Page_Load(object sender, EventArgs e)
54     {
55     }
56     protected void Button1_Click(object sender, EventArgs e)
57     {
58         string selectFlag = selectOper.Value;
59         localhost.Service web = new localhost.Service();
60         if (selectFlag.Equals("+"))
61         {
62             Result.Text =(web.addition(double.Parse(Num1.Text),double.Parse(Num2.Text))).ToString();
63         }
64         else if (selectFlag.Equals("-"))
65         {
66             Result.Text = (web.subtract(double.Parse(Num1.Text), double.Parse(Num2.Text))).ToString();
67         }
68         else if (selectFlag.Equals("*"))
69         {
70             Result.Text = (web.multiplication(double.Parse(Num1.Text), double.Parse(Num2.Text))).ToString();
71         }
72         else if (selectFlag.Equals("/"))
73         {
74             Result.Text = (web.division(double.Parse(Num1.Text), double.Parse(Num2.Text))).ToString();
75         }
76     }
77 }
78  
后台代码

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

原文地址:https://www.cnblogs.com/yinyuejie/p/3216822.html