如何检查文件是否退出上的Web服务器可以通过URL? [英] How to check if a file exits on an webserver by its URL?

查看:104
本文介绍了如何检查文件是否退出上的Web服务器可以通过URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的应用中有一些类型的在线帮助。它的工作原理很简单:如果用户点击帮助按钮的URL是构建依赖于当前的语言和帮助上下文(如http://example.com/help/+ LANG_ID] +[HELP_CONTEXT])和所谓在浏览器中。

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.

所以我的问题是:我如何检查文件是否存在Web服务器上,而无需加载整个文件内容

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

感谢您的帮助!

更新:感谢您的帮助。我的问题已经回答。 现在我们有代理认证问题的一个无法发送HTTP请求;)

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

推荐答案

您可以使用.NET做一个HEAD请求,然后看一下响应的状态。

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

您code会是这个样子(摘自<一个href="http://www.eggheadcafe.com/tutorials/aspnet/2c13cafc-be1c-4dd8-9129-f82f59991517/the-lowly-http-head-reque.aspx">The卑微的HTTP HEAD请求):

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;

下面是一个清单,详细说明状态codeS 的可以通过状态code枚举返回。

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

这篇关于如何检查文件是否退出上的Web服务器可以通过URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆