您的位置:首页 > Web前端 > HTML

C#一般处理程序+html实现短信发送

2017-10-11 16:43 471 查看
下载.net sdk中把四个dll文件拿出来放到项目中引用之后

后台代码

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using Aliyun.Acs.Core.Profile;

using Aliyun.Acs.Core;

using Aliyun.Acs.Dysmsapi.Model.V20170525;

using Aliyun.Acs.Core.Exceptions;

namespace WebApplication1.net

{

    /// <summary>

    /// Handler2 的摘要说明

    /// </summary>

    public class Handler2 : IHttpHandler

    {

        public void ProcessRequest(HttpContext context)

        {

            context.Response.ContentType = "text/html";

            if (context.Request["command"] != null)

            {

                string command = context.Request["command"].ToString();

                System.Reflection.MethodInfo method = this.GetType().GetMethod(command);//搜索指定名称的方法

                if (method != null)

                {

                    method.Invoke(this, new object[] { context });

                }

            }

        }

        public void SendMessage(HttpContext context)

        {

            String product = "Dysmsapi";//短信API产品名称

            String domain = "dysmsapi.aliyuncs.com";//短信API产品域名

            String accessKeyId = "";//这里写你的accessKeyId

            String accessKeySecret = "";//你的accessKeySecret

            string NameValue = context.Request ["NameValue"];

            string TimeValue = context.Request ["TimeValue"];

            IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", accessKeyId, accessKeySecret);

            //IAcsClient client = new DefaultAcsClient(profile);

            // SingleSendSmsRequest request = new SingleSendSmsRequest();

            DefaultProfile.AddEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);

            IAcsClient acsClient = new DefaultAcsClient(profile);

            SendSmsRequest request = new SendSmsRequest();

            try

            {

                //必填:待发送手机号。支持以逗号分隔的形式进行批量调用,批量上限为20个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式

                request.PhoneNumbers = context.Request ["PhoneNumber"];

                //必填:短信签名-可在短信控制台中找到

                request.SignName = "";//这里要写上你的签名

                //必填:短信模板-可在短信控制台中找到

                request.TemplateCode = "";//这里要写上你的模板code

                //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为

                request.TemplateParam = "{\"name\":\"" + NameValue + "\",\"time\":\"" + TimeValue + "\"}";//这里是你在设置模板是预留的参数

                //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者

                request.OutId = "21212121211";//这里随便写

                //请求失败这里会抛ClientException异常

                SendSmsResponse sendSmsResponse = acsClient.GetAcsResponse(request);

                //System.Console.WriteLine(sendSmsResponse.Message);

                con
4000
text.Response.Write(sendSmsResponse.Message);

            }

            catch (ServerException e)

            {

                System.Console.WriteLine("Hello World!");

            }

            catch (ClientException e)

            {

                System.Console.WriteLine("Hello World!");

            }

        }

        public bool IsReusable

        {

            get

            {

                return false;

            }

        }

    }
}

下面是html代码

  <title></title>

    <script src="/lib/jquery/jquery-1.11.3.js" type="text/javascript"></script>

   <script type="text/javascript" >

       function SendMessage() {

           var PhoneNumber = $("#PhoneNumber").val();

           var NameValue = $("#NameValue").val();

           var TimeValue = $("#TimeValue").val();

           if (PhoneNumber==""||NameValue==""||TimeValue=="") {

               alert("请全部填写完成!");

               return;

           }

           $.get("/net/Handler2.ashx", { command:"SendMessage",PhoneNumber:PhoneNumber,NameValue:NameValue,TimeValue:TimeValue,Random:Math.random()}, function () {

           });

       }

   

   </script>

</head>

<body>

<table style="width:400px;margin:0 auto;text-align:center;border:1px solid Gray;">

    <tr style="height:50px;">

        <td>手机号码:</td>

        <td>

            <input type="text" id="PhoneNumber"/>

        </td>

    </tr>

      <tr style="height:50px;">

        <td>姓名:</td>

        <td>

            <input type="text" id="NameValue"/>

        </td>

    </tr>

      <tr style="height:50px;">

        <td>时间:</td>

        <td>

            <input type="text" id="TimeValue"/>

        </td>

    </tr>    

    <tr style="height:50px;">

        <td colspan="2" style="text-align:center;">

            <input type="button" value="发送" onclick="SendMessage()" />

        </td>       

    </tr>

</table>

</body>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: