0

When downloading an XML response from a REST API, I cannot get .NET to download the full XML document on many requests. In each case, I'm missing the last several characters of the XML file which means I can't parse it. The requests work fine in a browser.

I have tried WebResponse.GetResponseStream() using a StreamReader. Within the StreamReader I have tried Read(...) with a buffer, ReadLine(), and ReadToEnd() to build a string for the response. Wondering if there was a bug in my code, I also tried WebClient.DownloadString(url) with the same result and XmlDocument.Load(url) which just throws an exception (unexpected end of file while parsing ____).

I know for a fact that this API has had some encoding issues in the past, so I've tried specifying multiple different encodings (e.g., UTF-8, iso-8859-1) for the StreamReader as well as letting .NET detect the encoding. Changing the encoding seems to result in a different number of characters that get left off the end.

Is there any way I can detect the proper encoding myself? How does a browser do it? Is there somewhere in any browser to see the actual encoding the response is using (not what the HTTP headers say it's returning)? Any other methods of getting a string response from a web site with an unknown encoding?

StreamReader sample code

StringBuilder sb = new StringBuilder();
using (resp = (HttpWebResponse)req.GetResponse())
{
    using (Stream stream = resp.GetResponseStream())
    {
        using (StreamReader sr = new StreamReader(stream))
        {
            int charsRead = 1;
            char[] buffer = new char[4096];
    
            while (charsRead > 0)
            {
                charsRead = sr.Read(buffer, 0, buffer.Length);
                sb.Append(buffer, 0, charsRead);
            }
        }
    }
}

WebClient sample code

WebClient wc = new WebClient();
string text = wc.DownloadString(url);

XmlDocument sample code

XmlDocument doc = new XmlDocument();
doc.Load(url)
Rick
  • 1,863
  • 2
  • 19
  • 46
  • 1
    I suggest you read the response as a `byte[]` (not as a string) and then write the bytes to a file. Open the file with a text editor that lets you switch encoding (e.g. Notepad++) and see which one works best. – John Wu Sep 09 '22 at 18:31
  • Please [edit] your question to provide a [mcve]. – JosefZ Sep 09 '22 at 18:31
  • Could you provide the actual code used? I kinda suspect a missing flush or Dispose is at fault here – X39 Sep 09 '22 at 21:05
  • Code added. I feel like if it was a flushing issue, then I wouldn't see the same issue with WebClient or XmlDocument. @X39 – Rick Sep 12 '22 at 13:10

0 Answers0