发送短信(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; }
}
}
}