I’m using the following method to convert a response stream to a string. The stream contains JavaScript.
public string GetFileAsString()
{
using (WebResponse response = this.GetWebResponse())
{
Stream responseStream = response.GetResponseStream();
if (responseStream != null)
{
// Pipe the stream to a stream reader with the required
// encoding format.
using (StreamReader reader = new StreamReader(
responseStream,
Encoding.UTF8))
{
return reader.ReadToEnd();
}
}
return string.Empty;
}
When I try to test the string for a valid terminator the result is always false e.g.
// Check for a valid ; terminator.
bool hasTerminator = script.EndsWith(";", StringComparison.Ordinal);
if (!string.IsNullOrWhiteSpace(script) && !hasTerminator)
{
script += ";";
}
return script;
It looks to me like I require a smarter check but I’m not sure what is wrong. Any ideas?
Your code actually works for me in a console app. inputting string as “test” and “test;” produces false and true respectively. However “test; ” doesnt work, you might want to trimend() to remove whitespace: