博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
电信SMS短信SOAP发送格式(C#手工组成.)
阅读量:7226 次
发布时间:2019-06-29

本文共 9435 字,大约阅读时间需要 31 分钟。

C#手动XML组成,电信SMS短信SOAP发送格式.
SOAP,XML格式
1 [WebMethod]         2         public string SendSmsService(string tel, string msg,string productId)  3         {  4   5             System.Text.StringBuilder str = new System.Text.StringBuilder();  6             string timeStamp = DateTime.Now.ToString("MMddHHmmss");  7   8             ////--soap xmlns;  9             str.Append("
"); 17 18 19 XmlDocument doc = new XmlDocument(); 20 doc.LoadXml(str.ToString()); 21 //XmlDeclaration decl = doc.CreateXmlDeclaration("1.0", "utf-8", null); 22 //doc.InsertBefore(decl, doc.DocumentElement); 23 24 #region requestSOAPHeader 25 XmlElement soapHeader = doc.CreateElement("Header", "http://schemas.xmlsoap.org/soap/envelope/"); 26 soapHeader.Prefix = "SOAP-ENV"; 27 28 XmlElement requestSOAPHeader = doc.CreateElement("RequestSOAPHeader","http://www.chinatelecom.com.cn/schema/ctcc/common/v2_1"); 29 requestSOAPHeader.Prefix="ns3"; 30 31 XmlElement requestSOAPHeaderChild = doc.CreateElement("spId"); 32 requestSOAPHeaderChild.InnerText = ObjectToSoapXml(Field.spId); 33 requestSOAPHeader.AppendChild(requestSOAPHeaderChild); 34 35 requestSOAPHeaderChild = doc.CreateElement("spPassword"); 36 requestSOAPHeaderChild.InnerText = ObjectToSoapXml(this.GeneratePassword(Field.spId, Field.SoapHeaderKey, timeStamp)); 37 requestSOAPHeader.AppendChild(requestSOAPHeaderChild); 38 39 requestSOAPHeaderChild = doc.CreateElement("timeStamp"); 40 requestSOAPHeaderChild.InnerText = ObjectToSoapXml(timeStamp); 41 requestSOAPHeader.AppendChild(requestSOAPHeaderChild); 42 43 requestSOAPHeaderChild = doc.CreateElement("productId"); 44 requestSOAPHeaderChild.InnerText = ObjectToSoapXml(productId); 45 requestSOAPHeader.AppendChild(requestSOAPHeaderChild); 46 47 requestSOAPHeaderChild = doc.CreateElement("SAN"); 48 requestSOAPHeader.AppendChild(requestSOAPHeaderChild); 49 50 requestSOAPHeaderChild = doc.CreateElement("transactionId"); 51 requestSOAPHeader.AppendChild(requestSOAPHeaderChild); 52 53 requestSOAPHeaderChild = doc.CreateElement("transEnd"); 54 requestSOAPHeaderChild.InnerText = ObjectToSoapXml("0"); 55 requestSOAPHeader.AppendChild(requestSOAPHeaderChild); 56 57 requestSOAPHeaderChild = doc.CreateElement("linkId"); 58 requestSOAPHeader.AppendChild(requestSOAPHeaderChild); 59 60 requestSOAPHeaderChild = doc.CreateElement("OA"); 61 requestSOAPHeaderChild.InnerText = ObjectToSoapXml("tel:+86" + tel); 62 requestSOAPHeader.AppendChild(requestSOAPHeaderChild); 63 64 requestSOAPHeaderChild = doc.CreateElement("FA"); 65 requestSOAPHeader.AppendChild(requestSOAPHeaderChild); 66 67 requestSOAPHeaderChild = doc.CreateElement("multicastMessaging"); 68 requestSOAPHeaderChild.InnerText = ObjectToSoapXml("false"); 69 requestSOAPHeader.AppendChild(requestSOAPHeaderChild); 70 71 soapHeader.AppendChild(requestSOAPHeader); 72 doc.DocumentElement.AppendChild(soapHeader); 73 74 #endregion requestSOAPHeader 75 76 XmlElement soapBody = doc.CreateElement("Body", "http://schemas.xmlsoap.org/soap/envelope/"); 77 soapBody.Prefix = "SOAP-ENV"; 78 79 XmlElement soapBodySendSms = doc.CreateElement("sendSms", "http://www.chinatelecom.com.cn/schema/ctcc/sms/send/v2_1/local"); 80 soapBody.InnerXml=""; 81 soapBodySendSms.Prefix = "ns1"; 82 83 XmlElement soapBodyChild = doc.CreateElement("addresses","http://www.chinatelecom.com.cn/schema/ctcc/sms/send/v2_1/local"); 84 soapBodyChild.Prefix = "ns1"; 85 soapBodyChild.InnerText = ObjectToSoapXml("tel:+86" + tel); 86 soapBodySendSms.AppendChild(soapBodyChild); 87 88 soapBodyChild = doc.CreateElement("senderName","http://www.chinatelecom.com.cn/schema/ctcc/sms/send/v2_1/local"); 89 soapBodyChild.Prefix = "ns1"; 90 soapBodyChild.InnerText = ObjectToSoapXml(Field.SAN); 91 soapBodySendSms.AppendChild(soapBodyChild); 92 93 XmlElement soapBodyCharging = doc.CreateElement("charging","http://www.chinatelecom.com.cn/schema/ctcc/sms/send/v2_1/local"); 94 soapBodyCharging.Prefix = "ns1"; 95 96 soapBodyChild = doc.CreateElement("description"); 97 soapBodyCharging.AppendChild(soapBodyChild); 98 99 soapBodyChild = doc.CreateElement("currency");100 soapBodyCharging.AppendChild(soapBodyChild);101 102 soapBodyChild = doc.CreateElement("amount");103 soapBodyChild.InnerText = ObjectToSoapXml("0");104 soapBodyCharging.AppendChild(soapBodyChild);105 106 soapBodyChild = doc.CreateElement("code");107 soapBodyCharging.AppendChild(soapBodyChild);108 109 soapBodySendSms.AppendChild(soapBodyCharging);110 111 soapBodyChild = doc.CreateElement("message","http://www.chinatelecom.com.cn/schema/ctcc/sms/send/v2_1/local");112 soapBodyChild.Prefix = "ns1";113 soapBodyChild.InnerText = ObjectToSoapXml(msg);114 soapBodySendSms.AppendChild(soapBodyChild); 115 116 117 118 XmlElement receiptRequest = doc.CreateElement("receiptRequest","http://www.chinatelecom.com.cn/schema/ctcc/sms/send/v2_1/local");119 receiptRequest.Prefix = "ns1";120 121 soapBodyChild = doc.CreateElement("endpoint");122 soapBodyChild.InnerText = ObjectToSoapXml("tel:+86" + tel);123 receiptRequest.AppendChild(soapBodyChild);124 125 soapBodyChild = doc.CreateElement("interfaceName");126 soapBodyChild.InnerText = ObjectToSoapXml("sendSms");127 receiptRequest.AppendChild(soapBodyChild);128 129 soapBodyChild = doc.CreateElement("correlator");130 soapBodyChild.InnerText = ObjectToSoapXml(Guid.NewGuid().ToString().ToUpper());131 receiptRequest.AppendChild(soapBodyChild);132 soapBodySendSms.AppendChild(receiptRequest);133 134 soapBody.AppendChild(soapBodySendSms);135 doc.DocumentElement.AppendChild(soapBody); 136 137 138 139 140 return Common.PostHelper.PostSOAPData("http://xx.xx.xx.xx:8081/SendSmsService", doc); 141 142 }143 144 /// 145 /// 根据SP密码生成提交短信密码146 /// 147 /// 148 ///
149 public string GeneratePassword(string spId, string spPwd, string timeStamp)150 {151 System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();152 153 string spPassword = Hex(md5.ComputeHash(System.Text.Encoding.ASCII.GetBytes(spId + spPwd + timeStamp)));154 spPassword = spPassword.ToUpper();155 return spPassword;156 }157 158 /// 159 /// 转换为16进制.160 /// 161 /// 162 ///
163 private static string Hex(byte[] s) //164 {165 string ret = "";166 for (int i = 0; i < 16; i++)167 {168 ret += s[i].ToString("x2");169 }170 return ret;171 }172 173 private static string ObjectToSoapXml(object o)174 {175 XmlSerializer mySerializer = new XmlSerializer(o.GetType());176 MemoryStream ms = new MemoryStream();177 mySerializer.Serialize(ms, o);178 XmlDocument doc = new XmlDocument();179 doc.LoadXml(Encoding.UTF8.GetString(ms.ToArray()));180 if (doc.DocumentElement != null)181 {182 return doc.DocumentElement.InnerXml;183 }184 else185 {186 return o.ToString();187 }188 }

利用Post 传递:

PostSOAPData方法
1 public static string PostSOAPData(string p_strUrl, XmlDocument doc) 2         { 3             Encoding dataEncode; 4             dataEncode = System.Text.Encoding.UTF8;            5  6  7             byte[] byteArray = dataEncode.GetBytes(doc.OuterXml); //转化 8             HttpWebRequest http = (HttpWebRequest)WebRequest.Create(p_strUrl); 9 10             http.Method = "POST";11             http.ContentType = "text/xml;charset=utf-8";           12             http.ContentLength = byteArray.Length;13            14             Stream newStream = http.GetRequestStream();15             newStream.Write(byteArray, 0, byteArray.Length);//写入参数16             newStream.Close();17 18             HttpWebResponse response = (HttpWebResponse)http.GetResponse();19             StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);20             string strRec = sr.ReadToEnd();21             sr.Close();22             response.Close();23             return strRec;24         }

终于搞定.哈哈...

供有做sp的开发人员使用.

 

转载于:https://www.cnblogs.com/xmyy/archive/2012/12/21/2827350.html

你可能感兴趣的文章
Databricks Scala 编程风格指南
查看>>
Tkinter,label内容随多选框变化
查看>>
PHP开发中的数据类型 ( 第3篇 ) :Heaps
查看>>
网络七层协议
查看>>
4种删除Word空白页的小技巧,都是你需要用到的!
查看>>
单服务器MySQL主从复制实践
查看>>
CentOS 7 root口令恢复
查看>>
| 刘知远:让计算机听懂人话
查看>>
苹果收购初创公司Tueo Health,哮喘监测或将应用到Apple Watch
查看>>
CLR存储过程
查看>>
初级运维(一)
查看>>
C语言字符串常用函数学习(一)
查看>>
Lync Server 2010部署与应用(三)---拓扑生成与发布
查看>>
安全摘记1:关于安全与黑客
查看>>
我的友情链接
查看>>
tbox中vector容器的使用
查看>>
一个简单的PHP笔试题
查看>>
firebug重新载入页面获取源码
查看>>
我的友情链接
查看>>
5月末周中国.COM总量净增1.2万个 美国净减2.6万个
查看>>