WebService介绍(三):使用WebService代理类
在上一篇中,我们已经生成了WebService代理类,下面的内容其实是很简单的,利用WebService调用远程机器上的类和调用本地机器上的类的方法是一致的。
先新建一个名称为Mobile的站点,这个站点就是我们模拟的通讯运营商的站点。在该站点的Default.aspx页面中,添加如下代码:
<%
@
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>
缴费</title>
</
head
>
<
body
>
<form id="form1" action="Result.aspx" method="post">
<div>
<span>
手机号:<input name="PhoneNumber" type="text" style="width: 150px" /><br />
银行帐号:<input name="AccountID" type="text" style="width: 150px" /><br />
帐号密码:<input name="Password" type="password" style="width: 150px" /><br />
缴费金额:<input name="deductMoney" type="text" style="width: 150px" /><br />
<input id="Submit1" type="submit" value="submit"/>
</span>
</div>
</form>
</
body
>
</
html
>
这个页面就是我们缴费的主页面,在这个页面上,缴费金额是通讯运营商和银行都需要了解的信息,手机号是通讯运营商所要了解的信息,银行帐号和帐号密码是银行所要了解的信息。
下面看看处理数据的页面Result.aspx:
<%
@
Page Language="C#" AutoEventWireup="true" CodeFile="Result.aspx.cs" Inherits="Result" %>
<!
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>
缴费结果</title>
</
head
>
<
body
>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server"/>
</div>
</form>
</
body
>
</
html
>
后台代码如下:
using
System;
using
System.Data;
using
System.Configuration;
using
System.Collections;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
using
System.Data.SqlClient;
using
Bank.WebService;
public
partial class Result : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Account acc = new Account();
string PhoneNumber = Request.Form["PhoneNumber"];
string AccountID = Request.Form["AccountID"];
string Password = Request.Form["Password"];
int deductMoney = int.Parse(Request.Form["deductMoney"]);
this.Label1.Text = "
正在联系银行,请稍候..."
;
try
{
if (acc.Deduct(AccountID, Password, deductMoney))
{
SqlConnection SqlConn = new SqlConnection();
SqlConn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MobileConnectionString"].ToString();
SqlConn.Open();
string commandText = "select PhoneMoney from dbo.PhoneInfo where PhoneNumber=" + PhoneNumber;
SqlCommand SqlComm = new SqlCommand(commandText, SqlConn);
int PhoneMoney = int.Parse(SqlComm.ExecuteScalar().ToString());
PhoneMoney = PhoneMoney + deductMoney;
SqlComm.CommandText = "update dbo.PhoneInfo set PhoneMoney=" + PhoneMoney + " where PhoneNumber=" + PhoneNumber;
SqlComm.ExecuteNonQuery();
SqlConn.Close();
this.Label1.Text = "
缴费成功!"
;
}
else
{
this.Label1.Text = "
缴费失败!"
;
}
}
catch (Exception ex)
{
this.Label1.Text = "
缴费失败!"
;
}
}
}
在处理页面,先创建一个代理类的对象,利用Default页面post过来的参数调用Account类的
Deduct
的方法,若改方法返回true,则相应的在通讯运营商的库中加上缴费的金额,并报告缴费成功;若返回false,则报告缴费失败。
在此过程中,若发生异常,也会报告缴费失败。
程序运行界面如下:
实际的商业应用是不可能如此简单的,下面就这个模拟实例中不合理的部分予以解释:
1.
在本实例中,缴费可以是负的,并且也能成功!实际是不允许的,这个可以在客户端验证。还有一些需要控制,比如缴费的限额等等。
2.
在本实例中,Account的
Deduct
只返回true或false,这实际上是非常简化的,在商业应用中,可能还有的Deduct返回string,用以报告错误(比如,密码错误),可能还会加上其他的校验,安全控制等等。
3.
在
在本实例中,密码是明文存储的,这在商业应用中是绝对不允许的!
4.
在
在本实例中,没有很好的机制能够保证“在Bank库中扣款”和“在Mobile库中缴费”这两个操作的事务性。很显然,如果用户信用卡被扣了款,而又没缴上费,他们一定会投诉银行和通讯运营商。
……
其他的一些内容将在以后的文章中讨论。