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.
1 Answer
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.
4 Comments
Christian Gollhardt
HttpWebRequest would be GET? OP says, there are wrong headers.Gusman
HttpWebRequest will be of the type the OP sets, so it can be GET POST or anything the server expects, just roll the same request as if you were downloading the file.
Christian Gollhardt
My bad, the wrong 200 Code was not related to
GET. +1HBCondo
Thank you for your reply, @Gusman. I'm trying your suggestion but the response is still being downloaded in its entirety. Do you mind providing a code snippet showing the usage of HttpWebRequest? Thanks.