如何在 C# 中捕获 FtpWebResponse 异常
我正在用 C# 构建一个 FTP 实用程序类。在调用 FtpWebRequest.GetResponse()
时引发 WebException
的情况下,在我的例子中,由于远程服务器上不存在所请求的文件而引发异常FtpWebResponse
变量超出范围。
但即使我在 try..catch
块之外声明变量,我也会收到一个编译错误,提示“使用未分配的局部变量'响应'”,但据我所知,没有办法分配它,直到您通过 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
}
}
I am building an FTP utility class in C#. In the case that a WebException
is thrown on a call to FtpWebRequest.GetResponse()
, in my case the exception is thrown for the requested file not existing on the remote server the FtpWebResponse
variable is out of scope.
But even if I declare the variable outside the try..catch
block I get a compile error saying "Use of unassigned local variable 'response'", but as far as I can tell there is no way to assign it until you assign the response via the FtpWebRequest.GetResponse()
method.
Can someone please advise, or am I missing something obvious?
Thanks!
Here is my current method:
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
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
作为解决此问题的通用方法,只需首先将
null
分配给响应,然后检查 catch 块是否为null
。但是,在这种特定情况下,您拥有
WebException
实例所需的所有属性(包括 服务器响应)!As generic way to solve this, just assign
null
to the response first and then check in the catch block if it isnull
.However, in this specific case, you have all the properties you need on the
WebException
instance (including the server response)!问题的正确解决方案可以在这个问题中找到:
如何在 FtpWebRequest 之前检查 FTP 上是否存在文件
简而言之:
由于错误,您的“响应”变量将始终为空。您需要从“webex.Response”测试 FtpWebResponse(强制转换)以获取 StatusCode。
The correct solution to the problem can be found in this question here:
How to check if file exists on FTP before FtpWebRequest
In short:
Your 'response' variable will always be null because of the error. You need to test the FtpWebResponse from 'webex.Response' (cast it) to get the StatusCode.
好吧,你总是可以分配一个变量:
Well you could always assign a variable: