public IRequest Marshall(PublishMessageRequest publicRequest) { MemoryStream stream = new MemoryStream(); XmlTextWriter writer = new XmlTextWriter(stream, Encoding.UTF8); writer.WriteStartDocument(); writer.WriteStartElement(MNSConstants.XML_ROOT_MESSAGE, MNSConstants.MNS_XML_NAMESPACE); if (publicRequest.IsSetMessageBody()) { writer.WriteElementString(MNSConstants.XML_ELEMENT_MESSAGE_BODY, publicRequest.MessageBody); } if (publicRequest.IsSetMessageTag()) { writer.WriteElementString(MNSConstants.XML_ELEMENT_MESSAGE_TAG, publicRequest.MessageTag); } if (publicRequest.IsSetMessageAttributes()) { MessageAttributes messageAttributes = publicRequest.MessageAttributes; writer.WriteStartElement(MNSConstants.XML_ELEMENT_MESSAGE_ATTRIBUTES); if (messageAttributes.IsSetMailAttributes()) { MailAttributes mailAttributes = messageAttributes.MailAttributes; writer.WriteElementString(MNSConstants.XML_ELEMENT_DIRECT_MAIL, mailAttributes.ToJson()); } if (messageAttributes.IsSetSmsAttributes()) { SmsAttributes smsAttributes = messageAttributes.SmsAttributes; writer.WriteElementString(MNSConstants.XML_ELEMENT_DIRECT_SMS, smsAttributes.ToJson()); } if (messageAttributes.IsSetBatchSmsAttributes()) { BatchSmsAttributes batchSmsAttributes = messageAttributes.BatchSmsAttributes; writer.WriteElementString(MNSConstants.XML_ELEMENT_DIRECT_SMS, batchSmsAttributes.ToJson()); } writer.WriteEndElement(); } writer.WriteEndElement(); writer.WriteEndDocument(); writer.Flush(); stream.Seek(0, SeekOrigin.Begin); IRequest request = new DefaultRequest(publicRequest, MNSConstants.MNS_SERVICE_NAME); request.HttpMethod = HttpMethod.POST.ToString(); request.ContentStream = stream; request.ResourcePath = MNSConstants.MNS_TOPIC_PRE_RESOURCE + publicRequest.TopicName + MNSConstants.MNS_MESSAGE_SUB_RESOURCE; return(request); }
public void ToJsonTest() { SmsAttributes smsAttributes = new SmsAttributes(); smsAttributes.FreeSignName = "陈舟锋"; smsAttributes.TemplateCode = "SMS_15535414"; Dictionary <string, string> param = new Dictionary <string, string>(); param.Add("name", "CSharpSingle"); smsAttributes.Receiver = "13735576932"; smsAttributes.SmsParams = param; Assert.AreEqual("{\"FreeSignName\":\"陈舟锋\",\"Receiver\":\"13735576932\",\"SmsParams\":\"{\\\"name\\\": \\\"CSharpSingle\\\"}\",\"TemplateCode\":\"SMS_15535414\"}", smsAttributes.ToJson()); }