关于使用ASP.NET短信接口开发

项目摘要:

该项目是一个基于C#的Web应用程序,主要用于通过HTTP POST请求发送短信验证码。代码中定义了一个send方法,该方法通过调用第三方短信API(https://api.sms.cn/sms/)发送短信。

发送的内容包括接收号码、短信内容以及经过MD5加密的用户密码。

doPostRequest方法负责执行POST请求,并处理请求的响应。

如果请求过程中出现异常,错误信息会被记录到日志中。

该项目的核心功能是通过Web服务实现短信发送,适用于需要短信验证码的场景。

代码如下:

using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    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.Text;
    using System.Net;
    using System.IO;
    
    public partial class _Default : System.Web.UI.Page
    {
    
    /********************************************************************************************************/
    //数据发送
    /********************************************************************************************************/
    #region 数据发送
    protected void send()
    {
    string sendurl="https://api.sms.cn/sms/";
    string mobile = "15900008888"; //发送号码
    string strContent = "您好,您的验证码是111【云信使】";
    StringBuilder sbTemp = new StringBuilder();
    string uid="test";
    string pwd="test123456";
    string Pass = FormsAuthentication.HashPasswordForStoringInConfigFile(pwd+uid, "MD5"); //密码进行MD5加密
    //POST 传值
    sbTemp.Append("ac=send&uid="+uid+"&pwd=" + Pass + "&mobile=" + mobile + "&content=" + strContent);
    byte[] bTemp = System.Text.Encoding.GetEncoding("GBK").GetBytes(sbTemp.ToString());
    String postReturn = doPostRequest(sendurl, 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
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值