1. 程式人生 > >C# 實現傳送手機簡訊(可用)

C# 實現傳送手機簡訊(可用)

前幾天分享了java如何對接簡訊介面的例項,這兩天技術經理又佈置了任務要求在老的網站也要對接簡訊介面,撿起了之前學習的c#,但是因為年代久遠,所有在技術方面略有不熟悉的,就參考了其他同行的程式碼~~加以修改,現在把原始碼分享給大家,希望可以給大家一個幫助,當然簡訊驗證碼還是用的互億無線的簡訊平臺,www.ihuyi.com

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
 
namespace WindowsForm
{
    public partial class Form9 : Form
    {
        private string THE_UID = "dourong"; //使用者名稱
        private string THE_KEY = "f891aefbdbfac7a3da9f"; //介面祕鑰
        public Form9()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            string number = textBox1.Text;
            string smsText = textBox2.Text;
            string PostUrl = GetPostUrl(number,smsText);
 
            string result = PostSmsInfo(PostUrl);
 
            string t = GetResult(result);
        }
        /// <summary>返回UTF-8編碼傳送介面地址</summary>
        /// <param name="receivePhoneNumber">目的手機號碼(多個手機號請用半形逗號隔開)</param>
        /// <param name="receiveSms">簡訊內容,最多支援400個字,普通簡訊70個字/條,長簡訊64個字/條計費</param>
        /// <returns></returns>
        public string GetPostUrl(string smsMob, string smsText)
        {
            string postUrl = "http://utf8.api.smschinese.cn/?Uid=" + THE_UID + "&key=" + THE_KEY + "&smsMob=" + smsMob + "&smsText=" + smsText;
            return postUrl;
        }
        /// <summary>
        /// 傳送簡訊,得到返回值
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public string PostSmsInfo(string url)
        {
            //呼叫時只需要把拼成的URL傳給該函式即可。判斷返回值即可
            string strRet = null;
 
            if (url == null || url.Trim().ToString() == "")
            {
                return strRet;
            }
            string targeturl = url.Trim().ToString();
            try
            {
                HttpWebRequest hr = (HttpWebRequest)WebRequest.Create(targeturl);
                hr.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
                hr.Method = "GET";
                hr.Timeout = 30 * 60 * 1000;
                WebResponse hs = hr.GetResponse();
                Stream sr = hs.GetResponseStream();
                StreamReader ser = new StreamReader(sr, Encoding.Default);
                strRet = ser.ReadToEnd();
            }
            catch (Exception ex)
            {
                strRet = null;
            }
            return strRet;
        }
        /// <summary>
        /// 確認返回資訊 
        /// </summary>
        /// <param name="strRet"></param>
        /// <returns></returns>
        public string GetResult(string strRet)
        {
            int result = 0;
            try
            {
                result = int.Parse(strRet);
                switch (result)
                {
                    case -1:
                        strRet = "沒有該使用者賬戶";
                        break;
                    case -2:
                        strRet = "介面金鑰不正確,不是賬戶登陸密碼";
                        break;
                    case -21:
                        strRet = "MD5介面金鑰加密不正確";
                        break;
                    case -3:
                        strRet = "簡訊數量不足";
                        break;
                    case -11:
                        strRet = "該使用者被禁用";
                        break;
                    case -14:
                        strRet = "簡訊內容出現非法字元";
                        break;
                    case -4:
                        strRet = "手機號格式不正確";
                        break;
                    case -41:
                        strRet = "手機號碼為空";
                        break;
                    case -42:
                        strRet = "簡訊內容為空";
                        break;
                    case -51:
                        strRet = "簡訊簽名格式不正確,介面簽名格式為:【簽名內容】";
                        break;
                    case -6:
                        strRet = "IP限制";
                        break;
                    default:
                        strRet = "傳送簡訊數量:" + result;
                        break;
                }
            }
            catch (Exception ex)
            {
                strRet = ex.Message;
            }
            return strRet;
        }
    }
}