C#代码实现短信验证码接口示例
本文实例为大家分享了C#实现短信验证码接口示例,供大家参考,具体内容如下
usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; usingSystem.Net.Security; usingSystem.Security.Cryptography.X509Certificates; usingSystem.Net; usingSystem.IO; usingSystem.IO.Compression; usingSystem.Text.RegularExpressions; usingSystem.Security.Cryptography; usingSystem.Web; publicclassTest { privatestaticreadonlystringDefaultUserAgent="Mozilla/4.0(compatible;MSIE6.0;WindowsNT5.2;SV1;.NETCLR1.1.4322;.NETCLR2.0.50727)"; privatestaticboolCheckValidationResult(objectsender,X509Certificatecertificate,X509Chainchain,SslPolicyErrorserrors) { returntrue;//总是接受 } staticvoidMain(string[]args) { DateTimedt=DateTime.Now; stringmttime=dt.ToString("yyyyMMddHHmmss"); stringpwd1="*************"+mttime; stringpwd=GetMD5(pwd1) stringcontent="【阅信】验证码888888,打死也不能告诉别人哦。"; stringurl="http://183.203.28.226:9000/HttpSmsMt"; Encodingencoding=Encoding.GetEncoding("utf-8"); IDictionary<string,string>parameters=newDictionary<string,string>(); parameters.Add("name","****"); parameters.Add("pwd",pwd); parameters.Add("content",content); parameters.Add("phone","13381272353"); parameters.Add("subid",""); parameters.Add("mttime",mttime); HttpWebResponseresponse=CreatePostHttpResponse(url,parameters,encoding); //打印返回值 Streamstream=response.GetResponseStream();//获取响应的字符串流 StreamReadersr=newStreamReader(stream);//创建一个stream读取流 stringhtml=sr.ReadToEnd();//从头读到尾,放到字符串html Console.WriteLine(html); } publicstaticstringGetMD5(stringmyString) { MD5md5=newMD5CryptoServiceProvider(); //byte[]fromData=System.Web.HttpUtility.UrlEncode.GetBytes(myString); byte[]fromData=Encoding.Default.GetBytes(myString); byte[]targetData=md5.ComputeHash(fromData); stringbyte2String=null; for(inti=0;i<targetData.Length;i++) { byte2String+=targetData[i].ToString("x"); } returnbyte2String; } publicstaticHttpWebResponseCreatePostHttpResponse(stringurl,IDictionary<string,string>parameters,Encodingcharset) { HttpWebRequestrequest=null; //HTTPSQ请求 ServicePointManager.ServerCertificateValidationCallback=newRemoteCertificateValidationCallback(CheckValidationResult); request=WebRequest.Create(url)asHttpWebRequest; request.ProtocolVersion=HttpVersion.Version10; request.Method="POST"; request.ContentType="application/x-www-form-urlencoded"; request.UserAgent=DefaultUserAgent; //如果需要POST数据 if(!(parameters==null||parameters.Count==0)) { StringBuilderbuffer=newStringBuilder(); inti=0; foreach(stringkeyinparameters.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(Streamstream=request.GetRequestStream()) { stream.Write(data,0,data.Length); } } returnrequest.GetResponse()asHttpWebResponse; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。