转载的web server实例
2015-01-22 17:24 mi-战斧 阅读(285) 评论(0) 收藏 举报- asp.net—web server模拟网上购物
-
在学vb的时候学到了api函数,今天学习asp.net中的web server,web server和api函数一样都是为用户提供了一个接口,客户端可以在远程直接调用,不需要知道它具体的算法,难易程度,可以直接使用方法。
一.基础
概念:
1.web服务是应用程序
2.它向外界暴露了一个能够通过web进行调用的api
3.能够用编程的方法,通过web来调用这个应用程序
4.把调用这个web服务应用程序叫做客户。
运行流程
1.目录:web service提供了一个用以定位其他单位提供的web service的中心位置。其中,uddi就是web service目录。Uudi通俗一点说就是建立web service时使用注册到uudi。如果使用服务,就来看uudi。
2.发现:使用wsdl对特定的web service进行描述,一般都是xml文档。其中,wsdl用于描述WebService及其函数、参数和返回值。可以用来向用户介绍Web service的功能,每个函数调用时的参数。
3.联网形式:使用开放式联网形式进行通讯,主要使用sopa通讯协议。
特点:
1.通过web进行访问。
2.使用接口进行调用
3.在服务注册表中注册
4.使用标准web协议通信
5.松散耦合
二.模拟银行转账的实例
需求
web server提供了可以使买家付款给卖家的方法方法和获取商品列表的方法;客户端调用这个两个方法,客户端选中购买的商品后,单击‘购买’按钮就可以买家付款给卖家,并显示买家消费金额。
代码实现
1.web service代码
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051publicclassserviceShopping : System.Web.Services.WebService{[WebMethod]//获取商品publicDataSet getGoods(){SqlConnection con =newSqlConnection("server=.;database=shop;uid=sa;pwd=123456;");con.Open();SqlDataAdapter adr =newSqlDataAdapter();adr.SelectCommand =newSqlCommand("select * from goods", con);DataSet ds =newDataSet();adr.Fill(ds,"goods");con.Close();returnds;}[WebMethod]//购物publicstring shopping(intsum){try{//买家买东西this.buy(sum);//卖家卖东西this.sell(sum);return"交易成功,消费:"+sum;}catch{return"交易失败";}}//买家买东西privatevoidbuy(intsum){SqlConnection con =newSqlConnection("server=.;database=shop;uid=sa;pwd=123456;");con.Open();SqlCommand cmd =newSqlCommand("update buy set money=money-"+ sum.ToString() +" where buyer='A'", con);cmd.ExecuteNonQuery();con.Close();}//卖家卖东西privatevoidsell(intsum){SqlConnection con =newSqlConnection("server=.;database=shop;uid=sa;pwd=123456;");con.Open();SqlCommand cmd =newSqlCommand("update sell set money=money+"+ sum.ToString() +" where seller='B'", con);cmd.ExecuteNonQuery();con.Close();}}2.客户端中引用web service的步骤
![\]()
备注:地址是运行web service后地址栏中地址。
3.客户端代码
客户端html代码客户端后台代码1234567891011<meta http-equiv="Content-Type"content="text/html; charset=utf-8"><title></title><form id="form1"runat="server"><div></asp:checkboxlist></asp:button></div></form>1234567891011121314151617181920212223242526272829303132publicpartialclassUseServerShopping : System.Web.UI.Page{//绑定商品列表protectedvoidPage_Load(object sender, EventArgs e){if(!IsPostBack){myserviceShopping.serviceShoppingSoapClient getGoodslist =newmyserviceShopping.serviceShoppingSoapClient();this.CheckBoxList1.DataSource = getGoodslist.getGoods();//绑定商品列表this.CheckBoxList1.DataTextField ="goodsname";this.CheckBoxList1.DataValueField ="cost";this.CheckBoxList1.DataBind();}}//购买商品protectedvoidButton1_Click(object sender, EventArgs e){//商品价格inttotalCost=0;//计算商品总共价格for(inti =0; i < CheckBoxList1.Items.Count; i++)//循环checjboxlist1的个数{if(CheckBoxList1.Items[i].Selected ==true)//checjboxlist1被选中{totalCost =totalCost+ Convert.ToInt32(CheckBoxList1.Items[i].Value);//计算商品总价格}}myserviceShopping.serviceShoppingSoapClient buyGoods =newmyserviceShopping.serviceShoppingSoapClient();buyGoods.shopping(totalCost);//调用服务中使买家付款给卖家Response.Write(buyGoods.shopping(totalCost));}}源码地址
里面有具体的源码:http://download.csdn.net/detail/suneqing/7313033
三.总结
Web Service技术, 能使得运行在不同机器上的不同应用无须借助附加的、专门的第三方软件或硬件, 就可相互交换数据或集成。依据Web Service规范实施的应用之间, 无论它们所使用的语言、 平台或内部协议是什么, 都可以相互交换数据。
我要投稿

备注:地址是运行web service后地址栏中地址。
浙公网安备 33010602011771号