用户名:   密码:    忘记密码  注册
我推信息网 > 学习园地 > 正文

Asp.Net中使用www.mb345.com 的服务发送 群发短信

2011年5月26日
信息简介:Asp.Net中使用www.mb345.com 的服务发送 群发短信,Request方法的代码

Asp.Net中使用www.mb345.com 的服务发送 群发短信

(使用Request方法代码)

使用Request方法代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Web;

namespace Project.WebClass
{
    /// <summary>
    /// 使用http://mb345.com 的服务发送短信
    /// </summary>
    public class mb345_SMS
    {
        // 您的企业号:******* 特服号:******* 密码*******
        // 密码请到官网申请

        /// <summary>
        /// CorpID-帐号
        /// </summary>
        protected static string smsID = "*******";
        /// <summary>
        /// 密码
        /// </summary>
        protected static string smsPwd = "*******";
        /// <summary>
        /// 特服号.没用到
        /// </summary>
        protected static string smsName = "*******";
        /// <summary>
        /// 服务所在网址
        /// </summary>
        protected static string URLHead = "http://mb345.com:999/WS";

        /// <summary>
        /// 编码方式
        /// </summary>
        protected static Encoding Enc = Encoding.GetEncoding("gb2312");


        /// <summary>
        /// 向目标电话发送一条短信息,禁止10小时以内向同一手机号发送相同短信
        /// </summary>
        /// <param name="Mobile">电话号码</param>
        /// <param name="Content">信息内容</param>
        /// <returns>返回信息:String</returns>
        public static string smsSend(string Mobile, string Content)
        {
            //Public int Send(string CorpID,string Pwd,string Mobile,string Content,string Cell,string SendTime)
            //http://mb345.com:999/WS/Send.aspx?CorpID=*&Pwd=*&Mobile=*&Content=*&Cell=*&SendTime=*

            string PageUrl = string.Format("{0}/Send.aspx?",URLHead);
            string Param = string.Format("CorpID={0}&Pwd={1}&Mobile={2}&Content={3}&Cell=&SendTime=", smsID, smsPwd, Mobile,HttpUtility.UrlEncode(Content,Enc));

            string RequestStr = WebRequestFun(PageUrl,Param);
            string Retext="";

            //输出参数:整数,0,发送成功;-1、帐号未注册;-2、其他错误;-3、密码错误;-4、手机号格式不对;-5、余额不足;-6、定时发送时间不是有效的时间格式

            switch (RequestStr)
            {
                case "0":
                    Retext = "发送成功";
                    break;
                case "-1":
                    Retext = "帐号未注册";
                    break;
                case "-3":
                    Retext = "密码错误";
                    break;
                case "-4":
                    Retext = "手机号格式不对";
                    break;
                case "-5":
                    Retext = "余额不足";
                    break;
                case "-6":
                    Retext = "定时发送时间不是有效的时间格式";
                    break;
                default:
                    Retext = "其他错误";
                    break;
            }

            return Retext;
        }

        /// <summary>
        /// (群发)向所有目标电话发送一条短信息,禁止10小时以内向同一手机号发送相同短信
        /// </summary>
        /// <param name="Mobile">电话号码,多个用逗号(,)分隔</param>
        /// <param name="Content">信息内容</param>
        /// <returns>返回信息:String</returns>
        public static string smsSend(string Mobile, string Content, bool Group)
        {
            //public int BatchSend(string CorpID,string Pwd,string Mobile,string Content,string Cell,string SendTime)
            //http://mb345.com:999/WS/BatchSend.aspx?CorpID=*&Pwd=*&Mobile=*&Content=*&Cell=*&SendTime=*

            string PageUrl = string.Format("{0}/BatchSend.aspx?", URLHead);
            string Param = string.Format("CorpID={0}&Pwd={1}&Mobile={2}&Content={3}&Cell=&SendTime=", smsID, smsPwd, HttpUtility.UrlEncode(Mobile, Enc), HttpUtility.UrlEncode(Content, Enc));

            string RequestStr = WebRequestFun(PageUrl, Param);
            string Retext = "";

            //输出参数:整数,0,发送成功进入审核阶段;1、直接发送成功;-1、帐号尚未注册;-2、其他错误;-3、帐号或则密码错误;-4、一次提交信息不能超过600个手机号码;-5、企业号帐户余额不足,请先充值再提交短信息!;-6、定时发送时间不是有效的时间格式;-7、发送短信内容包含黑字典关键字;-8、发送内容需在3到250个字之间;-9、 发送号码为空

            switch (RequestStr)
            {
                case "0":
                    Retext = "发送成功进入审核阶段";
                    break;
                case "1":
                    Retext = "直接发送成功";
                    break;
                case "-1":
                    Retext = "帐号未注册";
                    break;
                case "-3":
                    Retext = "帐号或则密码错误";
                    break;
                case "-4":
                    Retext = "一次提交信息不能超过600个手机号码";
                    break;
                case "-5":
                    Retext = "企业号帐户余额不足,请先充值再提交短信息!";
                    break;
                case "-6":
                    Retext = "定时发送时间不是有效的时间格式";
                    break;
                case "-7":
                    Retext = "发送短信内容包含黑字典关键字";
                    break;
                case "-8":
                    Retext = "发送内容需在3到250个字之间";
                    break;
                case "-9":
                    Retext = "发送号码为空";
                    break;
                default:
                    Retext = "其他错误";
                    break;
            }

            return Retext;
        }

        /// <summary>
        /// 用WebRequest返回指定的页面值
        /// </summary>
        /// <param name="URL">访问页面</param>
        /// <param name="PostBytes">字节参数</param>
        /// <returns>String:值</returns>
        public static string WebRequestFun(string URL,string Param)
        {
            string RequestStr = "";

            byte[] postBytes = Encoding.ASCII.GetBytes(Param);

            WebRequest Request = WebRequest.Create(URL);
            Request.Method = "POST";
            Request.ContentType = "application/x-www-form-urlencoded;charset=gb2312";
            Request.ContentLength = postBytes.Length;
            using (Stream reqStream = Request.GetRequestStream())
            {
                reqStream.Write(postBytes, 0, postBytes.Length);
            }

            WebResponse Response = Request.GetResponse();

            Stream resStream = Response.GetResponseStream();
            StreamReader Sr = new StreamReader(resStream, Enc);
            RequestStr = Sr.ReadToEnd();
            resStream.Close();
            Sr.Close();

            return RequestStr;
        }

        //public static string SendWebServiceSMS(string Mobile, string Content)
        //{
        //    //WebServiceSMS ws=new WebServiceSMS();
        //    //int WSBack=
        //    //http://tempuri.org/
        //    return "";
        //}
    }
}


调用代码

前台:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SMStest.aspx.cs" Inherits="Project.CommonWebApp.SMStest" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
ID:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
Content:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        Group<asp:CheckBox ID="CheckBox1" runat="server" Text="群发" />
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<br />
<br />
结果:<asp:Label ID="Label1" runat="server" Text="未提交"></asp:Label>
    </div>
    </form>
</body>
</html>

后台

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Project.CommonWebApp
{
    public partial class SMStest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.TextBox2.Text ="这里是程序发送的内容!" + DateTime.Now.ToString();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (this.TextBox1.Text != "" && this.TextBox2.Text != "")
            {
                if (this.CheckBox1.Checked)
                {
                    this.Label1.Text = WebClass.mb345_SMS.smsSend(this.TextBox1.Text, this.TextBox2.Text,true);
                }
                else
                {
                    this.Label1.Text = WebClass.mb345_SMS.smsSend(this.TextBox1.Text, this.TextBox2.Text);
                }
            }
        }
    }
}

(阅览次数:5970次)【关闭
上一条: Css的margin,你知道多少?Margin详解
下一条: “代理 XP”选项,SQL2005数据库自动备份
你认为此条信息得分为:
0% 100% 0% 0% 0%
很好 一般 不行
验证字符:
网友昵称: (Ctrl+Enter)
发表评论:
以下评论只代表网友个人观点,不代表我单位观点
暂时为空!
网站首页 -  政策法规 -  联系我们 -  免责申明 -  时尚一族 -  友情链接 -  关于我们 -  设为首页 -  加入收藏
版权所有:我推信息网 关键字:我推信息网 wotui 我推荐 我推
域 名:www.wotui.net 联系QQ (08:30-22:00在线): 邮 箱:yahali@sina.com
中华人民共和国电信与信息服务业务经营许可证

 

 

 

 

公共信息安全网络监察

经营性网站备案信息

不良信息举报中心 中国文明网络传播