简体   繁体   中英

C# Check if HTTP URL exists without using HEAD

I need to check if a remote file exists on a web server over HTTP that does not support HEAD requests (returns a 404 even if file exists). GET requests does return a 200 but I don't need to download the file which can be large. I also tried socket connections but they return 200 even if the file doesn't exist. Any suggestions on how to check if a remote URL exists efficiently? Thanks.

Well, you need to do a request to download the file but don't read the response stream. If you use HttpWebRequest, the request will be executed when the server has sent the headers, not the content of the file, so you can get the response, check the headers and dispose it, it will not download the full file, just some bytes while you were checking the headers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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