直接上代码,关键点:

133行的敏感词过滤

176行的6位扩展码写入

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CVTE.Utils;
using ImApiDotNet;
using CVTE.ServiceModel;
using System.Configuration;
using System.Threading;
using System.Text; namespace CVTE.Portal
{
public partial class SmsSend : System.Web.UI.Page
{
private String[] retunvalues = new String[] { "成功", "连接数据库失败", "数据库关闭失败", "数据库插入错误", "数据库删除错误", "数据库查询错误", "参数错误", "API标识非法", "消息内容太长", "没有初始化或初始化失败", "API接口处于暂停(失效)状态", "短信网关未连接" };
private String[] initvalues = new String[] { "成功", "连接失败", "用户名或密码错误", "密码错误", "接口编码不存在" };
private ImApiDotNet.APIClient apiclient; //是否联通号码,用于MAS过滤。在玄武短信中已不用考虑这个问题。
bool isLianTong(string mobile)
{
bool result = false;
if (mobile.StartsWith("") || mobile.StartsWith("") || mobile.StartsWith("") || mobile.StartsWith("")
|| mobile.StartsWith("") || mobile.StartsWith("") || mobile.StartsWith("") || mobile.StartsWith(""))
result = true; return result;
} protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
try
{
if (Request["mobile"] == null)
return;
if (Request["context"] == null)
return;
if (Request["context"].Length < )
return;
if (Request["AppName"] == null)
return; String context = Request["context"].Replace("&", "&").Replace("+", "+").Replace("#", "#"); String mobileStr = Request["mobile"];
if (mobileStr == "")
{
return;
}
string smsProvider = ConfigurationManager.AppSettings["SmsProvider"]; if (Request["SmsProvider"] == null)
{
if (smsProvider.Equals("MAS"))
{
SendByMas(context, mobileStr);
} if (smsProvider.Equals("MOS"))
{
//MOS的发送号码是以空格分隔的,和mas的半角逗号不同
mobileStr = mobileStr.Replace(";", " ").Replace(",", " ");
SendByMos(context, mobileStr);
}
}
else if (Request["SmsProvider"] == "MOS")
{
mobileStr = mobileStr.Replace(";", " ").Replace(",", " ");
SendByMos(context, mobileStr);
} }
catch (Exception ex)
{
//MessageBox.Show(this.Page, ex.Message);
}
} } /// <summary>
/// 玄武短信发送
/// </summary>
/// <param name="context">短信内容</param>
/// <param name="mobileStr">以,分隔的手机号码</param>
private void SendByMos(String context, String mobileStr)
{
string AppName = ""; if (Request["AppName"] == null)
{
AppName = "CVTalk";
}
else
{
AppName = Request["AppName"];
} string Phone6ID = "";
//发送内容
string Context = ""; PhoneDal phoneDal = new PhoneDal();
try
{
string fromPhone = "";
if (Request["from"] != null)
{
if (Request["from"].ToLower() != "sysmessage")
{
fromPhone = phoneDal.GetPhone(Request["from"]);
}
} //MOS只能识别GB2312,所以要对utf-8进行UrlDecode转码
string IMMoshost = ConfigurationManager.AppSettings["MosURL"] + @"username=XXXXXX&password=XXXXXX&";
string contextUrlDecode = System.Web.HttpUtility.UrlDecode(context, System.Text.Encoding.UTF8); //关键词库为空,则从数据库把词库导入内存
if (SensitiveWords.GetWords().Count == )
{
SensitiveWords.SetWords(SensitiveWordsDal.GetAllWords());
} int keyMatchCount = ;
string keys = ""; //开始关键词过滤,通过内存查询
foreach (string word in SensitiveWords.GetWords())
{
if (contextUrlDecode != null)
{
if (contextUrlDecode.Contains(word))
{
StringBuilder sb = new StringBuilder();
foreach (char ch in word)
{
sb.Append(ch + "~");
}
keyMatchCount++;
keys += word + " ";
context = context.Replace(word, sb.ToString());
}
}
} if (keyMatchCount > )
{
string alertMSG = "亲,您先前发送的短信包含运营商敏感字:“ " + keys + " ” 我们修改了您的短信内容后重新发送。修改后短信内容:" + context;
string IMhost = "http://xxxxxxxx.cn:8989/IMMessageAPI/im/toim.do?tousername=" + Request["from"] + "&message=" + alertMSG + "&subject=CVTalk短信异常";
WebHelper.GetWebData(IMhost);
contextUrlDecode = System.Web.HttpUtility.UrlDecode(context, System.Text.Encoding.UTF8); }
string contextSuccess = System.Web.HttpUtility.UrlEncode(contextUrlDecode, System.Text.Encoding.GetEncoding("GB2312")); if (Request["to"] != null)
{
if (Request["cnName"] != null && fromPhone != null)
{
Context = "CVTalk" + HttpUtility.UrlEncode(Request["cnName"], System.Text.Encoding.GetEncoding("GB2312")) + fromPhone + " :" + contextSuccess + ConfigurationSettings.AppSettings["smsAD"];
}
else
Context = contextSuccess;
}
else
{
Context = contextSuccess;
} if (string.IsNullOrEmpty(fromPhone))
{
//fromPhone为空,则一定是系统发送的短信,不予处理回复,统一设置扩展码为0
Phone6ID = "";
}
else//查询到用户的手机号
{
//获取用户的手机号后6位,这里是为了定位用户,有100万分之一的重复风险
Phone6ID = fromPhone.Substring();
} string result = WebHelper.GetWebData(IMMoshost + "to=" + mobileStr + "&text=" + Context + "&subid=" + Phone6ID + "&msgtype=1");
//对第一次发送失败,进行第二次补发
if (result != "")
{
Thread.Sleep();
//第二次发送
string result2 = WebHelper.GetWebData(IMMoshost + "to=" + mobileStr + "&text=" + Context + "&subid=" + Phone6ID + "&msgtype=1");
Response.Write(result2);
Response.End();
}
Response.Write(result);
Response.End();
}
catch (Exception ex)
{
//Response.Write(ex.Message);
}
} /// <summary>
/// 移动MAS短信发送
/// </summary>
/// <param name="context">短信内容</param>
/// <param name="mobileStr">以,或;分隔的手机号码</param>
/// <returns></returns>
private string SendByMas(String context, String mobileStr)
{
string AppName = ""; if (Request["AppName"] == null)
{
AppName = "CVTalk";
}
else
{
AppName = Request["AppName"];
} apiclient = new APIClient(); int con = apiclient.init("172.xx.xx.xx", "bpm", "bpm", "BPM", "mas");
con = System.Math.Abs(con); mobileStr = mobileStr.Replace(",", ","); int sm = ;
//if (Request["cnName"] != null)
Random r = new Random();
int smsID = r.Next(, );//产生0-999999之间的整数,用于无后六位号码的情况
int Phone6ID = ;
//只支持发送一个手机号码
string Context = ""; PhoneDal phoneDal = new PhoneDal();
string fromPhone = phoneDal.GetPhone(Request["from"]); if (Request["to"] != null)
{
if (Request["cnName"] != null)
Context = "CVTalk" + Request["cnName"] + fromPhone + " :" + context + ConfigurationSettings.AppSettings["smsAD"];
else
Context = context; }
else
{
Context = context;
} string[] mobiles = mobileStr.Split(',');
if (string.IsNullOrEmpty(fromPhone) || mobiles.Length > )
{
//sm = apiclient.sendSM(mobiles, Context, smsID);
for (int i = ; i < mobiles.Length; i++)
{
//如果是联通号码
if (isLianTong(mobiles[i]))
{
sm = apiclient.sendSM(mobiles[i], Context + "回复TD退订", smsID);
}
else
{
sm = apiclient.sendSM(mobiles[i], Context, smsID);
} }
}
else
{ //sm = apiclient.sendSM(mobileStr.Split(','), Context, smsID);
//发送者号码的后6位
//smsID和srcID 没有办法分离,如果分置,将无法回复,因为将溯源至srcID,而不是smsID。
//smsID和srcID就必须设置为一样的!这样可以让发送者固定发送号码,但这样发送者给所有人的smsID都将将相同,
//造成的结果是,第一次发送可以成功,第一次回复是可以成功,但第三次交互(发送者再次回复),信息会回复错乱
//(这是因为在发送者给多人发送信息后,smsID是一样的,第三次回复将无法准确定位)
//目前采用后6位为ID的方法,因为最多只能拿6位,如果重复将异常。 Phone6ID = Convert.ToInt32(fromPhone.Substring());
sm = apiclient.sendSM(mobiles, Context, Phone6ID);
smsID = Phone6ID;
} apiclient.release(); CVTE.Model.IMSms model = new CVTE.Model.IMSms();
model.AppName = AppName;
model.FromUserName = Request["from"];
model.ToUserName = Request["to"];
model.ID = Guid.NewGuid().ToString(); System.Text.Encoding utf8 = System.Text.Encoding.UTF8;
byte[] key = Convert.FromBase64String("YzJjZGxmZ2hcamtsvW5vcHbycXXXXXXXXXX");
byte[] iv = new byte[] { , , , , , , , }; //当模式为ECB时,IV无用
byte[] data = utf8.GetBytes(context); byte[] byteEncode = DES3.Des3EncodeECB(key, iv, data);
string strEncode = Convert.ToBase64String(byteEncode);
//加密
model.Msg = sm.ToString() + "-" + strEncode; model.SendTime = System.DateTime.Now;
model.SmsID = smsID;// Convert.ToInt32(mobileStr.Split(',')[0].Substring(3)); srcID;//
model.ToMoblie = Request["mobile"];
model.FromUserCNName = Request["cnName"]; CVTE.BLL.IMSms bll = new CVTE.BLL.IMSms();
bll.Add(model); Response.Write(retunvalues[sm]);
Response.End();
return mobileStr;
}
}
}
05-06 23:55