public string GetString()
{
if (response == null)
return null;
// If encoding was not set manually try to detect it
if (encoding == null) {
try {
// Try to get the encoding using the characterset
encoding = Encoding.GetEncoding(response.CharacterSet);
}
catch (Exception e) {
// If this fails default to the system's default encoding
logger.DebugException("Encoding could not be determined, using default.", e);
encoding = Encoding.Default;
}
}
// Debug
if (_debug) logger.Debug("GetString: Encoding={2}", encoding.EncodingName);
// Converts the stream to a string
try {
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream, encoding, true);
string data = reader.ReadToEnd();
reader.Close();
stream.Close();
response.Close();
// return the string data
return data;
}
catch (Exception e) {
if (e.GetType() == typeof(ThreadAbortException))
throw e;
// There was an error reading the stream
// todo: might have to retry
logger.ErrorException("Error while trying to read stream data: ", e);
}
// return nothing.
return null;
}