.NET:验证 URL 是否为图像

发布于 2024-08-05 01:48:26 字数 78 浏览 3 评论 0原文

我需要验证用户指定的 URL 是否指向图像。所以我确实需要一种方法来确定 URL 字符串是否指向有效图像。我怎样才能在.NET 中做到这一点?

I have a requirement to verify that a user-specified URL is to an image. So really I need a way to determine that a URL string points to a valid image. How can I do this in .NET?

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

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

发布评论

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

评论(3

饮湿 2024-08-12 01:48:26

如果不下载文件(至少是其中的一部分),您就无法做到这一点。使用 WebClient 获取 URL 并尝试从返回的 byte[] 创建新的 Bitmap。如果成功了,那就真的是一个形象了。否则,它会在过程中的某个地方抛出异常。

顺便说一句,您可以发出 HEAD 请求并检查响应中的 Content-Type 标头(如果存在)。然而,这种方法并不是万无一失的。服务器可能会使用无效的 Content-Type 标头进行响应。

You can't do that without downloading the file (at least a part of it). Use a WebClient to fetch the URL and try creating a new Bitmap from the returned byte[]. If it was successful, it's really an image. Otherwise, it will throw an exception somewhere in the process.

By the way, you can issue a HEAD request and check the Content-Type header in the response (if it's there). However, this method is not fool-proof. The server can respond with an invalid Content-Type header.

江湖正好 2024-08-12 01:48:26

我将使用 HttpWebRequest 获取标头,然后检查内容类型以及内容长度是否非零。

I would use HttpWebRequest to get the headers, then check the content type, and that the content length is non-zero.

愛放△進行李 2024-08-12 01:48:26

这是我现在正在使用的。有什么批评吗?

public class Image
{
    public static bool Verifies(string url)
    {
        if (url == null)
        {
            throw new ArgumentNullException("url");
        }

        Uri address;

        if (!Uri.TryCreate(url, UriKind.Absolute, out address))
        {
            return false;
        }

        using (var downloader = new WebClient())
        {
            try
            {
                var image = new Bitmap(downloader.OpenRead(address));
            }
            catch (Exception ex)
            {
                if (// Couldn't download data
                    ex is WebException ||
                    // Data is not an image
                    ex is ArgumentException)
                {
                    return false;
                }
                else
                {
                    throw;
                }
            }
        }

        return true;
    }
}

Here's what I'm using now. Any critiques?

public class Image
{
    public static bool Verifies(string url)
    {
        if (url == null)
        {
            throw new ArgumentNullException("url");
        }

        Uri address;

        if (!Uri.TryCreate(url, UriKind.Absolute, out address))
        {
            return false;
        }

        using (var downloader = new WebClient())
        {
            try
            {
                var image = new Bitmap(downloader.OpenRead(address));
            }
            catch (Exception ex)
            {
                if (// Couldn't download data
                    ex is WebException ||
                    // Data is not an image
                    ex is ArgumentException)
                {
                    return false;
                }
                else
                {
                    throw;
                }
            }
        }

        return true;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文