.Net WebService Demo
来源:互联网 发布:淘宝宝贝在线人数查询 编辑:程序博客网 时间:2024/06/01 08:55
这只是一个简单的实例,适合想使用webservice,但是又不知道如何使用的朋友.
当然,前提是你已经对webservice有所了解了.至少知道它是什么,能做什么,如何能添加相关的引用.
这个Demo由两个asp.net网站组成,网站a提供了webservice的接口,网站b调用webservice.
步骤一
新建一个asp.net网站,然后添加一个"web 服务",也就是后缀是".asmx"的文件.
这里给起个名字叫MyWebService.asmx;里面会自动添加一个"hello world"的实例方法.
在这里再给它写四个加减乘除计算的方法,代码如下:
namespace Sigil.Zenith.sigil.zh_cn.task
{
/// <summary>
/// MyWebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
public class MyWebService : System.Web.Services.WebService
{
[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;
}
}
}
}
这其中[WebMethod]是必须的,如果这个方法是想让用户调用到的话,如果有不想让用户调用的方法,就不加[WebMethod].
步骤二
再新建一个asp.net网站,添加"web 引用",
在需要实现引用的页面编写代码.
前台代码如下:
<%@ 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 runat="server">
<title>MyWebService</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="Num1" runat="server"></asp:TextBox>
<select id="selectOper" runat = "server">
<option>+</option>
<option>-</option>
<option>*</option>
<option>/</option>
</select>
<asp:TextBox ID="Num2" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="=" onclick="Button1_Click" />
<asp:TextBox ID="Result" runat="server"></asp:TextBox>
</div>
</form>
</body>
</html>
前台代码没有可说的,下面是后台代码:
protected void Button1_Click(object sender, EventArgs e)
{
string selectFlag = selectOper.Value;
localhost.MyWebService web = new localhost.MyWebService();
if (selectFlag.Equals("+"))
{
Result.Text = (web.addition(double.Parse(Num1.Text), double.Parse(Num2.Text))).ToString();
}
else if (selectFlag.Equals("-"))
{
Result.Text = (web.subtract(double.Parse(Num1.Text), double.Parse(Num2.Text))).ToString();
}
else if (selectFlag.Equals("*"))
{
Result.Text = (web.multiplication(double.Parse(Num1.Text), double.Parse(Num2.Text))).ToString();
}
else if (selectFlag.Equals("/"))
{
Result.Text = (web.division(double.Parse(Num1.Text), double.Parse(Num2.Text))).ToString();
}
}
"MyWebService"是webservice的文件名或者说类名.
运行一下页面就可以体验一下最简单的webservice功能了.
最后要感谢博客园魄力的博客http://www.cnblogs.com/denylau/archive/2010/07/23/1783530.html.
- .Net WebService Demo
- php调用 .net webservice demo
- Flash调用.net WebService的Demo
- webService demo
- webservice demo
- webservice demo
- webservice-demo
- .NET(WEBSERVICE) EXT (Tree)JSON数据小demo
- webservice的demo
- 动态加载WebService DEMO
- ios webservice 登陆 demo
- android 调用webService Demo
- webservice(resteasy demo)
- webservice(xfire demo)
- webservice(cxf demo)
- webservice demo示例
- WebService 入门 Demo
- webService的demo
- Source insight上修改samba共享ubuntu上内核源程序文件出现"Confirm by typing ‘yes' below"、"has been changed outside of the editor. Do you want to
- mysql 更改编码
- JQUERY 1.3.2 的scrollTop的BUG
- HashMap源代码
- 20110603总结 端午
- .Net WebService Demo
- YUI 3:Node
- WIN32平台下学生成绩,课程情况显示程序
- 搜索二叉树转换成链表
- 进程共享同一个DLL数据
- 【转】Matlab Out of memory问题总结
- android Tabhost部件(详细)
- linux qq安装指南
- Delphi原创算法:动态创建30个按钮,每行6个共5行,顺序排列,显示在ScrollBox上