C#调用服务商 短信接口 例子

xiaoxiao2021-02-28  150

public bool SendSmsTest(string mobile, string content, out string msg) { msg = ""; int code = 10000; string url = "短信接口Url?"; //参数见服务商的文档 这里的参数仅供参考 url += "username=aaa" + "&tkey=aaaaa"; url += "&password="+password; url += "&mobile=" + mobile; url += "&content=" + content; bool result = false; try { string text = HttpPost(url, "", "get"); code = Convert.ToInt32(text.Split(',')[0]); switch (code) { case -1: msg = "发送失败"; break; case 1: result = true; msg = "发送成功"; break; case 11: msg = "产品错误(联系客服)"; break; case 12: msg = "产品禁用(联系客服)"; break; case 13: msg = "手机号码错误,不支持的号段"; break; case 15: msg = "签名不合规"; break; case 16: msg = "签名屏蔽"; break; case 17: msg = "签名分配扩展失败"; break; case 18: msg = "短信内容不能为空"; break; case 19: msg = "短信内容最大1000个字"; break; case 20: msg = "预付费用户条数不足"; break; case 21: msg = "发送内容存在黑词"; break; case 22: msg = "通道错误(联系客服)"; break; case 28: msg = "签名最长15字"; break; case 29: msg = "小号错误"; break; case 98: msg = "异常(联系客服)"; break; case 99: msg = "DES解密Exception"; break; } } catch (Exception ex) { msg = ex.Message; } return result; } private static string HttpPost(string url, string body, string method, string contentType = "charset=utf-8") { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); //设置请求消息头信息 request.Method = method; request.ContentType = contentType; request.Timeout = 30 * 1000; //获取回应数据 接收方开始接受数据 using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { using (Stream responseStream = response.GetResponseStream()) { using (StreamReader reader = new StreamReader(responseStream, Encoding.UTF8)) { return reader.ReadToEnd(); } } } }

然后其他地方就可以调用啦

protected void Button1_Click(object sender, EventArgs e) //弄个按钮试一试 { Class1 prov = new Class1(); string smsmsg; bool f = prov.SendSmsTest("手机号", "内容【签名】", out smsmsg); }

转载请注明原文地址: https://www.6miu.com/read-35230.html

最新回复(0)