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); } } public static TOut NullSafe(this TIn obj, Func memberAction) { //Note we should not use obj != null because it can not test value types and also //compiler has to lift the type to a nullable type for doing the comparision with null. return (!EqualityComparer.Default.Equals(obj, default(TIn))) ? memberAction(obj) : default(TOut); } } }