代码之家  ›  专栏  ›  技术社区  ›  jaywon

如何在C中捕获ftpWebResponse异常#

  •  4
  • jaywon  · 技术社区  · 14 年前

    我正在C中构建一个ftp实用程序类。在这种情况下, WebException 在呼叫时引发 FtpWebRequest.GetResponse() ,对于远程服务器上不存在的请求文件,引发异常 FtpWebResponse 变量超出范围。

    但即使我在 try..catch 块我得到一个编译错误,说“使用未分配的局部变量‘response’”,但据我所知,在通过 ftpWebRequest.getResponse()。 方法。

    有人能给我建议吗?还是我遗漏了一些明显的东西?

    谢谢!

    这是我目前的方法:

    private void Download(string ftpServer, string ftpPath, string ftpFileName, string localPath, 
                               string localFileName, string ftpUserID, string ftpPassword)
        {
            FtpWebRequest reqFTP;
            FtpWebResponse response;
            try
            {
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://"
                   + ftpServer + "/" + ftpPath + "/" + ftpFileName));
                reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
                reqFTP.UseBinary = true;
                reqFTP.Credentials = new NetworkCredential(ftpUserID,
                                                           ftpPassword);
    
                /* HERE IS WHERE THE EXCEPTION IS THROWN FOR FILE NOT AVAILABLE*/
                response = (FtpWebResponse)reqFTP.GetResponse();
                Stream ftpStream = response.GetResponseStream();
    
    
                FileStream outputStream = new FileStream(localPath + "\\" +
                   localFileName, FileMode.Create);
    
                long cl = response.ContentLength;
                int bufferSize = 2048;
                int readCount;
                byte[] buffer = new byte[bufferSize];
    
                readCount = ftpStream.Read(buffer, 0, bufferSize);
                while (readCount > 0)
                {
                    outputStream.Write(buffer, 0, readCount);
                    readCount = ftpStream.Read(buffer, 0, bufferSize);
                }
    
                ftpStream.Close();
                outputStream.Close();
                response.Close();
            }
            catch (WebException webex)
            {
                /*HERE THE response VARIABLE IS UNASSIGNED*/
                if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable) { 
                    //do something
                }
            }
    
    3 回复  |  直到 14 年前
        1
  •  6
  •   Lucero    14 年前

    作为解决这个问题的一般方法,只需分配 null 首先响应,然后签入catch块(如果是) 无效的 .

        FtpWebResponse response = null;
        try
        {
    ...
        }
        catch (WebException webex)
        {
            if ((response != null) && (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)) { 
                //do something
            }
        }
    

    但是,在这种特定的情况下,您在 WebException 实例(包括 server response )!

        2
  •  2
  •   Community Mike Causer    7 年前

    在此问题中可以找到问题的正确解决方案:
    How to check if file exists on FTP before FtpWebRequest

    简而言之:
    由于出现错误,“response”变量将始终为空。您需要测试“webex.response”(强制转换)中的ftpWebResponse以获取状态代码。

        3
  •  1
  •   Darin Dimitrov    14 年前

    你可以给一个变量赋值:

    FtpWebRequest reqFTP = null;
    FtpWebResponse response = null;