典型的webservice例程序(1)-计算器

时间:2023-01-09 22:47:24

a。先建一个web服务

1.新建-网站

2-添加-项目-web服务

典型的webservice例程序(1)-计算器

典型的webservice例程序(1)-计算器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

/// <summary>
///WebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {

    public WebService () {

        //如果使用设计的组件,请取消注释以下行
        //InitializeComponent();

 

    }

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }

    [WebMethod]
    public double Sum(double a, double b)
    {
        return a + b;
    }

    [WebMethod]
    public double Sub(double a, double b)
    {
        return a - b;
    }

    [WebMethod]
    public double Mult(double a, double b)
    {
        return a * b;
    }

    [WebMethod]
    public double Div(double a, double b)
    {
        return a / b;
    }
   
}

执行

b.建立一个引用

1.添加web引用

典型的webservice例程序(1)-计算器

2.添加一个index.aspx 文件

前台:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <div style="text-align: center">
                    <table style="border-right: blue 1px solid; border-top: blue 1px solid; border-left: blue 1px solid;
                        width: 53px; border-bottom: blue 1px solid">
                        <tr>
                            <td colspan="4" style="text-align: center">
                                <asp:TextBox ID="TextBox1" runat="server" Width="125px"></asp:TextBox></td>
                        </tr>
                        <tr>
                            <td style="text-align: center">
                                <asp:Button ID="Button1" runat="server" Font-Size="9pt" OnClick="Button1_Click" Text="1"
                                    Width="28px" /></td>
                            <td style="text-align: center">
                                <asp:Button ID="Button2" runat="server" Font-Size="9pt" OnClick="Button2_Click" Text="2"
                                    Width="28px" /></td>
                            <td style="text-align: center">
                                <asp:Button ID="Button3" runat="server" Font-Size="9pt" OnClick="Button3_Click" Text="3"
                                    Width="28px" /></td>
                            <td style="text-align: center">
                                <asp:Button ID="Button13" runat="server" Font-Size="9pt" OnClick="Button13_Click"
                                    Text="AC" Width="28px" /></td>
                        </tr>
                        <tr>
                            <td style="text-align: center">
                                <asp:Button ID="Button4" runat="server" Font-Size="9pt" OnClick="Button4_Click" Text="4"
                                    Width="28px" /></td>
                            <td style="text-align: center">
                                <asp:Button ID="Button5" runat="server" Font-Size="9pt" OnClick="Button5_Click" Text="5"
                                    Width="28px" /></td>
                            <td style="text-align: center">
                                <asp:Button ID="Button6" runat="server" Font-Size="9pt" OnClick="Button6_Click" Text="6"
                                    Width="28px" /></td>
                            <td style="text-align: center">
                                <asp:Button ID="Button14" runat="server" Font-Size="9pt" OnClick="Button14_Click"
                                    Text="*" Width="28px" /></td>
                        </tr>
                        <tr>
                            <td style="text-align: center">
                                <asp:Button ID="Button7" runat="server" Font-Size="9pt" OnClick="Button7_Click" Text="7"
                                    Width="28px" /></td>
                            <td style="text-align: center">
                                <asp:Button ID="Button8" runat="server" Font-Size="9pt" OnClick="Button8_Click" Text="8"
                                    Width="28px" /></td>
                            <td style="text-align: center">
                                <asp:Button ID="Button9" runat="server" Font-Size="9pt" OnClick="Button9_Click" Text="9"
                                    Width="28px" /></td>
                            <td style="text-align: center">
                                <asp:Button ID="Button15" runat="server" Font-Size="9pt" OnClick="Button15_Click"
                                    Text="/" Width="28px" /></td>
                        </tr>
                        <tr>
                            <td style="text-align: center">
                                <asp:Button ID="Button10" runat="server" Font-Size="9pt" OnClick="Button10_Click"
                                    Text="0" Width="28px" /></td>
                            <td style="text-align: center">
                                <asp:Button ID="Button11" runat="server" Font-Size="9pt" OnClick="Button11_Click"
                                    Text="+" Width="28px" /></td>
                            <td style="text-align: center">
                                <asp:Button ID="Button12" runat="server" Font-Size="9pt" OnClick="Button12_Click"
                                    Text="-" Width="28px" /></td>
                            <td style="text-align: center">
                                <asp:Button ID="Button16" runat="server" Font-Size="9pt" OnClick="Button16_Click"
                                    Text="=" Width="28px" /></td>
                        </tr>
                    </table>
                    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox1"
                        Font-Size="9pt" ValidationExpression="\d+">*请输入数字</asp:RegularExpressionValidator><%--</td>--%>
    </div>
    </div>
    </form>
</body>
</html>

后台:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
   
        public static double temp1;  //存储第一个变量
        public static double temp2;   //存储第二个变量
        public static int m;      //保存输入的状态
        public bool dot = false;   //判断是否点击“=”号
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (dot != true)
            {
                this.TextBox1.Text += "1";
            }
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            if (dot != true)
            {
                this.TextBox1.Text += "2";
            }
        }
        protected void Button3_Click(object sender, EventArgs e)
        {
            if (dot != true)
            {
                this.TextBox1.Text += "3";
            }
        }
        protected void Button4_Click(object sender, EventArgs e)
        {
            if (dot != true)
            {
                this.TextBox1.Text += "4";
            }
        }
        protected void Button5_Click(object sender, EventArgs e)
        {
            if (dot != true)
            {
                this.TextBox1.Text += "5";
            }
        }
        protected void Button6_Click(object sender, EventArgs e)
        {
            if (dot != true)
            {
                this.TextBox1.Text += "6";
            }
        }
        protected void Button7_Click(object sender, EventArgs e)
        {
            if (dot != true)
            {
                this.TextBox1.Text += "7";
            }
        }
        protected void Button8_Click(object sender, EventArgs e)
        {
            if (dot != true)
            {
                this.TextBox1.Text += "8";
            }
        }
        protected void Button9_Click(object sender, EventArgs e)
        {
            if (dot != true)
            {
                this.TextBox1.Text += "9";
            }
        }
        protected void Button10_Click(object sender, EventArgs e)
        {
            if (dot != true)
            {
                this.TextBox1.Text += "0";
            }
        }
        protected void Button11_Click(object sender, EventArgs e)
        {
            if (this.TextBox1.Text != "")
            {
                temp1 = Convert.ToInt32(this.TextBox1.Text);
                this.TextBox1.Text = "";
                m = 0;
            }
        }
        protected void Button12_Click(object sender, EventArgs e)
        {
            if (this.TextBox1.Text != "")
            {
                temp1 = Convert.ToInt32(this.TextBox1.Text);
                this.TextBox1.Text = "";
                m = 1;
            }

        }
        protected void Button14_Click(object sender, EventArgs e)
        {
            if (this.TextBox1.Text != "")
            {
                temp1 = Convert.ToInt32(this.TextBox1.Text);
                this.TextBox1.Text = "";
                m = 2;
            }
        }
        protected void Button15_Click(object sender, EventArgs e)
        {
            if (this.TextBox1.Text != "")
            {
                temp1 = Convert.ToInt32(this.TextBox1.Text);
                this.TextBox1.Text = "";
                m = 3;
            }
        }
        protected void Button13_Click(object sender, EventArgs e)
        {
            dot = false;
            this.TextBox1.Text = "";
            temp1 = 0.0;
            temp2 = 0.0;
        }
        protected void Button16_Click(object sender, EventArgs e)
        {
            if (this.TextBox1.Text == "" && temp1 != null)
            {
                this.TextBox1.Text = temp1.ToString();
                dot = true;
            }
            else
            {
                double temp3 = 0.0;
                temp2 = Convert.ToDouble(this.TextBox1.Text);
                localhost.WebService result = new localhost.WebService();
                switch (m)
                {
                    case 0:
                        temp3 = result.Sum(temp1, temp2);
                        break;
                    case 1:
                        temp3 = result.Sub(temp1, temp2);
                        break;
                    case 2:
                        temp3 = result.Mult(temp1, temp2);
                        break;
                    case 3:
                        temp3 = result.Div(temp1, temp2);
                        break;
                    default:
                        Response.Write("数据有误,请重新输入!");
                        break;
                }
                if (temp3 > double.MaxValue)
                {
                    Response.Write("<script>alert('结果值超出双精度最大值!')</script>");
                    return;
                }
                this.TextBox1.Text = temp3.ToString();
                dot = true;
            }
        }
    }


典型的webservice例程序(1)-计算器

 注意:

1.在实务中,建立web服务和引用是分开的,这里放在一起是便于学习。

2.a的操作生成的文件我可以把它嵌入某服务其中,然后www.xxx.net/WebService.asmx调用。

3.b的操作我可以在设置中修改部分配置,然后把这个程序放在另一个服务器中,调用。(  localhost.WebService result = new localhost.WebService();一定改一下,改成net.xxx.www.WebService result = new net.xxx.www.WebService();)

典型的webservice例程序(1)-计算器