title: Encoding issue with Response filter in asp.net
body: <p>I am dealing with some code that uses a custom HttpResponse filter in an asp.net application.</p>
  
  <pre><code>response.Filter = new HtmlResponseFilterStream();
  </code></pre>
  
  <p>This <em>HtmlResponseFilterStream</em> inherits from <em>MemoryStream</em>. The write method has been overriden to get the contents of the HTTP response. I use  <em>Encoding.UTF8.GetString</em> to get the contents of the page in chunks. </p>
  
  <pre><code>public override void Write(byte[] buffer, int offset, int count)
  {
          var contentInBuffer = Encoding.UTF8.GetString(buffer);
  
          ...
   }
  </code></pre>
  
  <p>In my local machine if I go to any page and debug the code I can see the the variable <em>contentInBuffer</em> populated with the actual html contents. Both with local ISS or iss express.
  This also works in our production environment.</p>
  
  <p>However, if I go to a test machine things don't work as expected. If I debug the contents inside the write method (using remote debugger). I get some weird characters instead. So I guess this is due to some different encoding used.. maybe..</p>
  
  <p>Both production and test servers have same specs with same OS and iis server versions. </p>
  
  <p>I checked .Net Globalization settings and the encodings are set to <em>UTF-8</em> for Request, Response Headers and Response in both machines. </p>
  
  <p>I am pretty sure this has to be something related to the environment settings. Not sure if it's related with IIS or windows.</p>
  
  <p>Any idea anyone?</p>