I assume somehow you can user an XmlReader to read the envelope from an XmlDocument. Does anyone have a good example how? I want to simply extract the entire sent SOAP text into a string variable so we can log it in case of a request error.
Example methods:
protected virtual HttpWebRequest CreateWebRequest(string endPoint, Int32 contentLength)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_requestURI);
request.Method = "POST";
request.ContentLength = contentLength;
request.ContentType = "text/xml;charset=UTF-8";
return request;
}
public Response.Response SendRequest()
{
XmlDocument doc = CreateRequestXML();
// Determins if API call needs to use a session based URI
string requestURI = UseAuthURI ? _requestURIAuthBased + sID : _requestURI;
byte[] data = XmlUtil.DocumentToBytes(doc);
// Create the atual Request instance
HttpWebRequest request = CreateWebRequest(requestURI, data.Length);
request.ContentLength = data.Length;
request.KeepAlive = false;
request.Timeout = 30000;
// Send the Request
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(data, 0, data.Length);
}
// get response
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
if (response.StatusCode != HttpStatusCode.OK)
{
string message = String.Format("POST failed. Received HTTP {0}", response.StatusCode);
throw new Exception(message);
}
using (Stream responseStream = response.GetResponseStream())
{
using (XmlReader reader = XmlReader.Create(responseStream))
{
return CreateResponse(reader);
}
}
}
}