通过Soap发送xml内容

时间:2014-06-25 12:35:10

标签: c# web-services wcf soap asmx

我发送的SOAP请求如下所示:

WebRequest webRequest = WebRequest.Create("http://localhost:55056/myWebService.asmx");
HttpWebRequest httpRequest = (HttpWebRequest)webRequest;
httpRequest.Method = "POST";
httpRequest.ContentType = "text/xml; charset=utf-8";
httpRequest.Accept = "text/xml";
httpRequest.Headers.Add("SOAPAction: http://tempuri.org/myWebMethod");
httpRequest.ProtocolVersion = HttpVersion.Version11;
httpRequest.Credentials = CredentialCache.DefaultCredentials;
Stream requestStream = httpRequest.GetRequestStream();
//Create Stream and Complete Request             
StreamWriter streamWriter = new StreamWriter(requestStream, Encoding.ASCII);
soapRequest.Append("<soap:Body>");
soapRequest.Append("<myWebMethod xmlns=\"http://tempuri.org/\">");
soapRequest.Append("<eventType>myEventType</eventType>");
soapRequest.Append("<eventId><firstNode>myProduct</firstNode></eventId>");
soapRequest.Append("<eventData>myEventData</eventData>");
soapRequest.Append("</myWebMethod>");
soapRequest.Append("</soap:Body>");
streamWriter.Write(soapRequest.ToString());
streamWriter.Close();
//Get the Response    
HttpWebResponse wr = (HttpWebResponse)httpRequest.GetResponse();
StreamReader srd = new StreamReader(wr.GetResponseStream());
string resulXmlFromWebService = srd.ReadToEnd();

我的问题是eventId包含一些xml内容=&gt; &lt; firstNode&gt; myProduct&lt; / firstNode&gt;,当我发送 soapRequest 时,另一方面我处于调试模式:eventType =“myEventType”eventId =“”eventData = null

我的网络方法看起来像

        [WebMethod]
        public void myWebMethod(string eventType, string eventId, string eventData)
        {
        }

我能做些什么让我的webmethod接受这个小的“xml”:

<firstNode>myProduct</firstNode>

EDIT1:

感谢你重播马克思赖特,我试过这个,但它不起作用。我设法通过替换:“&lt;”手动转换你的sMessage用“&amp; lt;”和“&gt;”用“&amp; gt;”它很有效。但如果我能在另一方面做出改变,那就太好了。

1 个答案:

答案 0 :(得分:0)

我不是100%确定这会有效,但您可以尝试使用以下函数将转发消息进行发送,我相信您可能会在另一端读取数据。

String sMessage = "<tag>Data</tag>";

String messageFilling = System.Security.SecurityElement.Escape(sMessage);