/* 功能: 企业短信通 C# HTTP接口 发送短信 修改日期: 2014-09-01 说明: http://api.cnsms.cn/?ac=send&uid=用户账号&pwd=MD5位32密码&mobile=号码&content=内容 状态: 100 发送成功 101 验证失败 102 短信不足 103 操作失败 104 非法字符 105 内容过多 106 号码过多 107 频率过快 108 号码内容空 109 账号冻结 110 禁止频繁单条发送 111 系统暂定发送 112 号码不正确 120 系统升级 */ using System; using System.Text; using System.Net; using System.IO; using System.Data; namespace EsmsTest { class SendEsms { static void Main(string[] args) { string strContent = "企业短信通 测试c#"; //GET 方式 String getReturn = doGetRequest("http://api.cnsms.cn/?ac=send&uid=100226&pwd=fa246d0262c3925617b0c72bb20eeb1d&mobile=13585519197,13900008888&content=" + strContent); Console.WriteLine("Get response is: " + getReturn); StringBuilder sbTemp = new StringBuilder(); //POST sbTemp.Append("ac=send&uid=70299999&pwd=fa246d0262c3925617b0c72bb20eeb1d&mobile=13339196131,15375379376&content=" + strContent); byte[] bTemp = Encoding.ASCII.GetBytes(sbTemp.ToString()); String postReturn = doPostRequest("http://api.cnsms.cn/", bTemp); Console.WriteLine("Post response is: " + postReturn); } //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) { 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方式发送得结果 private static String doGetRequest(string url) { HttpWebRequest hwRequest; HttpWebResponse hwResponse; string strResult = string.Empty; try { hwRequest = (System.Net.HttpWebRequest)WebRequest.Create(url); hwRequest.Timeout = 5000; 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; } private static void WriteErrLog(string strErr) { Console.WriteLine(strErr); System.Diagnostics.Trace.WriteLine(strErr); } } }
返回结果
100
获取余额
请求
/* 功能: 企业短信通 C# HTTP接口 取余额 修改日期:2014-09-01 说明: http://api.cnsms.cn/?ac=gc&uid=用户账号&pwd=MD5位32密码 状态: 100 发送成功 101 验证失败 102 短信不足 103 操作失败 104 非法字符 105 内容过多 106 号码过多 107 频率过快 108 号码内容空 109 账号冻结 110 禁止频繁单条发送 111 系统暂定发送 112 号码不正确 120 系统升级 */ using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using Present.Model; using Present.DAL; using Present.BLL; using System.Data.SqlClient; using System.Text; using System.Net; using System.IO; using System.Globalization; using System.Security.Cryptography; public partial class _Default : System.Web.UI.Page { #region 短信发送 protected void Button2_Click(object sender, EventArgs e) { string uid="100226"; //用户名 string pass = "100226"; //密码 StringBuilder sbTemp = new StringBuilder(); pass = FormsAuthentication.HashpwdForStoringInConfigFile(pass, "MD5"); //密码进行MD5加密 //POST 传值 sbTemp.Append("ac=gc&uid="+uid+"&pwd=" + pass ); byte[] bTemp = System.Text.Encoding.GetEncoding("GBK").GetBytes(sbTemp.ToString()); String postReturn = doPostRequest("http://api.cnsms.cn/", bTemp); Response.Write("Post response is: " + postReturn); //测试返回结果 } //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) { 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; } private static void WriteErrLog(string strErr) { Console.WriteLine(strErr); System.Diagnostics.Trace.WriteLine(strErr); } #endregion }
返回结果
100||22348
时间: 2024-10-12 09:43:21