C# 106 短信发送

最近遇到短信验证码发送老是失败,咨询客服,经过反复折腾,是我这边的源码有问题(同事写的,同事写的,同事写的),他重新发了我一份源码,然后成功了!代码如下

using System;
using System.Text;
using System.Net;
using System.IO;

namespace Commons
{
    public class Sms106
    {
        private static string _user = "";
        private static string _pwd = "";

        public static string sendSms(string mobile, string content)
        {
            //发送验证码
            string postStrTpl = "account={0}&password={1}&mobile={2}&content={3}";

            UTF8Encoding encoding = new UTF8Encoding();
            byte[] postData = encoding.GetBytes(string.Format(postStrTpl, _user, _pwd, mobile, content));

            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://sms.106jiekou.com/utf8/openapi.aspx");
            myRequest.Method = "POST";
            myRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
            myRequest.ContentLength = postData.Length;

            Stream newStream = myRequest.GetRequestStream();
            // Send the data.
            newStream.Write(postData, 0, postData.Length);
            newStream.Flush();
            newStream.Close();
            HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
            if (myResponse.StatusCode == HttpStatusCode.OK)
            {
                StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
                string strResult = reader.ReadToEnd();  //100为成功
                //反序列化upfileMmsMsg.Text
                //实现自己的逻辑

                return strResult;
            }

            return "";
        }
    }
}

官网地址:http://www.106jiekou.com/

个人觉得他们的客服超级给力,值得安利!

posted on 2020-05-27 09:25  不朽阁主  阅读(144)  评论(0编辑  收藏  举报

导航