asp.net—web server模拟网上购物
2014-05-08     我来说两句   来源: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代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
    public class serviceShopping : System.Web.Services.WebService
{
    [WebMethod]
    //获取商品
    public DataSet getGoods()
    {
        SqlConnection con = new SqlConnection("server=.;database=shop;uid=sa;pwd=123456;");
        con.Open();
        SqlDataAdapter adr = new SqlDataAdapter();
        adr.SelectCommand = new SqlCommand("select * from goods", con);
        DataSet ds = new DataSet();
        adr.Fill(ds, "goods");
        con.Close();
        return ds;
    }
    [WebMethod]
    //购物
    public string shopping(int sum)
    {
        try
        {
            //买家买东西
            this.buy(sum);
            //卖家卖东西
            this.sell(sum);
            return "交易成功,消费:"+sum;
        }
        catch
        {
            return "交易失败";
        }
    }
    //买家买东西
    private void buy(int sum)
    {
        SqlConnection con = new SqlConnection("server=.;database=shop;uid=sa;pwd=123456;");
        con.Open();
        SqlCommand cmd = new SqlCommand("update buy set money=money-" + sum.ToString() + " where  buyer='A'", con);
        cmd.ExecuteNonQuery();
        con.Close();
    }
    //卖家卖东西
    private void sell(int sum)
    {
        SqlConnection con = new SqlConnection("server=.;database=shop;uid=sa;pwd=123456;");
        con.Open();
        SqlCommand cmd = new SqlCommand("update sell set money=money+" + sum.ToString() + " where  seller='B'", con);
        cmd.ExecuteNonQuery();
        con.Close();
    }
}

2.客户端中引用web service的步骤

备注:地址是运行web service后地址栏中地址。

3.客户端代码

客户端html代码

1
2
3
4
5
6
7
8
9
10
11
<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>

客户端后台代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public partial class UseServerShopping : System.Web.UI.Page
{
    //绑定商品列表
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            myserviceShopping.serviceShoppingSoapClient getGoodslist = new myserviceShopping.serviceShoppingSoapClient();
            this.CheckBoxList1.DataSource = getGoodslist.getGoods();   //绑定商品列表
            this.CheckBoxList1.DataTextField = "goodsname";
            this.CheckBoxList1.DataValueField = "cost";
            this.CheckBoxList1.DataBind();
        }
    }
    //购买商品
    protected void Button1_Click(object sender, EventArgs e)
    {
        //商品价格
        int totalCost=0;
        //计算商品总共价格
          for (int i = 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 = new myserviceShopping.serviceShoppingSoapClient();
        buyGoods.shopping(totalCost);   //调用服务中使买家付款给卖家
        Response.Write(buyGoods.shopping(totalCost));
    }
}

源码地址

里面有具体的源码:http://download.csdn.net/detail/suneqing/7313033

三.总结

Web Service技术, 能使得运行在不同机器上的不同应用无须借助附加的、专门的第三方软件或硬件, 就可相互交换数据或集成。依据Web Service规范实施的应用之间, 无论它们所使用的语言、 平台或内部协议是什么, 都可以相互交换数据。

转载的web server实例的更多相关文章

  1. PHP Web Server 实例

    通过WebService,我们可以调用部署在其它地方的程序,而不用关心被调用的程序是在什么平台用什么语言编写的.这里我们使用php调用. 在php4时代调用WebService大部分使用的nusoap ...

  2. 【实例图文详解】OAuth 2.0 for Web Server Applications

    原文链接:http://blog.csdn.net/hjun01/article/details/42032841        OAuth 2.0 for Web Server Applicatio ...

  3. 【转载】springboot启动报错(Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWe)

    SpringBoot启动时的异常信息如下: 1 "C:\Program Files\Java\jdk1.8.0_161\bin\java" ......... com.fangxi ...

  4. Tomcat是怎么工作的(2) -- 动手实现山寨版的简单Web Server

    本文先讲解一下Java web server都是怎么工作的.web server也叫HTTP server——顾名思义它是用HTTP协议和客户端交互的.客户端一般就是各种各样的浏览器了.相信所有朋友都 ...

  5. Atitit。Web server Jetty9 使用 attilax 总结

    Atitit.Web server Jetty9 使用 attilax 总结 1.1. 静态文件的资源1 1.2. Servlet使用1 1.3. code1 1.1. 静态文件的资源 WebAppC ...

  6. Server Develop (九) Simple Web Server

    Simple Web Server web服务器hello world!-----简单的socket通信实现. HTTP HTTP是Web浏览器与Web服务器之间通信的标准协议,HTTP指明了客户端如 ...

  7. Atitit.Gui控件and面板----web server区----- web服务器监控面板and控制台条目

    Atitit.Gui控件and面板----web server区----- web服务器监控面板and控制台条目 1. Resin4.0.22 1 2. 查看http连接数::Summary>& ...

  8. 利用PowerUpSQL攻击SQL Server实例

    这篇博客简述如何快速识别被第三方应用使用的SQL Server实例,该第三方软件用PowerUpSQL配置默认用户/密码配置.虽然我曾经多次提到过这一话题,但是我认为值得为这一主题写一篇简短的博客,帮 ...

  9. 【转载】Windows Server 2012服务器删除IIS方法

    在Windows Server2012版本的服务器系统中,我们可以通过服务器管理器中的"添加角色和功能"来添加IIS的Web服务器,当我们不再使用IIS功能时候,我们也可以通过删除 ...

随机推荐

  1. android手机测试中如何查看内存泄露

    (一) 生成.hprof文件生成.hprof 文件的方法有很多,而且Android 的不同版本中生成.hprof 的方式也稍有差别,我使用的版本的是2.1,各个版本中生成.prof 文件的方法请参考: ...

  2. elasticsearch 一、环境配置

    简介 ElasticSearch是一个开源的分布式搜索引擎,具备高可靠性,支持非常多的企业级搜索用例,是基于Lucene构建的.支持时间时间索引和全文检索.官网:http://www.elastics ...

  3. 前端Js框架 UI框架汇总 特性 适用范围 选择

    身为一个资深后端工程师,面对层出不穷的前端框架,总让人眼花缭乱,做一个综合解析贴,从全局着眼,让我们明白各种前端框架的应用范围,为如何选择前端框架,从不同的维度提供一些线索,做为一个长期优化贴,欢迎指 ...

  4. C++系统自己主动生成默认构造函数的情况

    (1) 基类存在默认构造函数 class CBaseClass { public: CBaseClass() { m_i = 0; } private: int m_i; }; class CDriv ...

  5. Debian/Ubuntu pip default install to $HOME/.local

    pip default install to $HOME/.local on Debian/Ubuntu After pip 8.1.1-2 on Debian or Ubuntu you can p ...

  6. Spring的ApplicationEvent实现

    原理:ApplicationContextAware接口提供了publishEvent方法,实现了Observe(观察者)设计模式的传播机制,实现了对bean的传播.通过ApplicationCont ...

  7. HTTPS原理和CA证书申请(转)

    原文地址:http://blog.51cto.com/11883699/2160032 众所周知,WEB服务存在http和https两种通信方式,http默认采用80作为通讯端口,对于传输采用不加密的 ...

  8. [git/GitHub] git push 时报错:fatal: remote error: You can't push to git://github.com/user/xxx.git(已解决)

    当使用  git push  时,提示以下错误: fatal: remote error: You can't push to git://github.com/user/xxx.git Use ht ...

  9. 《Go语言网络编程》第一章:体系

    原书地址:http://tumregels.github.io/Network-Programming-with-Go 如果不知道想要构建什么,是不可能创建一个系统的.而且如果不知道它工作的环境,也同 ...

  10. 命令行部署SharePoint2016离线前提条件和添加服务器的Feature

    前言 Sp2016的软件环境要求如下: 服务器场中的数据库服务器的最低要求: 以下各项之一: Microsoft SQL Server 2014 Service Pack 1 (SP1) 的 64 位 ...