0

Getting HttpResponse Message contentLength as:

var contentLength = response.Content.Headers.ContentLength;
Console.WriteLine(contentLength);

Getting XML File Length as:

FileInfo f = new FileInfo(fileName);
long filesize = f.Length;

Now when printing both the size, it's giving me one as 3970 and other as 3968. I want to compare both the size but because of this difference, I am failing to do it. Any Help?

artplastika
  • 1,972
  • 2
  • 19
  • 38
ayushi
  • 1
  • 2
  • I would say it could be the Byte Order Mark however that's 3 bytes not 2. – Lloyd Sep 22 '17 at 08:36
  • Have you tried to do an (automated) byte-to-byte comparison of response and file contents to see where the difference is? – Sefe Sep 22 '17 at 08:47
  • @Lloyd The BOM is 2 bytes in UTF-16, 4 in UTF-32, and in 3 bytes (broken) UTF-8. Broken, because UTF-8 does not need BOM (and in fact, it is/was forbidden by the Unicode Standard), but some editors got it wrong and now it's a defacto (broken) standard – king_nak Sep 22 '17 at 08:55
  • @king_nak Ahh interesting I always thought it was 3, well could be that then. – Lloyd Sep 22 '17 at 09:31
  • Well, then one of the lengths must be wrong. – Sefe Sep 22 '17 at 11:46

1 Answers1

0

Check the first 2 bytes of the stream vs the file. UTF-16 has a 2 byte BOM, it's probably that:

http://www.opentag.com/xfaq_enc.htm#enc_default

First bytes        Encoding assumed
-----------------------------------------
EF BB BF           UTF-8
FE FF              UTF-16 (big-endian)
FF FE              UTF-16 (little-endian)
00 00 FE FF        UTF-32 (big-endian)
FF FE 00 00        UTF-32 (little-endian)
None of the above  UTF-8
CamW
  • 3,223
  • 24
  • 34
  • It's a UTF-8 encoding. Is there any way to avoid contentLength and use something else because ContentLength is definition from Headers and thus response is taking as Header+content (extra 2 bytes) – ayushi Sep 22 '17 at 11:08
  • I think there's some confusion here, You're saying that ContentLength is Headers+Content. That's not true, it's defined as: "The Content-Length entity-header field indicates the size of the entity-body" It's just the size of the body of the HTTP request. Might be best if you can post the first few (5) bytes of the file and response stream as a hex string and all of the response headers for the HTTP. – CamW Sep 22 '17 at 11:28
  • File Hex content 3c3f786d6c2076657273696f6e3d22312e302220656e636f64696e673d227574662d3822207374616e64616c6f6e653d22796573223f3ea3c6665656420786d6c3a626173653d22687474703a2f2f6465762d73656c6563742d73657276696365732e79746e696f702e636f6d2f70726f6475637472656769737472792f7365617263682f2220786d6c6e733a643d22687474703 – ayushi Sep 22 '17 at 12:00
  • Response Headers: Cache-Control:no-cache Content-Length:3970 Content-Type:application/atom+xml;charset=utf-8 DataServiceVersion:1.0; Date:Fri, 22 Sep 2017 12:06:06 GMT Server:Microsoft-IIS/8.5 X-AspNet-Version:4.0.30319 X-Powered-By:ASP.NET – ayushi Sep 22 '17 at 12:06
  • Ok, so you will need to read the HTTP stream, check it for a BOM and remove the BOM if it exists then compare the resulting size with the file size. – CamW Sep 22 '17 at 12:36