//httppost.cs 文件内容(放入App_code里面)
public class httppost : System.Web.IHttpHandler
{
private string mobile;//手机号
private string amount;//交易金额
private string user;//用户
private string orderNo;//订单号
private string payType;// '0' - 话费支付 '1' - 移动电子账户支付
private string trxType;//交易类型
string nowTime;
public http_SpPay()
{
}
public void ProcessRequest(HttpContext context)
{
HttpRequest Request = context.Request;
Stream content = Request.InputStream;
StreamReader sr = new StreamReader(content);
string resStr = sr.ReadToEnd();
sr.Close();
content.Close();
string clientIP = GetUserIP(Request);
if (ReadReceiveXml(resStr))//读取接受报文
{
SpPay pay = new SpPay();
if (PaySend(pay,clientIP, mobile, amount, orderNo, trxType, payType))
{
ReturnTo("<?xml version='1.0' encoding='gb2312'?><cmts>" +
"<UserMobile>" + mobile + "</UserMobile>" +
"<Amount>" + amount + "</Amount>" +
"<OrderNo>" + orderNo + "</OrderNo>" +
"<ResultCode>0</ResultCode>" +
"<ResultCodeInfo>扣费成功</ResultCodeInfo>" +
"</cmts>", context);
}
else
{
ReturnTo("<?xml version='1.0' encoding='gb2312'?><cmts>" +
"<UserMobile>" + mobile + "</UserMobile>" +
"<Amount>" + amount + "</Amount>" +
"<OrderNo>" + orderNo + "</OrderNo>" +
"<ResultCode>" + pay.ResultCode + "</ResultCode>" +
"<ResultCodeInfo>" + pay.ErroString + "</ResultCodeInfo>" +
"</cmts>", context);
}
pay = null;
}
else
{
ReturnTo("<?xml version='1.0' encoding='gb2312'?><cmts>" +
"<ResultCode>2</ResultCode>" +
"<ResultCodeInfo>报文错误</ResultCodeInfo>" +
"</cmts>", context);
}
}
public bool IsReusable
{
get { return false; }
}
//取客户端IP
public string GetUserIP(HttpRequest Request)
{
string userIP;
if (Request.ServerVariables["HTTP_VIA"] == null)
{
userIP = Request.UserHostAddress;
}
else
{
userIP = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
}
return userIP;
}
#region 即时返回
private void ReturnTo(string content, HttpContext context)
{
HttpResponse Response = context.Response;
Response.Write(content);
}
#endregion
#region 读取post内容 读取xml
private bool ReadReceiveXml(string receiveXml)
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load(new StringReader(receiveXml)); // 装入指定的XML文档
XmlNodeList elemList = doc.GetElementsByTagName("xml");
XmlNodeList subelemList;
XmlElement elem = (XmlElement)elemList[0];
subelemList = elem.GetElementsByTagName("UserMobile");
mobile = subelemList[0].InnerText;
subelemList = elem.GetElementsByTagName("Amount");
amount = subelemList[0].InnerText;
subelemList = elem.GetElementsByTagName("user");
user = subelemList[0].InnerText;
subelemList = elem.GetElementsByTagName("OrderNo");
orderNo = subelemList[0].InnerText;
subelemList = elem.GetElementsByTagName("PayType");
payType = subelemList[0].InnerText;
subelemList = elem.GetElementsByTagName("TrxType");
trxType = subelemList[0].InnerText;
}
catch
{
return false;
}
return true;
}
#endregion
#region 支付方法
private bool PaySend(SpPay pay,string clientIP, string userMobile,
string amount, string orderNo, string trxType, string payType)
{
nowTime = Convert.ToString(DateTime.Now.Year) +
String.Format("{0:00}",
Int32.Parse(Convert.ToString(DateTime.Now.Month))) +
String.Format("{0:00}", Int32.Parse(Convert.ToString(DateTime.Now.Day)))
+ String.Format("{0:00}",
Int32.Parse(Convert.ToString(DateTime.Now.Hour))) +
String.Format("{0:00}",
Int32.Parse(Convert.ToString(DateTime.Now.Minute))) +
String.Format("{0:00}",
Int32.Parse(Convert.ToString(DateTime.Now.Second)));;//'SP交易时间:年月日时分
秒:20030420233020
return (bool));
}
#endregion
}
//webconfig配置
</system.web>
<httpHandlers>
<add verb="*" path="httppost.cmts" type="httppost"/>
</httpHandlers>
</system.web>
//IIS配置
网站属性--〉主目录 --〉配置 -->添加 --〉浏览这个文件(aspnet_isapi.dll)--〉扩展名(cmts) --〉确定