V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
jiaowobaba
V2EX  ›  C

C#短信接口 DEMO

  •  
  •   jiaowobaba · 2017-04-18 14:48:32 +08:00 · 2263 次点击
    这是一个创建于 2778 天前的主题,其中的信息可能已经有所发展或是发生改变。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net.Security;
    using System.Security.Cryptography.X509Certificates;
    using System.Net;
    using System.IO;
    using System.IO.Compression;
    using System.Text.RegularExpressions; using System.Security.Cryptography; using System.Web; public class Test { private static readonly string DefaultUserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
    private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
    {
    return true; //总是接受
    }
    static void Main(string[] args)
    {
    DateTime dt = DateTime.Now; string mttime = dt.ToString("yyyyMMddHHmmss"); string pwd1 = "密码"+mttime; string pwd = md5(pwd1); Console.WriteLine(pwd.Length); string content = " [阅信] 验证码 888888 ,请注意保密,以防泄露!"; //string content = "123456"; 语音验证码内容 string url = " http://60.205.14.180:9000/HttpSmsMt";
    Encoding encoding = Encoding.GetEncoding("utf-8");
    IDictionary<string, string> parameters = new Dictionary<string, string>();
    parameters.Add("name", "用户名");
    parameters.Add("pwd", pwd);
    parameters.Add("content",content); parameters.Add("phone","13000000000"); parameters.Add("subid",""); parameters.Add("mttime", mttime);
    parameters.Add("rpttype", "1");
    HttpWebResponse response = CreatePostHttpResponse(url,parameters,encoding);
    //打印返回值
    Stream stream = response.GetResponseStream(); //获取响应的字符串流
    StreamReader sr = new StreamReader(stream); //创建一个 stream 读取流
    string html = sr.ReadToEnd(); //从头读到尾,放到字符串 html
    Console.WriteLine(html);
    } public static String md5(String s) { MD5 md5 = new MD5CryptoServiceProvider(); byte[] bytes = System.Text.Encoding.UTF8.GetBytes(s); bytes = md5.ComputeHash(bytes); md5.Clear();

            string ret = "";
            for (int i = 0; i < bytes.Length; i++)
            {
                ret += Convert.ToString(bytes[i], 16).PadLeft(2, '0');
            }
    
            return ret.PadLeft(32, '0');
        }
     public static HttpWebResponse CreatePostHttpResponse(string url, IDictionary<string, string> parameters,Encoding charset)  
        {  
            HttpWebRequest request = null;  
            //HTTPSQ 请求  
            ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);  
            request = WebRequest.Create(url) as HttpWebRequest;  
            request.ProtocolVersion = HttpVersion.Version10;  
            request.Method = "POST";  
            request.ContentType = "application/x-www-form-urlencoded";  
            request.UserAgent = DefaultUserAgent;  
            //如果需要 POST 数据     
            if (!(parameters == null || parameters.Count == 0))  
            {  
                StringBuilder buffer = new StringBuilder();  
                int i = 0;  
                foreach (string key in parameters.Keys)  
                {  
                    if (i > 0)  
                    {  
                       buffer.AppendFormat("&{0}={1}",key,parameters[key]);  
                    }  
                    else  
                    {  
                        buffer.AppendFormat("{0}={1}",key, parameters[key]);  
                    }  
                    i++;  
                }  
                byte[] data = charset.GetBytes(buffer.ToString());  
                using (Stream stream = request.GetRequestStream())  
                {  
                    stream.Write(data, 0, data.Length);  
                }  
            }  
            return request.GetResponse() as HttpWebResponse;  
        } 
    

    }

    更多语言的 DEMO 可以去阅信短信平台找他们要,测试了下还不错。 http://www.jinloushiji.cn

    8 条回复    2017-04-19 14:58:14 +08:00
    Septembers
        1
    Septembers  
       2017-04-18 14:56:26 +08:00
    请发布时考虑读者感受
    jayong
        2
    jayong  
       2017-04-18 15:44:28 +08:00
    同上
    keller
        3
    keller  
       2017-04-18 16:52:26 +08:00
    同上
    imherer
        4
    imherer  
       2017-04-18 17:07:41 +08:00
    这明显是广告啊
    atnopc
        5
    atnopc  
       2017-04-18 17:45:03 +08:00
    写得不错,我选择阿里大鱼
    debye
        6
    debye  
       2017-04-19 08:36:29 +08:00
    @imherer
    同意,这个是广告
    Fx8m
        7
    Fx8m  
       2017-04-19 08:44:53 +08:00
    真硬啊
    sipangzifu
        8
    sipangzifu  
       2017-04-19 14:58:14 +08:00
    好干的货
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3351 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 12:02 · PVG 20:02 · LAX 04:02 · JFK 07:02
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.