using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
namespace ConsoleApplicationdemo
{
class Program
{
#region MDK提供方Demo
static void Main(string[] args)
{
LoginGo();
}
public static void LoginGo()
{
//Http地址
string ContentType = "application/json";
string Url = "http://www.17int.cn/xxsmsweb/smsapi/send.json";
Dictionary< < string, object> param = new Dictionary< string, object>();
//设定键值对
param.Add("account", "acc"); //SDK用户
string passMD5 = GetMd5Hash("passwd"); //MD5大写加密;
param.Add("password", passMD5); //SDK密码
param.Add("mobile", "135xxxxxxxx"); //手机号
param.Add("content", "【签名】短信发送的内容"); //短信内容
param.Add("requestId", "1"); //客户请求ID
param.Add("extno", ""); //扩展码
//拼接集合中的键值并且以&符号分隔 ,可以用json类
string body = "{" + string.Join(",", param.Select(o => "/"" + o.Key + "/":/"" + o.Value + "/"")) + "}";
try
{
string SentStatus = HttpPost(Url, body, ContentType);
Console.WriteLine("【提交状态】:" + SentStatus);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
public static string GetMd5Hash(String input)
{
if (input == null)
{
return null;
}
MD5 md5Hash = MD5.Create();
byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
StringBuilder sBuilder = new StringBuilder();
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("X2"));
}
return sBuilder.ToString();
}
//HttpPost方法
private static string HttpPost(string Url, string Body, string ContentType)
{
string ResponseContent = "";
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(Url);
httpWebRequest.ContentType = ContentType;
httpWebRequest.Method = "POST";
httpWebRequest.Timeout = 20000; //setInstanceFollowRedirects
httpWebRequest.MediaType = "json";
byte[] btBodys = Encoding.UTF8.GetBytes(Body);
httpWebRequest.ContentLength = btBodys.Length;
httpWebRequest.GetRequestStream().Write(btBodys, 0, btBodys.Length);
try
{
HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream());
ResponseContent = streamReader.ReadToEnd();
httpWebResponse.Close();
streamReader.Close();
httpWebRequest.Abort();
httpWebResponse.Close();
}
catch (Exception ex)
{
return ex.Message;
}
return ResponseContent;
}
#endregion
}
}