电信SMS短信SOAP发送格式,C#手工组成.

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("<SOAP-ENV:Envelope  xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" ");
 10             str.Append(" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" ");
 11             str.Append(" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
 12             str.Append(" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"");
 13             str.Append(" xmlns:ns4=\"http://www.chinatelecom.com.cn/schema/ctcc/sms/v2_1\" ");
 14             str.Append(" xmlns:ns3=\"http://www.chinatelecom.com.cn/schema/ctcc/common/v2_1\" ");
 15             str.Append(" xmlns:ns6=\"http://www.chinatelecom.com.cn/wsdl/ctcc/sms/send/v2_1/service\"");
 16             str.Append(" xmlns:ns1=\"http://www.chinatelecom.com.cn/schema/ctcc/sms/send/v2_1/local\"></SOAP-ENV:Envelope>");
 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         /// <summary>
145         /// 根据SP密码生成提交短信密码
146         /// </summary>
147         /// <param name="spPwd"></param>
148         /// <returns></returns>
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         /// <summary>
159         /// 转换为16进制.
160         /// </summary>
161         /// <param name="s"></param>
162         /// <returns></returns>
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             else
185             {
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的开发人员使用.