Last post Feb 19, 2015 01:36 PM by mattshiao
Feb 16, 2015 07:49 PM|mattshiao|LINK
I was asked to find a way to get the raw header and body of a SOAP Response. Usually when I call SOAP based web service, I generate a proxy class by using svcutil, then I include the proxy in my project and code against the proxy, the response I get is automatically
translated into objects, I don't have a chance to get the raw body of the response. I found a way to get the raw header by using System.ServiceModel.Web.WebOperationContext.Current.IncomingResponse.Headers property, that solves half my problems, but IncomingResponse
class does not have access to body.
Feb 19, 2015 08:35 AM|Shawn - MSFT|LINK
These two methods, BeforeSendReply() and AfterReceiveRequest() methods,pre-process and post-process the SOAP XML packets, allowing us to intercept and peek at the raw content. We can then output
the resulting XML, in its entirety, to the Visual Studio Console window, a text file, or any other output method.
Note in the above code, we first make a copy of the MessageBuffer processed by the WCF service. The MessageBuffer contains the SOAP XML, including the header and body of the request or response. However, the body XML content of the SOAP message
is further encoded. It's also important to note that if we were to access the original MessageBuffer's body content (without first making a copy), the body would be marked as processed and would no longer continue through the web service. Due to this, we first
copy the MessageBuffer via CreateBufferedCopy() and then access the message. The message will contain a placeholder text of "... stream ..." where the encoded body text is placed. To access the body of the SOAP XML, we call GetReaderAtBodyContents() and then
read the outer XML, replacing it back into our output string. We now have the complete SOAP XML request and response for displaying.
For more information, you could refer to:
Feb 19, 2015 01:36 PM|mattshiao|LINK
Thank you for the answer, I read through the articles you listed, seems to me they are server side solutions. Unfortunately I am calling web service developed by other company and have no controls over server side.