.net 使用webservice 技术的测试案例

1:我们打开vs开发工具新建项目添加asp.net web 服务应用程序

2 添加web应用程序服务好后可以先测试service.asmx文档

3 我在后service.asmx后台添加四个功能函数(求和、差、乘、除)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Xml.Linq;
using System.Web.Services.Protocols;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。
// [System.Web.Script.Services.ScriptService]

public class Service : System.Web.Services.WebService
{
    public Service () {

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

    [WebMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }
    [WebMethod(Description = "求和方法")]
    public double Addition(double i, double j)
    {
        return i + j;   
    }
    [WebMethod(Description="求差的方法")]
    public double Subtract(double i,double j)
    {
     return i-j;
    }
    [WebMethod(Description = "求积的方法")]
    public double Multiplication(double i, double j)
    {
        return i * j;
    }
    [WebMethod(Description = "求商的方法")]
    public double Division(double i, double j)
    {
        if (j != 0)
            return i / j;
        else
            return 0;
    }
}

  4 我们再添加一个实现以上四个功能函数的页面

5 我们添加web服务引用

6 我看页面的后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;  
using System.Web.UI.HtmlControls;  
using System.Web.UI.WebControls;  
using System.Web.UI.WebControls.WebParts;  
using System.Xml.Linq;
using localhost;
  
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DropDownList1.Items.Add("+");
            DropDownList1.Items.Add("-");
            DropDownList1.Items.Add("x");
            DropDownList1.Items.Add("/");
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
       
        if (TextBox1.Text == "")
        {
            Response.Write("<script>alert('第一个文本框不能为空')</script>");
            TextBox1.Focus();
        
        }
        else if (TextBox2.Text == "")
        {
            Response.Write("<script>alert('第二个文本框不能为空')</script>");
            TextBox2.Focus();
        }
        else
        {
            double a = double.Parse(TextBox1.Text);
            double b = double.Parse(TextBox2.Text);
            Service web = new Service();
            switch (DropDownList1.SelectedValue)
            {
                case "+":
                    TextBox3.Text = (web.Addition(a, b)).ToString();
                    break;
                case "-":
                    TextBox3.Text = (web.Subtract(a, b)).ToString();
                    break;
                case "x":
                    TextBox3.Text = (web.Multiplication(a, b)).ToString();
                    break;
                case "/":
                    TextBox3.Text = (web.Division(a, b)).ToString();
                    break;
            }
        }
    }
}

7 测试效果:

 

 

 

 

 

 

posted @ 2013-04-21 16:14  曹县三胖暴打大猩猩  阅读(385)  评论(0编辑  收藏  举报