c# - 如何在 C#/ASP.NET 中检查图像是否存在于 http ://someurl/myimage. jpg

标签 c# asp.net http url

如何检查图像是否存在于 http://someurl/myimage.jpg在 C#/ASP.NET 中 似乎应该有一种方法来检查这一点——但我找不到。

我找到了 this , 但它并没有真正回答问题。

最佳答案

这段代码应该可以工作:

private static bool UrlExists(string url)
{
    try
    {
        new System.Net.WebClient().DownloadData(url);
        return true;
    }
    catch (System.Net.WebException e)
    {
        if (((System.Net.HttpWebResponse)e.Response).StatusCode == System.Net.HttpStatusCode.NotFound)
            return false;
        else
            throw;
    }
}

关于c# - 如何在 C#/ASP.NET 中检查图像是否存在于 http ://someurl/myimage. jpg,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1639878/

相关文章:

c# - 添加到字符串字段

c# - 如何声明新类型

c# - 对于从 IEnumerable<T> 继承的 .Net 类,为什么 GetEnumerator 在 COM 中不可见?

asp.net - GridView AutoGenerateColumns 和排序

c# - Oracle.DataAccess 错误

c# - Facebook Graph API 在用户未登录时获取数据

javascript - 如何发送带有 header 参数的 HTTP 请求?

android - 在 Android 中模拟 HTML 表单

C#后缀和前缀递增/递减重载区别

python - Flask 中的基本 HTTP 身份验证 AttributeError : 'NoneType' error