Last post Mar 05, 2008 09:43 AM by justsnafu
Mar 04, 2008 03:07 PM|justsnafu|LINK
Say I invoke a WebRequest progammatically and grab the raw stream from the response, like this:
WebResponse webResponse = webRequest.GetResponse();
Stream stream = webResponse.GetResponseStream();
Is there a way to introspect that stream to determine what encoding was used to encode it? There is no property on the stream object itself that allows me to determine this, and once you do this:
StreamReader sr = new StreamReader(stream);
you've defaulted it to UTF-8 encoding so that's useless. Any help would be greatly appreciated.
Mar 04, 2008 03:39 PM|justsnafu|LINK
duh, I'm an idiot, I missed this overload:
StreamReader reader = new StreamReader(stream, true);
this isnt perfect because you're still screwed if you don't have a BOM but it should work for most cases.
Mar 05, 2008 09:43 AM|justsnafu|LINK
OK so I'm back to square one...as I noted the overload works fine if your stream starts with a BOM which it often will if you load it from a file on the file system. However XML returned from a web request is likely not going to contain the BOM so all this
overload will do is default it to UTF-8.
Anyone with any other ideas?