C#短信接口、C#短信验证码接口源码、.NET发短信接
时间:2016-06-13 11:56 来源:原创 作者:admin
C#短信接口、C#短信验证码接口源码、.NET发短信接口
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Web;
/*
* 乱码问题解决方案,1、GBK编码提交的首先urlencode短信内容(content),然后在API请求时,带入encode=gbk
2、UTF-8编码的将content 做urlencode编码后,带入encode=utf8或utf-8
实例:http://m.5c.com.cn/api/send/index.php?username=XXX&password_md5=XXX&apikey=XXX&mobile=XXX&content=%E4%BD%A0%E5%A5%BD%E6%89%8D%E6%94%B6%E7%9B%8A%E9%9F%A6&encode=utf8
*
* 关于内容转码问题。 UTF-8 转 GBK:$content = iconv("UTF-8","GBK//IGNORE",$content);GBK 转 UTF-8:$content = iconv("GBK","UTF-8",$content);
*
* username 用户名
* password_md5 密码
* mobile 手机号
* apikey apikey秘钥
* content 短信内容
* startTime UNIX时间戳,不写为立刻发送,http://tool.chinaz.com/Tools/unixtime.aspx (UNIX时间戳网站)
*
* success:msgid 提交成功。
error:msgid 提交失败
error:Missing username 用户名为空
error:Missing password 密码为空
error:Missing apikey APIKEY为空
error:Missing recipient 手机号码为空
error:Missing message content 短信内容为空
error:Account is blocked 帐号被禁用
error:Unrecognized encoding 编码未能识别
error:APIKEY or password error APIKEY或密码错误
error:Unauthorized IP address 未授权 IP 地址
error:Account balance is insufficient 余额不足
*/
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
//UTF-8格式: String encode="UTF-8"
String encode = "UTF-8"; //页面编码和短信内容编码为GBK。重要说明:如提交短信后收到乱码,请将GBK改为UTF-8测试。如本程序页面为编码格式为:ASCII/GB2312/GBK则该处为GBK。如本页面编码为UTF-8或需要支持繁体,阿拉伯文等Unicode,请将此处写为:UTF
String username = ""; //用户名
String password_md5 = ""; ////32位MD5密码加密,不区分大小写
String mobile = ""; //手机号,只发一个号码:13800000001。发多个号码:13800000001,13800000002,...N 。使用半角逗号分隔。
String apikey = ""; //apikey秘钥(请登录 http://m.5c.com.cn 短信平台-->账号管理-->我的信息 中复制apikey)
string content = "您好!您的验证码是:1234【美联】"; //要发送的短信内容,特别注意:签名必须设置,网页验证码应用需要加添加【图形识别码】。;
String content2 = UrlEncode(content);
//新建一个StringBuilder类
StringBuilder sbTemp = new StringBuilder();
//发送链接(用户名,密码,手机号,apikey,短信内容,编码格式)
sbTemp.Append("username=" + username + "&password_md5=" + password_md5 + "&mobile=" + mobile + "&apikey=" + apikey + "&content=" + content2 + "&encode=" + encode);
//对短信内容做Urlencode编码操作。
byte[] bTemp = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(sbTemp.ToString());
//发送返回的结果存入result中
String result = PostRequest("http://m.5c.com.cn/api/send/?", bTemp); ////如连接超时,可能是您服务器不支持域名解析,请将下面连接中的:【m.5c.com.cn】修改为IP:【115.28.23.78】
//输出result内容,查看返回值,成功为success,错误为error,详见该文档起始注释
Console.WriteLine(result);
}
//POST方式发送得结果
private static String PostRequest(string url, byte[] bData)
{
System.Net.HttpWebRequest hwRequest;
System.Net.HttpWebResponse hwResponse;
string strResult = string.Empty;
try
{
//获取上面的URL链接
hwRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
//设置超时时间
hwRequest.Timeout = 5000;
//发送请求为POST
hwRequest.Method = "POST";
hwRequest.ContentType = "application/x-www-form-urlencoded";
//bData的长度(也就是获取用户名,密码,手机号,apikey,短信内容,编码格式总长度)
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来获取数据
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;
}
//URLencode编码
public static string UrlEncode(string str)
{
StringBuilder sb = new StringBuilder();
byte[] byStr = System.Text.Encoding.UTF8.GetBytes(str); //默认是System.Text.Encoding.Default.GetBytes(str)
for (int i = 0; i < byStr.Length; i++)
{
sb.Append(@"%" + Convert.ToString(byStr[i], 16));
}
return (sb.ToString());
}
private static void WriteErrLog(string strErr)
{
Console.WriteLine(strErr);
System.Diagnostics.Trace.WriteLine(strErr);
}
}
}