简体   繁体   English

检查是否存在远程文件

[英]Check if a remote file exists

When retrieving remote files from a serrver via HTTP, there is one situation where I do not know the exact number of files I fill need to retrieve, incrementing a part of the filename until either the file does not exist or I hit a pre-defined threshold. 通过HTTP从服务器获取远程文件时,在一种情况下,我不知道需要填充的确切文件数量,增加文件名的一部分,直到文件不存在或命中预先定义的文件阈。

What is the best way to test if a remote file exists using C#? 使用C#测试远程文件是否存在的最佳方法是什么? Obvisouly I could send a webrequest and see if it times out, but that would not be optimal! 显然,我可以发送一个web请求,看看是否超时,但这不是最佳选择!

404 Errors (file not found) don't time out. 404错误(找不到文件)不会超时。 They return immediately with the status code 404. 他们立即返回状态码404。

Check the response status code of the request. 检查请求的响应状态代码。 200 is OK, 404 is File Not Found 200没问题,404找不到文件

顺便说一句,如果您只需要检查文件是否存在而无需实际下载,则应该使用HEAD请求(HttpWebRequest.Method =“ HEAD”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM