在C#中,是否可以检测文件的网址是图像还是视频?是否有这样的标题值?
我有以下代码来获取Web文件的文件大小:
System.Net.WebRequest req = System.Net.HttpWebRequest.Create("http://test.png");
req.Method = "HEAD";
using (System.Net.WebResponse resp = req.GetResponse())
{
int ContentLength;
if(int.TryParse(resp.Headers.Get("Content-Length"), out ContentLength))
{
//Do something useful with ContentLength here
}
}
是否可以修改此代码以查看文件是图像还是视频?
提前致谢
答案 0 :(得分:0)
答案 1 :(得分:0)
您正在寻找的是" Content-Type"头
string uri = "http://assets3.parliament.uk/iv/main-large//ImageVault/Images/id_7382/scope_0/ImageVaultHandler.aspx.jpg";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "HEAD";
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
var contentType = response.Headers["Content-Type"];
Console.WriteLine(contentType);
}