2014-06-25 57 views
0

XML内容,我发送SOAP请求,看起来像下面这样:发送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内容=> < firstNode> myProduct的</firstNode>,当我发送soapRequest,另一边我得到在调试模式:事件类型= “myEventType” 事件ID = “” EVENTDATA = NULL

我的Web方法看起来像

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

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

<firstNode>myProduct</firstNode> 

EDIT1:

感谢您的重播MarxWright,我已经尝试过这一点,但它不工作。我设法通过将“<”替换为“& lt”来手动转换您的sMessage和“>”与“& gt”;“它有效。但如果我可以在另一边进行更改,那将会很好。

回答

0

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

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

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

我不完全理解你的回答,你能否进一步解释一下。如果消息到达另一端,并且需要进行更改,您是否可以不使用消息的内容再次将它重新转换为Xml文档? – MarxWright