2 using System.Collections.Generic;
9 namespace Pithos.Network
11 public static class WebExtensions
14 public static string ReadToEnd(this HttpWebResponse response)
16 using (var stream = response.GetResponseStream())
20 using (var reader = new StreamReader(stream))
22 var body = reader.ReadToEnd();
28 public static void LogError(this ILog log,HttpWebResponse response)
30 if (log.IsDebugEnabled)
34 var body = response.ReadToEnd();
35 log.ErrorFormat("Headers:\n{0}\nBody:{1}", response.Headers,body);
40 public static TextReader GetLoggedReader(this Stream stream, ILog log)
42 var reader = new StreamReader(stream);
43 if (!log.IsDebugEnabled)
48 var body = reader.ReadToEnd();
49 log.DebugFormat("JSON response: {0}", body);
50 return new StringReader(body);
55 public static TOut NullSafe<TIn, TOut>(this TIn obj, Func<TIn, TOut> memberAction)
57 //Note we should not use obj != null because it can not test value types and also
58 //compiler has to lift the type to a nullable type for doing the comparision with null.
59 return (!EqualityComparer<TIn>.Default.Equals(obj, default(TIn))) ? memberAction(obj) : default(TOut);