WebService介绍(三):使用WebService代理类
<%
@
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.Collections;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
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))
{
sqlConn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MobileConnectionString"].ToString();
string commandText = "select PhoneMoney from dbo.PhoneInfo where PhoneNumber=" + PhoneNumber;
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,用以报告错误(比如,密码错误),可能还会加上其他的校验,安全控制等等。
……
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。