2.3、用ASP.NET调用Web Service
首先,打开VS2005,打开“文件-新建-网站”,选择“ASP.NET网站”。
选好存储位置,语言后点击确定,进入默认页面。然后先添加Web引用,把WebService引到当前的工程里面。方法是:在资源管理器中点击右键,选择添加Web 引用,调出对话框:
在URL中填入,前面写好的WebService运行后浏览器上面显示的地址,点击“前往”按钮,如上图,就会显示出所引用的WebService中可以调用的方法,然后点击“添加引用”,就将webservice引用到了当前的工程里面 ,如下图,解决方案中会出现引进来的WebService文件
我们在这就练习调用webservice的四个方法,做一个简单的调用的例子,先在网站的前台添加几个控件,代码如下:
?
然后在后台写调用的代码,调用之前和使用其它的对象一样,要先实例化,实例化的方法是localhost.Service a = new localhost.Service();然后就可以通过a来访问WebService里面提供的方法了。在这个例子里面,动态的创建了一个button控件来触发WebService的调用,后台代码如下:
1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Web;
5 using System.Web.Security;
6 using System.Web.UI;
7 using System.Web.UI.WebControls;
8 using System.Web.UI.WebControls.WebParts;
9 using System.Web.UI.HtmlControls;
10 public partial class _Default : System.Web.UI.Page
11 {
12 ????protected void Page_Load(object sender, EventArgs e)
13 ???? {
14 ????????//在页面加载的时候动态创建一个按钮,在它的事件里调用Webservice
15 ???????? Button btn = new Button();
16 ???????? btn.Width = 20;
17 ???????? btn.Text = " = ";
18 ???????? btn.Click +=new EventHandler(btn_Click);
19 ???????? E.Controls.Add(btn);
20 ???? }
21 ????/// <summary>
22 ????/// 定义动态创建Button的Click事件,在这个事件中调用Webservice
23 ????/// </summary>
24 ????/// <param name="sender"></param>
25 ????/// <param name="e"></param>
26 ????void btn_Click(object sender, EventArgs e)
27 ???? {
28 ????????if (Num1.Text != "" && Num2.Text != "")
29 ???????? {
30 ????????????//实例化引用的webservice对象
31 ???????????? localhost.Service WebserviceInstance = new localhost.Service();
32 ????????????int Oper = selectOper.SelectedIndex;
33 ????????????switch( Oper)
34 ???????????? {
35 ????????????????//通过实例化的webservice对象来调用Webservice暴露的方法
36 ????????????????case 0:
37 ???????????????????? Result.Text = WebserviceInstance.addition(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString();
38 ????????????????????break;
39 ????????????????case 1:
40 ???????????????????? Result.Text = WebserviceInstance.subtract(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString();
41 ????????????????????break;
42 ????????????????case 2:
43 ???????????????????? Result.Text = WebserviceInstance.multiplication(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString();
44 ????????????????????break;
45 ????????????????case 3:
46 ???????????????????? Result.Text = WebserviceInstance.division(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString();
47 ????????????????????break;
48 ???????????? }
49 ???????? }
50 ???? }
51 }
52
运行后可以看到效果,如下图所示,在前面两个Textbox里面输入两个操作数,在中间的下拉列表中选择操作符,然后点击“=”号,将计算的结果输出到第三个Textbox里面。2 using System.Data;
3 using System.Configuration;
4 using System.Web;
5 using System.Web.Security;
6 using System.Web.UI;
7 using System.Web.UI.WebControls;
8 using System.Web.UI.WebControls.WebParts;
9 using System.Web.UI.HtmlControls;
10 public partial class _Default : System.Web.UI.Page
11 {
12 ????protected void Page_Load(object sender, EventArgs e)
13 ???? {
14 ????????//在页面加载的时候动态创建一个按钮,在它的事件里调用Webservice
15 ???????? Button btn = new Button();
16 ???????? btn.Width = 20;
17 ???????? btn.Text = " = ";
18 ???????? btn.Click +=new EventHandler(btn_Click);
19 ???????? E.Controls.Add(btn);
20 ???? }
21 ????/// <summary>
22 ????/// 定义动态创建Button的Click事件,在这个事件中调用Webservice
23 ????/// </summary>
24 ????/// <param name="sender"></param>
25 ????/// <param name="e"></param>
26 ????void btn_Click(object sender, EventArgs e)
27 ???? {
28 ????????if (Num1.Text != "" && Num2.Text != "")
29 ???????? {
30 ????????????//实例化引用的webservice对象
31 ???????????? localhost.Service WebserviceInstance = new localhost.Service();
32 ????????????int Oper = selectOper.SelectedIndex;
33 ????????????switch( Oper)
34 ???????????? {
35 ????????????????//通过实例化的webservice对象来调用Webservice暴露的方法
36 ????????????????case 0:
37 ???????????????????? Result.Text = WebserviceInstance.addition(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString();
38 ????????????????????break;
39 ????????????????case 1:
40 ???????????????????? Result.Text = WebserviceInstance.subtract(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString();
41 ????????????????????break;
42 ????????????????case 2:
43 ???????????????????? Result.Text = WebserviceInstance.multiplication(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString();
44 ????????????????????break;
45 ????????????????case 3:
46 ???????????????????? Result.Text = WebserviceInstance.division(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString();
47 ????????????????????break;
48 ???????????? }
49 ???????? }
50 ???? }
51 }
52
而整个计算并不是在本地进行的,是在Web服务端进行计算的然后将结果通过XML返还给了调用方的,所以,在运行该程序的时候,WebService程序还必须启动,否则会报无法连接远程服务器的异常,如下图:
到此一个一个简单的WebService的开发和调用就已经完成了,在实际应用中可以根据自己的需要,写一些功能强大的,复杂的WebService,不管多么复杂,整个流程都是这样的。
?
1 <%@ Page Language="C#" AutoEventWireup="true"?? CodeFile="Default.aspx.cs" Inherits="_Default" %>
2
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>Webservice调用实例</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 ????????????<span id = E runat = "server"></span>
20 ????????????<asp:TextBox ID="Result" runat="server"></asp:TextBox>
21 ????????</div>
22 </form>
23 </body>
24 </html>
25
2
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>Webservice调用实例</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 ????????????<span id = E runat = "server"></span>
20 ????????????<asp:TextBox ID="Result" runat="server"></asp:TextBox>
21 ????????</div>
22 </form>
23 </body>
24 </html>
25
?