底層連接已關閉:接收時發生意外錯誤 (The underlying connection was closed: An unexpected error occurred on a receive)


問題描述

底層連接已關閉:接收時發生意外錯誤 (The underlying connection was closed: An unexpected error occurred on a receive)

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.

(by chatThomas LevesqueJohn SaundersTim Jarvis)

參考文件

  1. The underlying connection was closed: An unexpected error occurred on a receive (CC BY‑SA 3.0/4.0)

#windows-server-2003 #windows-server-2008 #webclient #C#






相關問題

Metode Dll tidak dipanggil di windows server 2008 (Dll methods are not invoked on windows server 2008)

刪除窗口別名 (Removing windows aliases)

從 IIS 6 升級到 8.5:增加並發請求 (Upgrading from IIS 6 to 8.5: increased concurrent requests)

IOException 寫入事件日誌 (IOException writing to event log)

底層連接已關閉:接收時發生意外錯誤 (The underlying connection was closed: An unexpected error occurred on a receive)

Win2003終端服務器的小程序加載問題 (Applet loading problem with Win2003 Terminal Server)

確保在 Windows Server 2003 64 位上安裝了 ASP.NET 3.5 Framework (Ensure that ASP.NET 3.5 Framework is installed on Windows Server 2003 64-bit)

為沒有證書模板的 PKCS#10 CSR 頒發證書。0x80094801 MS W2K3企業CA (Issue certificate for a PKCS#10 CSR without Cert template. 0x80094801 MS W2K3 enterprise CA)

操作系統升級後無法從 ANT 訪問環境變量 (Can't access env vars from ANT after OS upgrade)

在 Windows 上自動啟動批處理文件 (Autostart a batch file on Windows)

為什麼從 PHP 腳本調用 (ImageMagick) convert.exe 會導致頁面無響應? (Why is calling (ImageMagick) convert.exe from PHP script resulting in an unresponsive page?)

即使在修補程序之後 SHA256 也無法正常工作 (SHA256 not working even after hotfixes)







留言討論