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

ぃ、小莉子 提交于 2020-01-03 18:42:08

问题


When I try my program on my win2k8 machine it runs fine but on win 2k3 it gives me this error that error message

here's the code that' generating the error

WebClient wc = new WebClient(); 
wc.DownloadFile("ftp://ftp.website.com/sample.zip");

there's the weird part. if i disable the firewall on the server completely. the error goes away. but it still errors if i add the program to the exception list and turn on the firewall.

have search the web for days, couldn't find any solution.


回答1:


You should try using passive mode for FTP. The WebClient class doesn't allow that, but FtpWebRequest does.

FtpWebRequest request = WebRequest.Create("ftp://ftp.website.com/sample.zip") 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);
    }
}



回答2:


Please post the complete exception, including any InnerException:

try
{
    WebClient wc = new WebClient(); 
    wc.DownloadFile("ftp://ftp.website.com/sample.zip");
}
catch (Exception ex)
{
    Console.WriteLine(ex.ToString()); // Or Debug.Trace, or whatever
    throw;    // As if the catch were not present
}



回答3:


I had a similar issue (no ftp) and a different solution

The production server environment had been made so secure that the server could not reach the URL.

Quick test is to use a browser on the box and see if you can navigate to the url.

Hope this helps someone.



来源:https://stackoverflow.com/questions/875348/the-underlying-connection-was-closed-an-unexpected-error-occurred-on-a-receive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!