获得远程文件MediaType

//获得远程文件MediaType

using HttpRequestMessage request = new(HttpMethod.Get, remoteImagePath);
using var response = await httpClient.SendAsync(request);

if (response.Content.Headers.ContentType == null)
{
throw new Exception("无法获取Headers.ContentType信息");
}
var mediaType = response.Content.Headers.ContentType.MediaType;
if (mediaType == null)
{
throw new Exception("无法获取MediaType信息");
}
Console.WriteLine(mediaType.ToLower());
var imageExt = new[] { "image/gif", "image/jpeg", " image/png" };
if (!((IList)imageExt).Contains(mediaType.ToLower()))
{
throw new Exception("非图片文件");
}

原文地址:https://www.cnblogs.com/yibinboy/p/15566600.html