通过Soap发送xml内容

本文关键字:内容 xml 发送 Soap 通过 | 更新日期: 2023-09-27 18:10:09

我正在发送一个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内容=> myProduct ,当我发送soapRequest时,另一方面我进入调试模式:eventType="myEventType" eventId=" eventData=null

我的web方法看起来像

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

我怎么做才能让我的webmethod接受这个小的"xml":

<firstNode>myProduct</firstNode>

Edit1:

谢谢你的重播马克思赖特,我试过这个,但它不起作用。我设法通过将"<"替换为"&"来手动转换您的消息。Lt;" and ">" with ">"它工作。但如果我能在另一边做出改变就好了。

通过Soap发送xml内容

我不是100%确定这将工作,但你可以尝试使用以下函数将转换消息发送,我相信你可能能够读取数据在另一端。

String sMessage = "<tag>Data</tag>";
String messageFilling = System.Security.SecurityElement.Escape(sMessage);