码迷,mamicode.com
首页 > 移动开发 > 详细

.NET使用HttpWebRequest发送手机验证码

时间:2016-12-16 16:43:37      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:http   tty   使用   blog   stat   ***   byte   encode   namespace   

namespace SendSMS
{
    class Program
    {
        static void Main(string[] args)
        {

            string phone = "13770504xxxx";
            string sign = string.Format("【】您的验证码为{0},此验证码有效时间为10分钟", "1234");
            bool result = SendMsg(phone,sign);


        }



        public static bool SendMsg(string phone, string content)
        {
            var smsUrl = "*****";
            var account = "****";
            var pwd = "****";
            var extno = "****";


    
            var result = HttpPost(smsUrl, "&account=" + account + "&password=" + pwd + "&mobile=" + phone + "&content=" + HttpUtility.UrlEncode(content, Encoding.UTF8) + "&extno=" + extno);
            if (result.IndexOf("OK") > -1)
            {
                return true;
            }
            return false;
        }


        public static string HttpPost(string Url, string postDataStr)
        {
            byte[] bs = Encoding.UTF8.GetBytes(postDataStr);

            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(Url);
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            req.ContentLength = bs.Length;
            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(bs, 0, bs.Length);
            }
            using (WebResponse wr = req.GetResponse())
            {
                StreamReader sr = new StreamReader(wr.GetResponseStream(), System.Text.Encoding.UTF8);
                string result = sr.ReadToEnd();
                return result;
            }
        }
    }
}

 

.NET使用HttpWebRequest发送手机验证码

标签:http   tty   使用   blog   stat   ***   byte   encode   namespace   

原文地址:http://www.cnblogs.com/qzxj/p/6187418.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!