创蓝语音服务(语音通知验证码).net

  public static string PostUrl = "http://zapi.253.com/msg/HttpBatchSendSM";
        static void Main(string[] args)
        {
            string account = "";
            string password = "";
            string mobile = "1";
            string content = "您的登陆验证码是1234";

            string postStrTpl = "account={0}&pswd={1}&mobile={2}&msg={3}&needstatus=true&product=&extno=";

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

            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(PostUrl);
            myRequest.Method = "POST";
            myRequest.ContentType = "application/x-www-form-urlencoded";
            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);
                myResponse.Close();
                myRequest.Abort();
                Console.WriteLine("发送成功");
                Console.ReadKey();
            }
            else
            {
                myRequest.Abort();
                myResponse.Close();
                Console.WriteLine("发送失败");
                Console.ReadKey();
            }
        }

创蓝语音服务.net(返回错误的情况)

  public static string PostUrl = "http://zapi.253.com/msg/HttpBatchSendSM";
        static void Main(string[] args)
        {
            string account = "V0240532111";
            string password = "pE3e8r60wM01c5111";
            string mobile = "18516627499";
            string content = "您的登陆验证码是1234";
            string postStrTpl = "account={0}&pswd={1}&mobile={2}&msg={3}&needstatus=true&product=&extno=";

            UTF8Encoding encoding = new UTF8Encoding();
            byte[] postData = encoding.GetBytes(string.Format(postStrTpl, account, password, mobile, content));
            string resultCode = "";
            HttpWebRequest myRequest = null;
            HttpWebResponse myResponse = null;
            try
            {
                myRequest = (HttpWebRequest)WebRequest.Create(PostUrl);
                myRequest.Method = "POST";
                myRequest.ContentType = "application/x-www-form-urlencoded";
                myRequest.ContentLength = postData.Length;
                Stream newStream = myRequest.GetRequestStream();
                // Send the data.
                newStream.Write(postData, 0, postData.Length);
                newStream.Flush();
                newStream.Close();

                myResponse = (HttpWebResponse)myRequest.GetResponse();
        
                if (myResponse.StatusCode == HttpStatusCode.OK)
                {
                    StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
                    string result = reader.ReadToEnd();

                    if (result != null)
                    {
                        string[] arr = result.Split(',');
                        resultCode = arr[1];
                    }
                }
            }catch(Exception ex)
            {
                Console.Write(ex);
            }
            finally
            {
                if (myRequest != null)
                {
                    myRequest.Abort();
                }
                if (myResponse != null)
                {
                    myResponse.Close();
                }
            }
            if (resultCode.StartsWith("0"))
            {
                Console.WriteLine("发送成功");
                Console.ReadKey();
            }
            else
            {
                Console.WriteLine("发送失败" + resultCode);
                Console.ReadKey();
            }
        }

 

posted @ 2018-09-06 11:55  代码沉思者  阅读(648)  评论(0编辑  收藏  举报