Revision 692ec33b trunk/Pithos.Network/RestClient.cs

b/trunk/Pithos.Network/RestClient.cs
85 85
        }
86 86

  
87 87

  
88
        private WebHeaderCollection _responseHeaders;
89

  
90
        public new WebHeaderCollection ResponseHeaders
91
        {
92
            get
93
            {
94
                if (base.ResponseHeaders==null)
95
                {
96
                    return _responseHeaders;
97
                }
98
                else
99
                {
100
                    _responseHeaders = null;
101
                    return base.ResponseHeaders;   
102
                }
103
                
104
            }
105

  
106
            set { _responseHeaders = value; }
107
        } 
108 88
        protected override WebRequest GetWebRequest(Uri address)
109 89
        {
110 90
            TimedOut = false;
......
187 167
            //Does the response have any content to log?
188 168
            if (exc.Response.ContentLength > 0)
189 169
            {
190
                var content = GetContent(exc.Response);
170
                var content = LogContent(exc.Response);
191 171
                Log.ErrorFormat(content);
192 172
            }
193 173
            return false;
......
205 185

  
206 186
        public DateTime LastModified { get; private set; }
207 187

  
208
        private static string GetContent(WebResponse webResponse)
188
        private static string LogContent(WebResponse webResponse)
209 189
        {
210 190
            if (webResponse == null)
211 191
                throw new ArgumentNullException("webResponse");
212 192
            Contract.EndContractBlock();
213 193

  
214
            string content;
215
            using (var stream = webResponse.GetResponseStream())
216
            using (var reader = new StreamReader(stream))
194
            //The response stream must be copied to avoid affecting other code by disposing of the 
195
            //original response stream.
196
            var stream = webResponse.GetResponseStream();            
197
            using(var memStream=new MemoryStream((int) stream.Length))
198
            using (var reader = new StreamReader(memStream))
217 199
            {
218
                content = reader.ReadToEnd();
200
                stream.CopyTo(memStream);                
201
                string content = reader.ReadToEnd();
202

  
203
                stream.Seek(0,SeekOrigin.Begin);
204
                return content;
219 205
            }
220
            return content;
221 206
        }
222 207

  
223 208
        public string DownloadStringWithRetry(string address,int retries=0)
......
232 217
            
233 218
            var actualRetries = (retries == 0) ? Retries : retries;
234 219

  
235
            
220
            var uriString = String.Join("/", BaseAddress.TrimEnd('/'), actualAddress);
221

  
236 222
            var task = Retry(() =>
237
            {
238
                var uriString = String.Join("/", BaseAddress.TrimEnd('/'), actualAddress);                
223
            {                
239 224
                var content = base.DownloadString(uriString);
240 225

  
241 226
                if (StatusCode == HttpStatusCode.NoContent)
......
308 293
                if (method == "PUT")
309 294
                    request.ContentLength = 0;
310 295

  
296
                //Have to use try/finally instead of using here, because WebClient needs a valid WebResponse object
297
                //in order to return response headers
311 298
                var response = (HttpWebResponse)GetWebResponse(request);
312
                //var response = (HttpWebResponse)request.GetResponse();
299
                try
300
                {
301
                    LastModified = response.LastModified;
302
                    StatusCode = response.StatusCode;
303
                    StatusDescription = response.StatusDescription;
304
                }
305
                finally
306
                {
307
                    response.Close();
308
                }
313 309
                
314
                //ResponseHeaders= response.Headers;
315

  
316
                LastModified = response.LastModified;
317
                StatusCode = response.StatusCode;
318
                StatusDescription = response.StatusDescription;
319
                response.Close();
320 310

  
321 311
                return 0;
322 312
            }, actualRetries);

Also available in: Unified diff