c# - 底层连接已关闭 : An unexpected error occurred on a receive

标签 c# windows-server-2008 windows-server-2003 webclient

当我在我的 win2k8 机器上尝试我的程序时它运行良好但是在 win 2k3 上它给我这个错误消息

这是产生错误的代码

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

这是奇怪的部分。如果我完全禁用服务器上的防火墙。错误消失了。但如果我将程序添加到异常(exception)列表并打开防火墙,它仍然会出错。

在网上搜索了几天,找不到任何解决方案。

最佳答案

您应该尝试对 FTP 使用被动模式。 WebClient 类不允许这样做,但 FtpWebRequest 允许。

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);
    }
}

关于c# - 底层连接已关闭 : An unexpected error occurred on a receive,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/875348/

相关文章:

asp.net - Windows 2008 R2 Web服务器上的.NET Web应用程序只能寻址16 GB RAM,而不能寻址32 GB-为什么?

iis-6 - 证书 : Cannot find the certificate and private key for decryption Error when sign

windows-server-2003 - 如何确定 Windows 服务器上的端口是否打开?

winforms - 需要 dpiAware 可执行文件才能在 Windows XP/2003 中工作

c# - 在 Razor Pages 中调用操作

c# - Diagnostics.Process 的替代方案来运行简单的 shell 命令!

c# - PictureBox 在鼠标滚轮上缩放和滚动 C#

c# - 更新 WebService 实体定义的影响

iis - 服务器 - Windows 2K8/IIS7/ColdFusion 抛出间歇性 403 错误

vb6 - Windows 2008 64 位版本上的 TAPI