验证文件存在于网站上

发布于 2024-12-05 05:54:44 字数 382 浏览 0 评论 0原文

我正在尝试创建一个简单的函数来验证网站上是否存在特定文件。

Web 请求设置为 head,以便我可以获取文件长度而不是下载整个文件,但我收到“无法连接到远程服务器”异常。 如何验证网站上是否存在文件?

    WebRequest w;

    WebResponse r;

    w = WebRequest.Create("http://website.com/stuff/images/9-18-2011-3-42-16-PM.gif");
    w.Method = "HEAD";
    r = w.GetResponse();

编辑:我的错,在我检查日志后发现我的防火墙阻止了http请求。 它没有提示我输入例外规则,所以我认为这是一个错误。

I am attempting to make a simple function that verifies that a specific file exists on a website.

The web request is set to head so I can get the file length instead of downloading the entire file, but I get "Unable to connect to the remote server" exception.
How can I verify a file exists on a website?

    WebRequest w;

    WebResponse r;

    w = WebRequest.Create("http://website.com/stuff/images/9-18-2011-3-42-16-PM.gif");
    w.Method = "HEAD";
    r = w.GetResponse();

edit: My bad, turns out my firewall was blocking http requests after I checked the log.
It didnt prompt me for an exception rule so I assumed it was a bug.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜味超标? 2024-12-12 05:54:44

我已经测试过这个,它工作正常:

private bool testRequest(string urlToCheck)
{
    var wreq = (HttpWebRequest)WebRequest.Create(urlToCheck);

    //wreq.KeepAlive = true;
    wreq.Method = "HEAD";

    HttpWebResponse wresp = null;

    try
    {
        wresp = (HttpWebResponse)wreq.GetResponse();

        return (wresp.StatusCode == HttpStatusCode.OK);
    }
    catch (Exception exc)
    {
        System.Diagnostics.Debug.WriteLine(String.Format("url: {0} not found", urlToCheck));
        return false;
    }
    finally
    {
        if (wresp != null)
        {
            wresp.Close();
        }
    }
}

尝试使用以下网址: http://www .centrosardegna.com/images/losa/losaabbasanta.png 然后修改图片名称,会返回 false。 ;-)

I've tested this and it works fine:

private bool testRequest(string urlToCheck)
{
    var wreq = (HttpWebRequest)WebRequest.Create(urlToCheck);

    //wreq.KeepAlive = true;
    wreq.Method = "HEAD";

    HttpWebResponse wresp = null;

    try
    {
        wresp = (HttpWebResponse)wreq.GetResponse();

        return (wresp.StatusCode == HttpStatusCode.OK);
    }
    catch (Exception exc)
    {
        System.Diagnostics.Debug.WriteLine(String.Format("url: {0} not found", urlToCheck));
        return false;
    }
    finally
    {
        if (wresp != null)
        {
            wresp.Close();
        }
    }
}

try it with this url: http://www.centrosardegna.com/images/losa/losaabbasanta.png then modify the image name and it will return false. ;-)

永言不败 2024-12-12 05:54:44
try
{
    WebRequest request = HttpWebRequest.Create("http://www.microsoft.com/NonExistantFile.aspx");
    request.Method = "HEAD"; // Just get the document headers, not the data.
    request.Credentials = System.Net.CredentialCache.DefaultCredentials;
    // This may throw a WebException:
    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
        if (response.StatusCode == HttpStatusCode.OK)
        {
            // If no exception was thrown until now, the file exists and we 
            // are allowed to read it. 
            MessageBox.Show("The file exists!");
        }
        else
        {
            // Some other HTTP response - probably not good.
            // Check its StatusCode and handle it.
        }
    }
}
catch (WebException ex)
{
    // Cast the WebResponse so we can check the StatusCode property
    HttpWebResponse webResponse = (HttpWebResponse)ex.Response;

    // Determine the cause of the exception, was it 404?
    if (webResponse.StatusCode == HttpStatusCode.NotFound)
    {
        MessageBox.Show("The file does not exist!");
    }
    else
    {
        // Handle differently...
        MessageBox.Show(ex.Message);
    }
}
try
{
    WebRequest request = HttpWebRequest.Create("http://www.microsoft.com/NonExistantFile.aspx");
    request.Method = "HEAD"; // Just get the document headers, not the data.
    request.Credentials = System.Net.CredentialCache.DefaultCredentials;
    // This may throw a WebException:
    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
        if (response.StatusCode == HttpStatusCode.OK)
        {
            // If no exception was thrown until now, the file exists and we 
            // are allowed to read it. 
            MessageBox.Show("The file exists!");
        }
        else
        {
            // Some other HTTP response - probably not good.
            // Check its StatusCode and handle it.
        }
    }
}
catch (WebException ex)
{
    // Cast the WebResponse so we can check the StatusCode property
    HttpWebResponse webResponse = (HttpWebResponse)ex.Response;

    // Determine the cause of the exception, was it 404?
    if (webResponse.StatusCode == HttpStatusCode.NotFound)
    {
        MessageBox.Show("The file does not exist!");
    }
    else
    {
        // Handle differently...
        MessageBox.Show(ex.Message);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文