欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#代码实现短信验证码接口示例

程序员文章站 2022-05-03 16:32:52
本文实例为大家分享了c#实现短信验证码接口示例,供大家参考,具体内容如下 using system; using system.collections.gen...

本文实例为大家分享了c#实现短信验证码接口示例,供大家参考,具体内容如下

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 = getmd5(pwd1)
      string content = "【阅信】验证码888888,打死也不能告诉别人哦。";
      string url = "http://183.203.28.226: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","13381272353");
      parameters.add("subid","");
      parameters.add("mttime", mttime); 
      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 getmd5(string mystring) 
    {
      md5 md5 = new md5cryptoserviceprovider();
      // byte[] fromdata = system.web.httputility.urlencode.getbytes(mystring);
      byte[] fromdata = encoding.default.getbytes(mystring);
      byte[] targetdata = md5.computehash(fromdata);
      string byte2string = null;
      for(int i=0;i<targetdata.length;i++) 
      {
        byte2string+= targetdata[i].tostring("x");
      }
      return byte2string;
    } 
      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; 
    } 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。