0

I'm reading the content from a page using DownloadString from the WebClient class and then writing the contents of that to a static HTML file using the StreamWriter class. On the page that I'm reading in, there's an inline javascript method that just sets an anchor element's OnClick attribute to set the window.location = history.go(-1); I'm finding when I view the static HTML page, there's an odd looking letter showing up that isn't present on the dynamic web page.

WebClient & SteamWriter Code

    using (var client = new WebClient())
        {
            var html = client.DownloadString(url);

            //This constructor prepares a StreamWriter (UTF-8) to write to the specified file or will create it if it doesn't already exist
            using (var stream = new StreamWriter(file, false, Encoding.UTF8))
            {
                stream.Write(html);
                stream.Close();
            }
        }

The dynamic page's HTML snippet in question

<span>Sorry, but something went wrong on our end. &nbsp;Click <a href="#" onclick="window.location.href = history.go(-1);">here</a> to go back to the previous page.</span>

The static page's HTML snippet

<span>Sorry, but something went wrong on our end. Â&nbsp;Click <a href="#" onclick="window.location.href = history.go(-1);">here</a> to go back to the previous page.</span>

I was thinking that adding the Encoding.UTF8 parameter would solve this issue but it didn't seem to help. Is there some sort of extra encoding or decoding that I need to do? Or did I completely miss something else that's needed for this type of operation?

user3267755
  • 1,010
  • 1
  • 13
  • 32
  • perhaps this is what you're seeing http://stackoverflow.com/questions/1461907/html-encoding-issues-%C3%82-character-showing-up-instead-of-nbsp – Marshall Tigerus May 16 '16 at 20:42

1 Answers1

0

I updated the WebClient to encode in UTF8 as it converts the resource into a string, seems to have taken care of the issue.

            using (var client = new WebClient())
            {
                client.Encoding = System.Text.Encoding.UTF8;
                var html = client.DownloadString(url);

                //This constructor prepares a StreamWriter (UTF-8) to write to the specified file or will create it if it doesn't already exist
                using (var stream = new StreamWriter(file, false, Encoding.UTF8))
                {
                    stream.Write(html);
                    stream.Close();
                }
            }
user3267755
  • 1,010
  • 1
  • 13
  • 32