locked
404 error while trying to load XML file from Request.InputStream - Trying to pull XML node value RRS feed

  • Question

  • Hi,

    Im trying to select a node list from a xml file with no success.

    Here is the code I'm working with:

    C#

    private void m_application_EndRequest( object sender, EventArgs e )
            {
                HttpApplication app = (HttpApplication) sender;
                HttpContext cnt = app.Context;
                int duration = Environment.TickCount - (int) cnt.Items["TickCount"];
                int requestLength = cnt.Request.TotalBytes;
                TimeSpan tickspan = TimeSpan.FromMilliseconds(duration);
                string timespan = tickspan.Minutes.ToString() + ":" + tickspan.Seconds.ToString() + "." + tickspan.Milliseconds.ToString();
                string userID = "Null";
    
                string path = cnt.Request.Headers["SOAPAction"];
                if ((path != null) && (path != string.Empty))
                {
                    path = path.Replace("\"", string.Empty);
    
                    XmlDocument inputstream = new XmlDocument();
    
                    using (Stream receiveStream = cnt.Request.InputStream)
                    {
                        receiveStream.Position = 0;
    
                        using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8))
                        {
                            inputstream.Load(readStream);
                        }
                    }
    
                    XmlNamespaceManager nsmanager = new XmlNamespaceManager(inputstream.NameTable);
                    nsmanager.AddNamespace("soap", "http://www.w3.org/2003/05/soap-envelope");
                    nsmanager.AddNamespace("CoPilotHeader", "http://copilot.na.fire.com/copilotfire");
                    nsmanager.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
                    nsmanager.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
                    nsmanager.AddNamespace("df", "http://tempuri.org/");
                    XmlNodeList xnlist = inputstream.DocumentElement.SelectNodes("/soap:Envelope/soap:Header/df:CoPilotHeader", nsmanager);
    
                    userID = xnlist[0]["CmaToken"].InnerText;
    
                }
                else
                {
                    try
                    {
                        path = cnt.Request.Url.AbsoluteUri;
                    }
                    catch (NullReferenceException)
                    {
                        path = string.Empty;
                    }
    
                    userID = "Null";
                }
    
                SrvLogging.LogPerformance( path, duration, cnt.Timestamp, -1,
                    cnt.Request.ContentLength, Environment.MachineName, cnt.Request.HttpMethod,
                    cnt.Response.Status, timespan, userID);
            }

    Here is the SOAP/XML:

    POST /FireServices/route.asmx HTTP/1.1
    Host: cras.na.fire.com
    Content-Type: text/xml; charset=utf-8
    Content-Length: length
    SOAPAction: "http://copilot.na.fire.com/copilotfire/GetRoute"
    
    <?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
      <soap:Header>
        <CoPilotHeader xmlns="http://copilot.na.fire.com/copilotfire/">
          <CmaToken>string</CmaToken>
          <ErrorString>string</ErrorString>
          <RecordCount>int</RecordCount>
        </CoPilotHeader>
      </soap:Header>
      <soap:Body>
        <GetRoute xmlns="http://copilot.na.fire.com/copilotfire/">
          <routeToken>string</routeToken>
          <routeNo>string</routeNo>
        </GetRoute>
      </soap:Body>
    </soap:Envelope>

    Thoughts? I'm getting a 404 error when trying to load the xml file with the cnt.Request.InputStream

    The ultimate goal is to pull the CmaToken out into the UserID string.

    Monday, February 10, 2014 4:08 PM

Answers

  • Hello,

    >>I'm getting a 404 error when trying to load the xml file with the cnt.Request.InputStream

    The 404 error is usually caused by that you requested source could not be found. So please make sure that the file path is correct. And in the code I notice it replaces “\” with empty. Maybe this has broken the file path.

    >>The ultimate goal is to pull the CmaToken out into the UserID string.

    Change below three line:

    nsmanager.AddNamespace("soap", "http://www.w3.org/2003/05/soap-envelope");
    
    nsmanager.AddNamespace("CoPilotHeader", "http://copilot.na.fire.com/copilotfire");
    
    XmlNodeList xnlist = inputstream.DocumentElement.SelectNodes("/soap:Envelope/soap:Header/df:CoPilotHeader", nsmanager);
    

    To be:

    nsmanager.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
    
    nsmanager.AddNamespace("CoPilotHeader", "http://copilot.na.fire.com/copilotfire/");
    
    XmlNodeList xnlist = inputstream.DocumentElement.SelectNodes("/soap:Envelope/soap:Header/CoPilotHeader:CoPilotHeader", nsmanager);
    

    My test result:

    If I misunderstand, please let me know.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by John_G316 Thursday, February 13, 2014 3:04 PM
    Thursday, February 13, 2014 2:29 AM
    Moderator

All replies

  • Hello,

    >>I'm getting a 404 error when trying to load the xml file with the cnt.Request.InputStream

    The 404 error is usually caused by that you requested source could not be found. So please make sure that the file path is correct. And in the code I notice it replaces “\” with empty. Maybe this has broken the file path.

    >>The ultimate goal is to pull the CmaToken out into the UserID string.

    Change below three line:

    nsmanager.AddNamespace("soap", "http://www.w3.org/2003/05/soap-envelope");
    
    nsmanager.AddNamespace("CoPilotHeader", "http://copilot.na.fire.com/copilotfire");
    
    XmlNodeList xnlist = inputstream.DocumentElement.SelectNodes("/soap:Envelope/soap:Header/df:CoPilotHeader", nsmanager);
    

    To be:

    nsmanager.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
    
    nsmanager.AddNamespace("CoPilotHeader", "http://copilot.na.fire.com/copilotfire/");
    
    XmlNodeList xnlist = inputstream.DocumentElement.SelectNodes("/soap:Envelope/soap:Header/CoPilotHeader:CoPilotHeader", nsmanager);
    

    My test result:

    If I misunderstand, please let me know.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by John_G316 Thursday, February 13, 2014 3:04 PM
    Thursday, February 13, 2014 2:29 AM
    Moderator
  • Well that got me past the 404 error. But for some reason I'm getting Null for the CmaToken out of the XML. Either that or what's getting passed into the request is Null. I've got some more investigation to do. Do you know of a way to check the Request.InputStream or pass it into a file so I can see what is coming in?
    Thursday, February 13, 2014 3:04 PM
  • Hello,

    >> Do you know of a way to check the Request.InputStream or pass it into a file so I can see what is coming in?

    The XmlDocument instance has a property InnerXml. After loading the stream into XmlDocument, using it as:

    string value = inputstream.InnerXml;

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, February 14, 2014 7:40 AM
    Moderator