开发者

FtpWebRequest says Stream is Disposed

开发者 https://www.devze.com 2022-12-08 22:42 出处:网络
I was wondering if anyone knows the answer to why my code casts an exeption when I do the fileStream.Read call, with the message \"The stream is disposed\"

I was wondering if anyone knows the answer to why my code casts an exeption when I do the fileStream.Read call, with the message "The stream is disposed"

I can list the files in the folder and using my browser to the fileUrl works fine. No proxies is involved. UnifiedFile is just an subclass of VirtualFile which comes from the CMS i work with. (it's an image that i'm trying to download)

FtpWebRequest fileRequest = (FtpWebRequest)FtpWebRequest.Create(fileUrl)开发者_JAVA百科;
fileRequest.Method = WebRequestMethods.Ftp.DownloadFile;
fileRequest.Credentials = credentials;
fileRequest.EnableSsl = this.EnableSsl;
fileRequest.UseBinary = true;
fileRequest.UsePassive = true;
fileRequest.KeepAlive = true;
fileRequest.Proxy = null;

using (FtpWebResponse fileResponse = (FtpWebResponse)fileRequest.GetResponse())
{
    using (Stream fileStream = response.GetResponseStream())
    {
        UnifiedFile createdFile = PictureManager.Instance.CreateOriginalFile(name);
        using (Stream targetStream = createdFile.Open(FileMode.OpenOrCreate))
        {
             byte[] buffer = new byte[2048];

             // Read the file
             while (true)
             {
                int bytesRead = fileStream.Read(buffer, 0, buffer.Length);
                // Reached end of stream, close
                if (bytesRead == 0)
                  break;

                targetStream.Write(buffer, 0, bytesRead);
              }

         }
   }

}

Edit: Found the bug; the "GetResponseStream" is done to the previous request and therefor wrong request. doh

Thanks Guys for the "Granny help" ;-) (Swedish expression so don't know if it really applies in english)


You should be checking the response StatusCode before continuing. I suspect that the request has failed so there is no response body.

0

精彩评论

暂无评论...
验证码 换一张
取 消