C#技术百科
问问你的心你有没有信心 做事情要专一坚定,执着
using System;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Web;

namespace Report
{
    /// <summary>
    /// 结构名:PESMS
    /// 编  写:张远强
    /// 版  本:1.0.0
    /// 日  期:2009-12-22
    /// 描  述:动易短信通接口参数
    /// </summary>
    public struct PESMS
    {
        public string URL;
        public string Key;
        public string ID;
        public string UserName;
        public string SendNum;
        public string Content;
        public string SendTiming;
        public string SendTime;
        public string MD5String;
        public string Reserve;
    }

    /// <summary>
    /// 类 名:SendSMS
    /// 编  写:张远强
    /// 版  本:1.0.0
    /// 日  期:2009-12-21
    /// 描  述:短信发送类
    /// </summary>
    public static class SendSMS
    {
        public static string Send(string sendNum, string content)
        {
            /*** 参数配置 ***/
            PESMS pesms = new PESMS();
            pesms.URL = "http://sms.powereasy.net/MessageGate/Message.aspx";
            pesms.Key = "";//需修改,MD5私钥,短信通平台设置获取
            pesms.ID = DateTime.Now.ToString("yyyyMMddHHmmss");
            pesms.UserName = "";//需修改,短信通平台用户名
            pesms.SendNum = sendNum;
            pesms.Content = content;
            pesms.SendTiming = "0";
            pesms.SendTime = DateTime.Now.ToString();
            pesms.Reserve = "";
            pesms.MD5String = MD5(pesms.ID + pesms.UserName + pesms.Key + pesms.SendNum + pesms.Content + pesms.SendTiming + pesms.SendTime);

            /*** 短信发送 ***/
            try
            {
                //编码方式
                Encoding gb2312 = Encoding.GetEncoding("gb2312");

                //接口地址
                string sendURL = string.Format("{0}?ID={1}&UserName={2}&SendNum={3}&Content={4}&SendTiming={5}&SendTime={6}&MD5String={7}&Reserve={8}", pesms.URL, pesms.ID, pesms.UserName, pesms.SendNum, HttpUtility.UrlEncode(pesms.Content, gb2312), pesms.SendTiming, pesms.SendTime, pesms.MD5String, HttpUtility.UrlEncode(pesms.Reserve, gb2312));

                //请求
                WebRequest Request = WebRequest.Create(sendURL);
                Request.ContentType = "application/x-www-form-urlencoded";
                Request.Method = "GET";

                //响应
                WebResponse Response = Request.GetResponse();
                using (StreamReader Reader = new StreamReader(Response.GetResponseStream(), gb2312))
                {
                    string message = Reader.ReadToEnd();

                    if (message.IndexOf("短信已经提交到发送队列") != -1)
                        return "1|" + message + "|" + sendURL;
                    else
                        return "0|" + message + "|" + sendURL;
                }
            }
            catch(Exception ex)
            {
                return "0|" + ex.Message;
            }
        }

        /// <summary>
        /// MD5加密(兼容ASP)
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        private static string MD5(string str)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] t = md5.ComputeHash(Encoding.GetEncoding("gb2312").GetBytes(str));
            StringBuilder sb = new StringBuilder(32);
            for (int i = 0; i < t.Length; i++)
            {
                sb.Append(t[i].ToString("x").PadLeft(2, '0'));
            }
            return sb.ToString();
        }

    }
}
posted on 2010-10-18 22:15  王德田  阅读(506)  评论(0编辑  收藏  举报