发送短信(string转换为JSON)

  using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Demo1113
{
    public sealed class Program
    {
        public static void Main()
        {
            SmsMessage smsMessage = new SmsMessage();
            smsMessage.OpenId = "123";
            smsMessage.TempleaseId = "abc";
            smsMessage.MessageInfo = new List<MessageInfo> { new MessageInfo{
            Key="1",Value="q"
            },new MessageInfo{Key="2",Value="w"}};
            //Newtonsoft.Json
            string json = JsonConvert.SerializeObject(smsMessage);

            SmsMessageDictionary smsMessageDictionary = new Program.SmsMessageDictionary();
            smsMessageDictionary.OpenId = "123";
            smsMessageDictionary.TempleaseId = "abc";

            Dictionary<string, string> list = new Dictionary<string, string>();
            list.Add("d", "1");
            list.Add("d1", "13");
            list.Add("d2", "14");
            smsMessageDictionary.MessageInfo = list;
            Console.WriteLine("Json=" + json);
            Console.WriteLine("Json2=" + JsonConvert.SerializeObject(smsMessageDictionary));
            Console.ReadKey();

 

           string sJson = JsonConvert.SerializeObject(smsMessage); //传入参数有误时,可能会出现"远程服务器返回错误: (500) 内部服务器错误。"的错误
            System.Text.UTF8Encoding encodeing = new UTF8Encoding();
            byte[] contentBytes = encodeing.GetBytes(sJson);
            try
            {
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri("http://。。。。。。。。。"));
                //增加下面两个属性即可  (防止出现“基础连接已经关闭: 接收时发生错误。”的错误)
                webRequest.KeepAlive = false;
                webRequest.ProtocolVersion = HttpVersion.Version10;
                //webRequest.Proxy = new WebProxy("url", 80);
                //webRequest.UserAgent = "Mozilla-Firefox-Spider(Wenanry)";
                webRequest.Method = "POST";
                //webRequest.ContentType = "application/json";
                webRequest.ContentType = "application/json";
                webRequest.Accept = "application/json";
                webRequest.ContentLength = sJson.Length;  //这边是JSON的长度
                using (Stream newStream = webRequest.GetRequestStream())
                {
                    newStream.Write(contentBytes, 0, contentBytes.Length);   //这边读取的是字节
                }
                //ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
                //HttpWebResponse res;
                //try
                //{
                //    res = (HttpWebResponse)webRequest.GetResponse();
                //}
                //catch (WebException ex)
                //{
                //    res = (HttpWebResponse)ex.Response;
                //}
                using (var response = (HttpWebResponse)webRequest.GetResponse())
                {
                    var stream = response.GetResponseStream();
                    using (var sr = new StreamReader(stream, Encoding.Default))
                    {
                        str = sr.ReadToEnd();
                    }
                    stream.Dispose();
                }
               // JObject jo = JObject.Parse(str);
              //  string err = jo["errCode"].ToString();

 

                JObject jopdd = JObject.Parse(str);
                string err = jopdd["errorCode"].ToString();
                string errMsg = jopdd["errorMessage"].ToString();
            }
            catch (Exception e)
            {
                string aa = "aaa";
            }
        }


        public class SmsMessageDictionary
        {
            public string OpenId { get; set; }
            public string TempleaseId { get; set; }
            public Dictionary<string, string> MessageInfo { get; set; }
        }
        public class SmsMessage
        {
            public string OpenId { get; set; }
            public string TempleaseId { get; set; }
            public List<MessageInfo> MessageInfo { get; set; }
        }

        public class MessageInfo
        {
            public string Key { get; set; }
            public string Value { get; set; }
        }
    }
}

转载于:https://www.cnblogs.com/TJessica/p/6128528.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值