0

I appreciate this is a little bit niche, but I thought I would ask anyway. I'm writing a small c# application to utilise the HMRC web portal and electronically submit VAT returns in XML format. According to the HMRC specification it is just a simple Http 1.1 POST action required, and retrieving the response in XML. The application is built, however, I am having trouble with this code. I get an "OK" HttpWebResponse, but the HMRC server returns this spurious error message which they can't seem to tell me what it means. Here is my code:

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(destinationUrl);
            byte[] bytes;
            bytes = System.Text.Encoding.ASCII.GetBytes(requestXml);
            request.ContentType = "text/xml; charset='utf-8'";

            request.ContentLength = bytes.Length;
            request.Method = "POST";
            Stream requestStream = request.GetRequestStream();
            requestStream.Write(bytes, 0, bytes.Length);
            requestStream.Close();
            HttpWebResponse response;
            response = (HttpWebResponse)request.GetResponse();

            if (response.StatusCode == HttpStatusCode.OK)
            {
                Stream responseStream = response.GetResponseStream();
                string responseStr = new StreamReader(responseStream).ReadToEnd();
                return responseStr;
            }

And the error is:

1001 - The submitted XML document either failed to validate against the GovTalk schema for this class of document or its body was badly formed.

I know the XML is ok, because when I test it using a third-party tool like "Postman" it submits 100% and the Transaction Engine returns no errors, so it must be my code. Does anything look glaringly wrong to post an XML? I have tried different Content/MIME Types and also I have confirmed that 'utf-8' is the correct encoding.

I was just wondering if any developers out there had worked on the Transaction Engine and could share their submit/post code?

Davy C
  • 639
  • 5
  • 16
  • Have you tried System.Text.Encoding.UTF8.GetBytes? – ajg Sep 07 '17 at 14:47
  • also try request.ContentType = "text/xml; encoding=utf-8"; – ajg Sep 07 '17 at 14:48
  • Hello "alg". I have tried System.Text.Encoding.UTF8.GetBytes, yes, but possibly not tried taking the single quotes out from 'utf-8'. I will try that now. Many thanks for your input. Dave – Davy C Sep 07 '17 at 15:00
  • No, same error I'm afraid. – Davy C Sep 07 '17 at 15:02
  • notice 'encoding' as opposed to 'charset' too – ajg Sep 07 '17 at 15:02
  • Yes, I spotted that. One thing I did notice..... If I trap the code, I can see that the Bytes read from my XML (ContentLength property) is just 113, and the response I get back is 1024, so much bigger, yet my XML has far more data in it than the XML I get back (at least double). – Davy C Sep 07 '17 at 15:29
  • 1
    Ah, I think I might have figured it out. That code is encoding the string content which is the filename itself, not the contents of the file! – Davy C Sep 07 '17 at 15:54

1 Answers1

0

The answer is that the code converting the file contents into the Byte Array was actually converting the file name.

I changed this line:

bytes = System.Text.Encoding.ASCII.GetBytes(requestXml);

To this:

bytes = File.ReadAllBytes(requestXml);
Davy C
  • 639
  • 5
  • 16