.net web service 调用的几种方式

首先当然是开发一个webservice,我们来个简单的计算器的实现,代码如下

[c-sharp]  view plain  copy
  1. using System;  
  2. using System.Collections;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Linq;  
  6. using System.Web;  
  7. using System.Web.Services;  
  8. using System.Web.Services.Protocols;  
  9. using System.Xml.Linq;  
  10. namespace WebServiceApp  
  11. {  
  12.     /// <summary>  
  13.     /// CalcService 的摘要说明  
  14.     /// </summary>  
  15.     [WebService(Namespace = "http://tempuri.org/")]  
  16.     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  
  17.     [ToolboxItem(false)]  
  18.     // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。  
  19.     //[System.Web.Script.Services.ScriptService]  
  20.     public class CalcService : System.Web.Services.WebService  
  21.     {  
  22.         [WebMethod]  
  23.         public double Add(double x, double y)  
  24.         {  
  25.             return x + y;  
  26.         }  
  27.         [WebMethod]  
  28.         public double Minus(double x, double y)  
  29.         {  
  30.             return x - y;  
  31.         }  
  32.         [WebMethod]  
  33.         public double Multiply(double x, double y)  
  34.         {  
  35.             return x * y;  
  36.         }  
  37.         [WebMethod]  
  38.         public double Devide(double x, double y)  
  39.         {  
  40.             return x / y;  
  41.         }  
  42.         [WebMethod]  
  43.         public double Power(double x, int n)  
  44.         {  
  45.             double result = 1;  
  46.             for (int i = 1; i <= n; i++)  
  47.             {  
  48.                 result *= x;  
  49.             }  
  50.             return result;  
  51.         }  
  52.         [WebMethod]  
  53.         public double Sin(double x)  
  54.         {  
  55.             return Math.Sin(x);  
  56.         }  
  57.         [WebMethod]  
  58.         public double Cos(double x)  
  59.         {  
  60.             return Math.Cos(x);  
  61.         }  
  62.     }  
  63. }  
 

 

好,这么一个简单的webservice是开发完了,用IIS发布

调用的URL为: http://localhost/WebServiceApp/CalcService.asmx

 

下面分别介绍三种方式来调用该webservice,分别是:ASP.NET、Javascript、WinForm

 

先说第一种方式ASP.NET

 

新建一个ASP.NET网络应用程序,我取名为WebClient

 

第一步当然是添加web引用,把刚才的webservice调用网站复制到输入框中,然后取名为默认的localhost

 

在Default.aspx页面代码中添加如下代码

 

[c-sharp]  view plain  copy
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebClient._Default" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml" >  
  4. <head runat="server">  
  5.     <title>WebService Test</title>  
  6. </head>  
  7. <body>  
  8.     <form id="form1" runat="server">  
  9.     <div>  
  10.         <asp:TextBox ID="num1" runat="server"></asp:TextBox>  
  11.         <asp:DropDownList ID="oper" runat="server" AutoPostBack="True"   
  12.             onselectedindexchanged="oper_SelectedIndexChanged">  
  13.             <asp:ListItem Text="+" Value="+" Selected="True"></asp:ListItem>  
  14.             <asp:ListItem Text="-" Value="-"></asp:ListItem>  
  15.             <asp:ListItem Text="*" Value="*"></asp:ListItem>  
  16.             <asp:ListItem Text="/" Value="/"></asp:ListItem>  
  17.             <asp:ListItem Text="power" Value="power"></asp:ListItem>  
  18.             <asp:ListItem Text="sin" Value="sin"></asp:ListItem>  
  19.             <asp:ListItem Text="cos" Value="cos"></asp:ListItem>  
  20.         </asp:DropDownList>  
  21.         <asp:TextBox ID="num2" runat="server"></asp:TextBox>  
  22.         <asp:Button ID="btnResult" runat="server" Text="=" οnclick="btnResult_Click" />  
  23.         <asp:TextBox ID="result" runat="server"></asp:TextBox>  
  24.     </div>  
  25.     </form>  
  26. </body>  
  27. </html>  
 

 

也就是提供输入数字和显示结果的界面,很简单

 

后台代码为

 

[c-sharp]  view plain  copy
  1. using System;  
  2. using System.Collections;  
  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. namespace WebClient  
  14. {  
  15.     public partial class _Default : System.Web.UI.Page  
  16.     {  
  17.         protected void Page_Load(object sender, EventArgs e)  
  18.         {  
  19.         }  
  20.         protected void btnResult_Click(object sender, EventArgs e)  
  21.         {  
  22.             double num1 = Double.Parse(this.num1.Text.Trim());  
  23.             double num2 = Double.Parse(this.num2.Text.Trim());  
  24.             string oper = this.oper.SelectedValue;  
  25.             double result = 0;  
  26.   
  27.             localhost.CalcService service = new WebClient.localhost.CalcService();  
  28.             switch (oper)  
  29.             {   
  30.                 case "+":  
  31.                     result = service.Add(num1, num2);  
  32.                     break;  
  33.                 case "-":  
  34.                     result = service.Minus(num1, num2);  
  35.                     break;  
  36.                 case "*":  
  37.                     result = service.Multiply(num1, num2);  
  38.                     break;  
  39.                 case "/":  
  40.                     result = service.Devide(num1, num2);  
  41.                     break;  
  42.                 case "power":  
  43.                     result = service.Power(num1,(int)num2);  
  44.                     break;  
  45.                 case "sin":  
  46.                     result = service.Sin(num1);  
  47.                     break;  
  48.                 case "cos":  
  49.                     result = service.Cos(num1);  
  50.                     break;  
  51.                 default:  
  52.                     result = service.Add(num1, num2);  
  53.                     break;  
  54.             }  
  55.             this.result.Text = result + "";  
  56.         }  
  57.         protected void oper_SelectedIndexChanged(object sender, EventArgs e)  
  58.         {  
  59.             string oper = this.oper.SelectedValue;  
  60.             if (oper.Equals("sin") || oper.Equals("cos"))  
  61.             {  
  62.                 this.num2.Visible = false;  
  63.             }  
  64.             else  
  65.             {  
  66.                 this.num2.Visible = true;  
  67.             }  
  68.         }  
  69.     }  
  70. }  
 

 

着重要看的是下面一行代码

 

       localhost.CalcService service = new WebClient.localhost.CalcService();

 

该代码将创建了一个我们开发的webservice的计算器实例

然后通过该实例去调用webservice暴露出来的服务接口

 

结果

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值