0

When I execute a SOAP request, I get the response Content is not allowed in prolog.

So far I have tried:

  • adding a declaration (with different parameters)
  • changing the encoding
  • removing any characters in prolog (there aren't any)
  • changing the namespace order
  • and more I cannot probably remember

The XML is valid and well formed, so I have no idea, where my mistake is.

Here is my code:

        // Set the request text.
        string text = @"<ns2:Envelope xmlns:ns2=""http://www.w3.org/2003/05/soap-envelope"">
                <ns2:Header>
                ...
                </ns2:Header>
                <ns2:Body>
                ...
                </ns2:Body>
            </ns2:Envelope>";

        // Create the XDocument object.
        var reqDocument = new XDocument(new XDeclaration("1.0", "UTF-8", "yes"), XElement.Parse(text));

        // Create the web request.
        var request = (HttpWebRequest)WebRequest.Create(@"https://example.com");
        request.Method = "POST";

        // Send the web request.
        using (Stream stream = request.GetRequestStream())
            reqDocument.Save(stream);

        // Get the response.
        string response = null;
        using (WebResponse ws = request.GetResponse())
            using (var rd = new StreamReader(ws.GetResponseStream()))
                response = rd.ReadToEnd();

I also tried to include the header in the request. Same result.

Here is another (anonymous) example:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:Envelope xmlns="http://example.com" xmlns:ns2="http://www.w3.org/2003/05/soap-envelope" xmlns:ns3="http://example.com">
  <ns2:Header>
    <Auth>
      <UserName>user</UserName>
    </Auth>
  </ns2:Header>
  <ns2:Body>
    <ns3:Customer>
      <CustomerCode>22222222</CustomerCode>
    </ns3:Customer>
  </ns2:Body>
</ns2:Envelope>
Luke Wage
  • 693
  • 4
  • 13
  • Make sure the XML declaration is first character of the xml. Can you post the xml string so we can verify the xml doesn't contain errors. – jdweng Nov 12 '19 at 18:16
  • See edits above. I checked with a validation tool that my XML is correct and well formatted. – Luke Wage Nov 12 '19 at 18:33
  • You may have some invalid html characters in the innertext of the xml. See : https://stackoverflow.com/questions/51500738/content-is-not-allowed-in-prolog-error-yet-nothing-before-xml-declaration – jdweng Nov 12 '19 at 18:40
  • Thanks for the suggestion, but I also checked that. No hidden / invalid characters to be found. – Luke Wage Nov 13 '19 at 06:51

0 Answers0