zoukankan      html  css  js  c++  java
  • 用WebService实现两个整数运算

         最近,项目开发中需要用到Web Service.自己在网上搜集资料.自己做了一个小例子,用来加深自己对Web Service理解.

         概念:Web Service主要是为了使原来各孤立的站点之间的信息能够相互通信、共享而提出的一种接口。 Web Service所使用的是Internet上统一、开放的标准,如HTTP、XML、SOAP(简单对象访问协议)、WSDL等,所以Web Service可以在任何支持这些标准的环境(Windows,Linux)中使用。注:SOAP协议(Simple Object Access Protocal,简单对象访问协议),它是一个用于分散和分布式环境下网络信息交换的基于XML的通讯协议。在此协议下,软件组件或应用程序能够通过标准的HTTP协议进行通讯。它的设计目标就是简单性和扩展性,这有助于大量异构程序和平台之间的互操作性,从而使存在的应用程序能够被广泛的用户访问.

        这是Web Service服务端MyWeb Service.asmx文件代码:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Web;
     4 using System.Web.Services;
     5 
     6 /// <summary>
     7 ///MyWebService 的摘要说明
     8 /// </summary>
     9 [WebService(Namespace = "http://tempuri.org/")]
    10 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    11 public class MyWebService : System.Web.Services.WebService {
    12 
    13     public MyWebService () {
    14 
    15         //如果使用设计的组件,请取消注释以下行 
    16         //InitializeComponent(); 
    17     }
    18     [WebMethod(Description = "求和的方法")]
    19     public double addition(double i,double j) 
    20     {
    21         return i + j;
    22     }
    23     [WebMethod(Description = "求差的方法")]
    24     public double subtract(double i,double j) 
    25     {
    26         return i - j;
    27     }
    28     [WebMethod(Description = "求积的方法")]
    29     public double multiplication(double i, double j) 
    30     {
    31         return i * j;
    32     }
    33     [WebMethod(Description = "求商的方法")]
    34     public double division(double i, double j) 
    35     {
    36         if (j != 0) 
    37         { 
    38             return i / j; 
    39         } 
    40         else
    41         {
    42             return 0;
    43         }
    44     }
    45 }

    这是客户端前台index.aspx文件代码:

     1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="index.aspx.cs" Inherits="index" %>
     2 
     3 <!DOCTYPE html>
     4 
     5 <html xmlns="http://www.w3.org/1999/xhtml">
     6 <head runat="server">
     7     <title></title>
     8 </head>
     9 <body>
    10     <form id="form1" runat="server">
    11     <div>
    12         <asp:TextBox ID="Num1" runat="server"></asp:TextBox>
    13         <select id="selectOper" runat="server">
    14             <option>+</option>
    15             <option>-</option>
    16             <option>*</option>
    17             <option>/</option>
    18         </select>
    19         <asp:TextBox ID="Num2" runat="server"></asp:TextBox>
    20         <asp:Button ID="Button1" runat="server" Text="=" onclick="Button1_Click"/>
    21         <asp:TextBox ID="Result" runat="server"></asp:TextBox>
    22         </div>
    23     </form>
    24 </body>
    25 </html>

    这是客户端index.aspx.cs文件代码:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Web;
     4 using System.Web.UI;
     5 using System.Web.UI.WebControls;
     6 
     7 public partial class index : System.Web.UI.Page
     8 {
     9     protected void Page_Load(object sender, EventArgs e)
    10     {
    11 
    12     }
    13     protected void Button1_Click(object sender, EventArgs e)
    14     {
    15         string selectFlag = selectOper.Value;
    16         MyWebService.MyWebService web = new MyWebService.MyWebService();
    17         if(selectFlag.Equals("+"))
    18         {
    19         Result.Text=(web.addition(double.Parse(Num1.Text),double.Parse(Num2.Text))).ToString();
    20         }
    21         else if (selectFlag.Equals("-"))
    22         {
    23          Result.Text = (web.subtract(double.Parse(Num1.Text), double.Parse(Num2.Text))).ToString();
    24         }
    25         else if (selectFlag.Equals("*"))
    26         {
    27             Result.Text = (web.multiplication(double.Parse(Num1.Text), double.Parse(Num2.Text))).ToString();
    28         }
    29         else if (selectFlag.Equals("/"))
    30         {
    31             Result.Text = (web.division(double.Parse(Num1.Text), double.Parse(Num2.Text))).ToString();
    32         }
    33     }
    34 }

    就这样就实现了一个ASP.NET网站访问用Web Service服务实现了两个整数运算的功能.

  • 相关阅读:
    什么是操作系统
    去除按钮点击的边框
    unsupported time zone specified undefined
    w3c JS测试
    视频播放器
    document.write
    HTML4到HTML5
    MPU6050
    NRF24L01模块配置
    4-Four-Seeing hands
  • 原文地址:https://www.cnblogs.com/liubeimeng/p/3806140.html
Copyright © 2011-2022 走看看