生活随笔
收集整理的這篇文章主要介紹了
短信发送程序
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
先在中國網建注冊賬戶 明確用戶名和 密鑰 創建一個c#的Windows窗體應用程序 創建類
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace asms
{public class zzq{private string THE_UID = "zzzq"; //用戶名private string THE_KEY = "d41d8cd98f00b204e980"; //接口秘鑰public void Phone(string number, string smsText){string PostUrl = GetPostUrl(number, smsText);string result = PostSmsInfo(PostUrl);string t = GetResult(result);MessageBox.Show(GetResult(result));}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;}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;}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;}}
}
窗體后臺代碼
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace asms
{public partial class frmMain : Form{public frmMain(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){string a = textBox1.Text;string b = textBox2.Text;zzq c = new zzq();c.Phone(a, b);}}
}
總結
以上是生活随笔 為你收集整理的短信发送程序 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。