.NET短信接口 实例

  <!--短信接口Url-->
  <add key="SendUrl" value="http://api.sms7.cn/tx/" />
  <!--用户名-->
  <add key="Uid" value="98816" />
  <!--密码-->
  <add key="Pwd" value="std5805122" />
  <!-- ================== 8:短信接口配置 END================== -->

using DotNet.Kernel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web.Security;

namespace DotNet.Utilities
{
    public class MessageInterface
    {
        #region 数据发送
        public static bool send(string strMessage, string Mobile)
        {
            string sendurl = ConfigHelper.GetValue("SendUrl");//"http://api.sms7.cn/tx/";
            string mobile = Mobile;//发送号码
            string uid = ConfigHelper.GetValue("Uid");
            string pwd = ConfigHelper.GetValue("Pwd");
            string strContent ="";
            //判断是否已存在“【刷咯】”
            if (strMessage.IndexOf("【刷咯】") > 0)
            {
                strContent = strMessage;
            }
            else
            {
                strContent = strMessage + "【刷咯】";
            }
            StringBuilder sbTemp = new StringBuilder();

            string Pass = FormsAuthentication.HashPasswordForStoringInConfigFile(pwd + uid, "MD5"); //密码进行MD5加密
            //POST 传值
            sbTemp.Append("uid=" + uid + "&pwd=" + Pass + "&mobile=" + mobile + "&content=" + strContent);
            byte[] bTemp = System.Text.Encoding.GetEncoding("GBK").GetBytes(sbTemp.ToString());
            String postReturn = doPostRequest(sendurl, bTemp);
            DotNet.Kernel.DbLog.WriteLog("Post response is: " + postReturn);//测试返回结果
            if (postReturn.Contains("100"))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        //POST方式发送得结果
        private static String doPostRequest(string url, byte[] bData)
        {
            System.Net.HttpWebRequest hwRequest;
            System.Net.HttpWebResponse hwResponse;

            string strResult = string.Empty;
            try
            {
                hwRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
                hwRequest.Timeout = 5000;
                hwRequest.Method = "POST";
                hwRequest.ContentType = "application/x-www-form-urlencoded";
                hwRequest.ContentLength = bData.Length;

                System.IO.Stream smWrite = hwRequest.GetRequestStream();
                smWrite.Write(bData, 0, bData.Length);
                smWrite.Close();
            }
            catch (System.Exception err)
            {
                DbLog.WriteException(err);
                return strResult;
            }

            //get response
            try
            {
                hwResponse = (HttpWebResponse)hwRequest.GetResponse();
                StreamReader srReader = new StreamReader(hwResponse.GetResponseStream(), Encoding.ASCII);
                strResult = srReader.ReadToEnd();
                srReader.Close();
                hwResponse.Close();
            }
            catch (System.Exception err)
            {
                DbLog.WriteException(err);
            }
            return strResult;
        }
        #endregion
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值