C#当前位置:首页 代码示例-C#
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Security.Cryptography;

namespace TestMt
{
    /// 
    /// mt3 接口测试类。 直接调用 TestMt3.Main1 方法
    /// 修改 src 用户名 和 pwd 密码, dest: 目的号码, sender:原号码
    /// 
    public class TestMt3
	{ 
      public static void Main1(string[] args)
        {
			
          String URL = "http://210.51.190.233:8085/mt/mt3.ashx"; //接口地址
          String fmt = "src={0}&pwd={1}&ServiceID=SEND&dest={2}&sender={3}&msg={4}&codec=8"; // HTTP 请求参数格式

          string src = "";  // 你的用户名, 必须有值
          string pwd = "";  // 你的密码, 必须有值
          string dest = "861370xxxxxxx";// 你的目的号码【收短信的电话号码】, 必须有值
          // 你的原号码,可空【大部分国家原号码带不过去,只有少数国家支持透传,所有一般为空】
          string sender = "8613701234567";   
          // 短信内容
          string msg = "大家啊 , 测试一下 啊 ...hello ..";//你的短信内容

          // codec=8, Unicode 编码,  3, ISO-8859-1, 0 ASCII
          // 短信内容 HEX 编码,8 为 UTF-16BE HEX 编码, dataCoding = 8 ,支持所有国家的语言,建议直接使用 8
          string hexmsg = encodeHexStr(8, msg); 

          // 格式化 HTTP 请求参数格式
          string poststring = string.Format(fmt, new string[] { src, pwd, dest, sender, hexmsg }); 
          byte[] postData = System.Text.Encoding.ASCII.GetBytes(poststring);

          // HTTP 封包请求,返回结果,如果是以 “-” 开头的为发送失败,请查看错误代码,否则为MSGID
          string rs = doPostRequest(URL, postData); // HTTP 封包请求

          Console.WriteLine("SEND RESULT:" + rs);
          Console.ReadLine();
        }


        //字符编码成HEX
        private static String encodeHexStr(int dataCoding, String realStr)
        {
            string strhex = "";
            try
            {
                Byte[] bytSource = null;
                if (dataCoding == 15)
                {
                    bytSource = Encoding.GetEncoding("GBK").GetBytes(realStr);
                }
                else if (dataCoding == 3)
                {
                    bytSource = Encoding.GetEncoding("ISO-8859-1").GetBytes(realStr);
                }
                else if (dataCoding == 8)
                {
                    bytSource = Encoding.BigEndianUnicode.GetBytes(realStr);
                }
                else
                {
                    bytSource = Encoding.ASCII.GetBytes(realStr);
                }
                for (int i = 0; i < bytSource.Length; i++)
                {
                    strhex = strhex + bytSource[i].ToString("X2");

                }
            }
            catch (System.Exception err)
            {
                return realStr;
            }
            return strhex;
        }

        //hex编码还原成字符
        private static String decodeHexStr(int dataCoding, String hexStr)
        {
            String strReturn = "";
            try
            {
                int len = hexStr.Length / 2;
                byte[] bytSrc = new byte[len];
                for (int i = 0; i < len; i++)
                {
                    string s = hexStr.Substring(i * 2, 2);
                    bytSrc[i] = Byte.Parse(s, System.Globalization.NumberStyles.AllowHexSpecifier);
                }

                if (dataCoding == 15)
                {
                    strReturn = Encoding.GetEncoding("GBK").GetString(bytSrc);
                }
                else if (dataCoding == 3)
                {
                    strReturn = Encoding.GetEncoding("ISO-8859-1").GetString(bytSrc);
                }
                else if (dataCoding == 8)
                {
                    strReturn = Encoding.BigEndianUnicode.GetString(bytSrc);
                }
                else
                {
                    strReturn = System.Text.ASCIIEncoding.ASCII.GetString(bytSrc);
                }
            }
            catch (System.Exception err)
            {
                return hexStr;
            }
            return strReturn;
        }



        //POST方式发送得结果
        public 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 = 30000;
                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)
            {
                WriteErrLog(err.ToString());
                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)
            {
                WriteErrLog(err.ToString());
            }

            return strResult;
        }

        //GET方式发送得结果
        public static String doGetRequest(string url)
        {
            HttpWebRequest hwRequest;
            HttpWebResponse hwResponse;

            string strResult = string.Empty;
            try
            {
                hwRequest = (System.Net.HttpWebRequest)WebRequest.Create(url);
                hwRequest.Timeout = 30000;
                hwRequest.Method = "GET";
                hwRequest.ContentType = "application/x-www-form-urlencoded";
            }
            catch (System.Exception err)
            {
                WriteErrLog(err.ToString());
                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)
            {
                WriteErrLog(err.ToString());
            }

            return strResult;
        }

        public static void WriteErrLog(string strErr)
        {
            Console.WriteLine(strErr);
            System.Diagnostics.Trace.WriteLine(strErr);
        }

        const string SEND_SUC = "0"; // 发送成功
        const string SEND_ERROR = "-01"; // 系统维护中,请联系客服
        const string NOT_ENOUGHCREDITS = "-02"; // 当前账号余额不足
        const string ACCOUNT_BLOCKED = "-03"; // 帐号停止
        const string NETWORK_NOTCOVERED = "-04"; // 目的号码运营商不在服务覆盖范围
        const string DEST_NUMBER_EXCEED_MAX = "-05"; // 目的手机号码数量超长(30/次,超30个请自行做循环)
        const string INVALID_USER_OR_PASS = "-06"; // 用户或密码错误
        const string MISSING_DESTINATION_ADDRESS = "-07"; // 目的号码不能为空
        const string MISSING_SMSTEXT = "-08"; // 短信内容不能为空
        const string MISSING_SENDERNAME = "-09"; //源号码不能为空
        const string DEST_INVALIDFORMAT = "-10"; // DEST参数格式错误
        const string MISSING_USERNAME = "-11"; // 用户名空
        const string MISSING_PASS = "-12"; // 密码空
        const string NETWORK_FAIL = "-13"; // 网络错误
        const string INTERAL_ERROR = "-14"; // 内部错误
        const string INVALID_DESTINATION_ADDRESS = "-15"; // 非法手机号码,手机号码格式不对
        // 短信内容超长!(UNICODE最大70个字符,Alphabet编码(英文即以此方式传输)最大160字符)
        const string INVALID_SMS_MAX_LENGTH = "-16"; 
        const string BLACKWORD_IN_SMS = "-17"; // 短信内容含有非法字符
        const string DEST_IN_BLACKLIST = "-18"; //目的手机号码限制
        const string INVALID_DCS = "-19"; // 短信内容编码不对(比如发中文、韩文、日文而用Alphabet编码方式)
        const string INTERAL_ERROR_SERVICE_NO_START = "-20"; // 预处理服务没有启动
    }
}
        
  • 400-089-6671

    isms360@126.com

  • 关于我们/联系我们/帮助中心

    北京信合瑞科技有限公司 地址:北京市石景山区万达广场C座1315 邮编:100043

    @2010-2017 isms360.com, all rights reserved 京ICP备10001555号 增值电信经营许可证 京 B2-20120171