欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

获取下载文件长度

程序员文章站 2022-04-08 21:38:28
...
    private long getDownLength(string url)
    {
        long length = 0;
        try
        {
            var req = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url));
            req.Method = "HEAD";
            req.Timeout = 5000;
            var res = (HttpWebResponse)req.GetResponse();
            if (res.StatusCode == HttpStatusCode.OK)
            {
                length = res.ContentLength;
            }
            res.Close();
            return length;
        }
        catch (Exception)
        {
            return 0;
            throw;
        }
    }
相关标签: C# c#