Exemplo n.º 1
0
 public void ToFile(string path)
 {
     if (this.HasError)
     {
         throw new InvalidOperationException(Resources.InvalidOperationException_HttpResponse_HasError);
     }
     if (path == null)
     {
         throw new ArgumentNullException(nameof(path));
     }
     if (this.MessageBodyLoaded)
     {
         return;
     }
     try
     {
         using (FileStream fileStream = new FileStream(path, FileMode.Create))
         {
             foreach (HttpResponse.BytesWraper bytesWraper in this.GetMessageBodySource())
             {
                 fileStream.Write(bytesWraper.Value, 0, bytesWraper.Length);
             }
         }
     }
     catch (ArgumentException ex)
     {
         throw ExceptionHelper.WrongPath(nameof(path), (Exception)ex);
     }
     catch (NotSupportedException ex)
     {
         throw ExceptionHelper.WrongPath(nameof(path), (Exception)ex);
     }
     catch (Exception ex)
     {
         this.HasError = true;
         if (ex is IOException || ex is InvalidOperationException)
         {
             throw this.NewHttpException(Resources.HttpException_FailedReceiveMessageBody, ex);
         }
         throw;
     }
     if (this.ConnectionClosed())
     {
         this._request.Dispose();
     }
     this.MessageBodyLoaded = true;
 }