一、Web Service是一段内含某种特定功能或商业逻辑(例如:股票报价、身份证号码、信用卡检验等),并允许远程对象通过HTTP来启动和调用,如:在A机器上调用B机器上HelloWorld对象的SayHelloWorld方法。
三、另外一个例子
调用这个web service
<%@ WebService Language="C#" Class="HelloWorld"%>
using System.Web.Services;
Public class HelloWorld:Inherits WebService{
[WebMethod]public string SayHelloWorld()
{
Return("HelloWorld");
}
}
using System.Web.Services;
Public class HelloWorld:Inherits WebService{
[WebMethod]public string SayHelloWorld()
{
Return("HelloWorld");
}
}
之后直接放在虚拟目录下就可运行,用.asmx?wsdl 可以看到Web Service 的XML文件。
二、上面例子的简单调用
<Script Language="C#" runat="Server">
public void Page_Load(object sender,EventArgs e)
{
HelloWorld myHelloWorld=new HelloWorld();
string sReturn=myHelloWorld.SayHelloWorld();
Lable1.Text=sReturn;
}
</Script>
public void Page_Load(object sender,EventArgs e)
{
HelloWorld myHelloWorld=new HelloWorld();
string sReturn=myHelloWorld.SayHelloWorld();
Lable1.Text=sReturn;
}
</Script>
三、另外一个例子
<%@ WebService Language="C#" Class="MathService"%>
using System;
using System.Web.Services;
public class MathService
{
[WebMethod]
public int Add(int a,int b)
{
return a + b;
}
[WebMethod]
public int Substract(int a, int b)
{
return a - b;
}
[WebMethod]
public int Multiply(int a , int b)
{
return a * b;
}
[WebMethod]
public int Divide(int a , int b)
{
if (b==0) return -1;
return a / b;
}
}
using System;
using System.Web.Services;
public class MathService
{
[WebMethod]
public int Add(int a,int b)
{
return a + b;
}
[WebMethod]
public int Substract(int a, int b)
{
return a - b;
}
[WebMethod]
public int Multiply(int a , int b)
{
return a * b;
}
[WebMethod]
public int Divide(int a , int b)
{
if (b==0) return -1;
return a / b;
}
}
调用这个web service
<@ Import Namespace="MathServiceSpace">
<script language="C#" runat="server">
int Operand1 = 0;
int Operand2 = 1;
public void Submit_Click(object sender,EventArgs e)
{
try
{
operand1= Int32.Parse(Operand1.Text);
operand2= Int32.Parse(Operand2.Text);
}
catch (Exception)
{
Result.Text="数字初始化错误";
}
MathService Service=new MathService();
switch (((control)sender).ID)
{
case "Add":
Result.Text="计算结果:"+Service.Add(operand1,operand2).ToString;
break;
case "Subtract":
Result.Text="计算结果:"+Service.Subtract(operand1,operand2).ToString;
break;
case "Multiply":
Result.Text="计算结果:"+Service.Multiply(operand1,operand2).ToString;
break;
case "Divide":
Result.Text="计算结果:"+Service.Divide(operand1,operand2).ToString;
break;
}
}
</script>
<html>
<body>
<form runat="server">
Operand1:<asp:TextBox id="Operand1" runat="server"/><br>
Operand2;<asp:TextBox id="Operand2" runat="server"/><br>
<input type="button" id="Add" value="Add" OnServerClick="Submit_Click" runat="server"><br>
<input type="button" id="Subtract" value="Subtract" OnServerClick="Submit_Click" runat="server"><br>
<input type="button" id="Multiply" value="Multiply" OnServerClick="Submit_Click" runat="server"><br>
<input type="button" id="Divide" value="Divide" OnServerClick="Submit_Click" runat="server"><br>
<asp:Label id="Result" runat="server"/>
</form>
</body>
</html>
<script language="C#" runat="server">
int Operand1 = 0;
int Operand2 = 1;
public void Submit_Click(object sender,EventArgs e)
{
try
{
operand1= Int32.Parse(Operand1.Text);
operand2= Int32.Parse(Operand2.Text);
}
catch (Exception)
{
Result.Text="数字初始化错误";
}
MathService Service=new MathService();
switch (((control)sender).ID)
{
case "Add":
Result.Text="计算结果:"+Service.Add(operand1,operand2).ToString;
break;
case "Subtract":
Result.Text="计算结果:"+Service.Subtract(operand1,operand2).ToString;
break;
case "Multiply":
Result.Text="计算结果:"+Service.Multiply(operand1,operand2).ToString;
break;
case "Divide":
Result.Text="计算结果:"+Service.Divide(operand1,operand2).ToString;
break;
}
}
</script>
<html>
<body>
<form runat="server">
Operand1:<asp:TextBox id="Operand1" runat="server"/><br>
Operand2;<asp:TextBox id="Operand2" runat="server"/><br>
<input type="button" id="Add" value="Add" OnServerClick="Submit_Click" runat="server"><br>
<input type="button" id="Subtract" value="Subtract" OnServerClick="Submit_Click" runat="server"><br>
<input type="button" id="Multiply" value="Multiply" OnServerClick="Submit_Click" runat="server"><br>
<input type="button" id="Divide" value="Divide" OnServerClick="Submit_Click" runat="server"><br>
<asp:Label id="Result" runat="server"/>
</form>
</body>
</html>