如何检查 WebClient 请求是否有 404 错误
我正在编写一个下载到文件的程序。第二个文件不是必需的,仅在某些情况下包含在内。当不包含第二个文件时,它将返回 HTTP 404
错误。
现在的问题是,当返回此错误时,整个程序就会结束。我想要的是继续程序并忽略 HTTP 错误。所以,我的问题是如何从 WebClient.DownloadFile
请求捕获 HTTP 404
错误?
这是当前使用的代码::
WebClient downloader = new WebClient();
foreach (string[] i in textList)
{
String[] fileInfo = i;
string videoName = fileInfo[0];
string videoDesc = fileInfo[1];
string videoAddress = fileInfo[2];
string imgAddress = fileInfo[3];
string source = fileInfo[5];
string folder = folderBuilder(path, videoName);
string infoFile = folder + '\\' + removeFileType(retrieveFileName(videoAddress)) + @".txt";
string videoPath = folder + '\\' + retrieveFileName(videoAddress);
string imgPath = folder + '\\' + retrieveFileName(imgAddress);
System.IO.Directory.CreateDirectory(folder);
buildInfo(videoName, videoDesc, source, infoFile);
textBox1.Text = textBox1.Text + @"begining download of files for" + videoName;
downloader.DownloadFile(videoAddress, videoPath);
textBox1.Text = textBox1.Text + @"Complete video for" + videoName;
downloader.DownloadFile(imgAddress, imgPath);
textBox1.Text = textBox1.Text + @"Complete img for" + videoName;
}
I have a program I'm writing that downloads to files. The second file is not neccassary and is only some times included. When the second file is not included it will return an HTTP 404
error.
Now, the problem is that when this error is returned it ends the whole program. What I want is to continue the program and ignore the HTTP error. So, my question is how do I catch an HTTP 404
error from a WebClient.DownloadFile
request?
This is the code currently used::
WebClient downloader = new WebClient();
foreach (string[] i in textList)
{
String[] fileInfo = i;
string videoName = fileInfo[0];
string videoDesc = fileInfo[1];
string videoAddress = fileInfo[2];
string imgAddress = fileInfo[3];
string source = fileInfo[5];
string folder = folderBuilder(path, videoName);
string infoFile = folder + '\\' + removeFileType(retrieveFileName(videoAddress)) + @".txt";
string videoPath = folder + '\\' + retrieveFileName(videoAddress);
string imgPath = folder + '\\' + retrieveFileName(imgAddress);
System.IO.Directory.CreateDirectory(folder);
buildInfo(videoName, videoDesc, source, infoFile);
textBox1.Text = textBox1.Text + @"begining download of files for" + videoName;
downloader.DownloadFile(videoAddress, videoPath);
textBox1.Text = textBox1.Text + @"Complete video for" + videoName;
downloader.DownloadFile(imgAddress, imgPath);
textBox1.Text = textBox1.Text + @"Complete img for" + videoName;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
如果您特别想要捕获错误 404:
或者,使用 C# 7 或更高版本:
If you specifically want to catch error 404:
Or, using C# 7 or later:
WebClient 将为所有 4xx 和 5xx 响应抛出 WebException。
WebClient will throw a WebException for all 4xx and 5xx responses.
将
try
catch
放入foreach
循环中。下载文件的私有函数
您可以检查
WebException
的状态。根据错误代码,您可以继续或中断。阅读更多内容@MSDN
建议
+
。希望这对你有用。
Put the
try
catch
inside yourforeach
Loop.Private function to Download file
You can check the
WebException
for status. Depending upon the error code you can continue or break.Read More @ MSDN
Suggestion
+
.Hope this works for you.
在代码中使用
try catch
WebException
并检查Exception
消息 - 它将包含 httpStatusCode
。您可以清除
异常
并继续。Use a
try catch
WebException
in your code and examine theException
message - it will contain the httpStatusCode
.You can clear the
Exception
and continue.您可以尝试使用此代码从 WebException 或 OpenReadCompletedEventArgs.Error 获取 HTTP 状态代码:
you can try this code to get HTTP status code from WebException or OpenReadCompletedEventArgs.Error:
在循环内使用带有 WebException 的 try{} catch{} 块!
不知道你正在使用什么 IDE,但是使用 Visual Studio 你可以获得很多有关异常的信息:)
Use a try{} catch{} block with the WebException inside your loop !
Dunno what IDE u are using but with Visual Studio u can get a lot of information about the exception :)
重要提示:发生 404 故障时,
DownloadFileTaskAsync
将引发异常,但也会创建一个空文件。至少可以说这可能会令人困惑!我花了太长时间才意识到这段代码除了抛出异常之外还创建了一个空文件:
相反,我切换到了这个(
DownloadDataTaskAsync
而不是File
):*I'我不确定 500 行为,但可以肯定 404 会这样做。
Important: On a 404 failure
DownloadFileTaskAsync
will throw an exception but will ALSO create an empty file. This can be confusing to say the least!Took me way too long to realize that this code creates an empty file in addition to throwing an exception:
Instead I switched to this (
DownloadDataTaskAsync
instead ofFile
):*I'm not sure about 500 behavior, but for sure a 404 does this.
正如其他人所写,try-catch 就足够了。
另一个技巧是使用 HTTP HEAD 来检查是否有任何东西(它比执行完整的 HTTP GET):
As other write, as try-catch would suffice.
Another tip is to use HTTP HEAD to check if there is anything there (it's lighter than doing a full HTTP GET):
我会像这样使用某物。
I would use sth like this.