views:

201

answers:

2

I am trying to upload a PGP encrypted file through FTP. But I am getting an error message as follows:

The underlying connection was closed: An unexpected error occurred on a receive.

I am using the following code and getting the error at line:

Stream ftpStream = response.GetResponse();

Is there any one who can help me out ASAP.

Following is the code sample:

FtpWebRequest request =
  WebRequest.Create("ftp://ftp.website.com/sample.txt.pgp") as FtpWebRequest; 
request.UsePassive = true;    
FtpWebResponse response = request.GetResponse() as FtpWebResponse;    
Stream ftpStream = response.GetResponse();    
int bufferSize = 8192;    
byte[] buffer = new byte[bufferSize];    
using (FileStream fileStream =
       new FileStream("localfile.zip", FileMode.Create, FileAccess.Write))
{
    int nBytes;
    while((nBytes = ftpStream.Read(buffer, 0, bufferSize) > 0)
    {
        fileStream.Write(buffer, 0, nBytes);
    }
}

Regards, Sumeet

A: 

Why are you trying to upload using GetResponse()? You need at least request.Method = WebRequestMethods.Ftp.UploadFile; and request.GetRequestStream();

wRAR
A: 

Stream ftpStream = response.GetResponseStream();

DmitryK