using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.IO; using log4net; namespace Pithos.Network { public static class WebExtensions { public static string ReadToEnd(this HttpWebResponse response) { using (var stream = response.GetResponseStream()) { if (stream == null) return null; using (var reader = new StreamReader(stream)) { var body = reader.ReadToEnd(); return body; } } } public static void LogError(this ILog log,HttpWebResponse response) { if (log.IsDebugEnabled) { if (response != null) { var body = response.ReadToEnd(); log.ErrorFormat("Headers:\n{0}\nBody:{1}", response.Headers,body); } } } public static TextReader GetLoggedReader(this Stream stream, ILog log) { var reader = new StreamReader(stream); if (!log.IsDebugEnabled) return reader; using (reader) { var body = reader.ReadToEnd(); log.DebugFormat("JSON response: {0}", body); return new StringReader(body); } } } }