How to check if a file exists on an webserver by i

2020-02-11 02:46发布

in our application we have some kind of online help. It works really simple: If the user clicks on the help button a URL is build depending on the current language and help context (e.g. "http://example.com/help/" + [LANG_ID] + "[HELP_CONTEXT]) and called within the browser.

So my question is: How can i check if a file exists on the web server without loading the complete file content?

Thanks for your Help!

Update: Thanks for your help. My question has been answered. Now we have proxy authentication problems an cannot send the HTTP request ;)

标签: c# .net http
7条回答
够拽才男人
2楼-- · 2020-02-11 03:08

You can use .NET to do a HEAD request and then look at the status of the response.

Your code would look something like this (adapted from The Lowly HTTP HEAD Request):

// create the request
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;

// instruct the server to return headers only
request.Method = "HEAD";

// make the connection
HttpWebResponse response = request.GetResponse() as HttpWebResponse;

// get the status code
HttpStatusCode status = response.StatusCode;

Here's a list detailing the status codes that can be returned by the StatusCode enumerator.

查看更多
登录 后发表回答