发送短信(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; }
}
}
}
发送短信(string转换为JSON)的更多相关文章
- java + maven 实现发送短信验证码功能
如何使用java + maven的项目环境发送短信验证码,本文使用的是榛子云短信 的接口. 1. 安装sdk 下载地址: http://smsow.zhenzikj.com/doc/sdk.html ...
- java发送短信验证码
业务: 手机端点击发送验证码,请求发送到java服务器端,由java调用第三方平台(我们使用的是榛子云短信http://smsow.zhenzikj.com)的短信接口,生成验证码并发送. SDK下载 ...
- android发送短信验证码并自动获取验证码填充文本框
android注册发送短信验证码并自动获取短信,截取数字验证码填充文本框. 一.接入短信平台 首先需要选择短信平台接入,这里使用的是榛子云短信平台(http://smsow.zhenzikj.com) ...
- java攻城师之路(Android篇)--搭建开发环境、拨打电话、发送短信、布局例子
一.搭建开发环境 1.所需资源 JDK6以上 Eclipse3.6以上 SDK17, 2.3.3 ADT17 2.安装注意事项 不要使用中文路径 如果模拟器默认路径包含中文, 可以设置android_ ...
- Android 打开URL中的网页和拨打电话、发送短信功能
拨打电话需要的权限 <uses-permission android:name="android.permission.CALL_PHONE"/> 为了省事界面都写一起 ...
- 移动MAS短信平台发送短信
MAS短信平台发送短信分为两种方式 参考文档下载 一.sdk调用 using mas.ecloud.sdkclient; using System; namespace 短信发送 { class Pr ...
- android: 接收和发送短信
8.2 接收和发送短信 收发短信应该是每个手机最基本的功能之一了,即使是许多年前的老手机也都会具备这 项功能,而 Android 作为出色的智能手机操作系统,自然也少不了在这方面的支持.每个 A ...
- 阿里大鱼.net core 发送短信
阿里大鱼还未提供 .net core 版SDK,但提供了相关API,下面是.net core版实现,只是简单发送短信功能: using System; using System.Collections ...
- 短信接口调用以及ajax发送短信接口实现以及前端样式
我们短信api用的是云信使平台提供的非免费短信服务:官网提供的demo有两种,分别是function加其调用.class文件加其调用. 在这里我们用class文件加调用: 首先,ThinkPHP里面自 ...
- 如何使用微信小程序云函数发送短信验证码
其实微信小程序前端和云端都是可以调用短信平台接口发送短信的,使用云端云函数的好处是无需配置域名,也没有个数限制. 本文使用的是榛子云短信平台(http://smsow.zhenzikj.com) ,S ...
随机推荐
- mysq常见问题
1.Reading table information for completion of table and column names You can turn off this feature t ...
- pads
1安装和破解,这个网上很多资料,破解的时候比较麻烦一点,注意安装环境. 2无模命令 (pads特点就是快捷键操作) 参考http://www.cnblogs.com/asus119/archive/2 ...
- CI框架源码分析
这几天,把ci源码又看了一遍,于是有了新的收获.明白了在application目录下core文件夹的作用,就是用来写ci核心文件的扩展的, 而且需要在配置文件中添加类前缀MY_. CI框架整体是但入口 ...
- IP的包头格式什么?请分析每个字段的含义
Version:版本号 Header Length:IP包头长度 Type of service:服务类型 Total Length:IP包总长 Identifier:标识符 Flags:标记 Fra ...
- 如何让LinearLayout也有类似Button的点击效果?
有的时候,我们希望LinearLayout布局也有点击的效果,这时候我们不仅需要一个作为背景的selector,还要设置一些其它属性才行: android:clickable="true&q ...
- kallisto:Near-optimal RNA-Seq quantification
Near-optimal RNA-Seq quantification https://pachterlab.github.io/kallisto 文章标题: Pseudoalignment fo ...
- HDU 2277 Change the ball
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2277 Change the ball Time Limit: 2000/1000 MS (Java/O ...
- [debian]SublimeText>PrettyCode無效
怣 apt-get install node http://nodejs.org/#download.
- Word 2013发布博客配置步骤
.打开Word 2013,点击"共享"››"博客": .在打开的窗口中点击"管理帐户": .然后点击"新建": ...
- CF 628B New Skateboard --- 水题
CD 628B 题目大意:给定一个数字(<=3*10^5),判断其能被4整除的连续子串有多少个 解题思路:注意一个整除4的性质: 若bc能被4整除,则a1a2a3a4...anbc也一定能被4整 ...